Skip to content

Commit

Permalink
Add Testcontainers Service Connection in examples (#63)
Browse files Browse the repository at this point in the history
Use Testcontainers OpenFGA Module to improve DevEx when starting the
example.

This is an alternative solution for #58 due to the current state of the
project. I'm planning to add another PRs to add native integration where
the DynamicPropertyRegistry is not needed.
  • Loading branch information
jimmyjames authored Oct 2, 2024
2 parents b035687 + 52739e9 commit 2ba1379
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
11 changes: 1 addition & 10 deletions examples/servlet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,9 @@ To use a different FGA server, update `src/main/resources/application.yaml` acco

## Usage

### Start OpenFGA server

In a terminal, start the OpenFGA Server:

```bash
docker pull openfga/openfga:latest
docker run --rm -e OPENFGA_HTTP_ADDR=0.0.0.0:4000 -p 4000:4000 -p 8081:8081 -p 3000:3000 openfga/openfga run
```

### Start the example application:

In a separate terminal, start the application:
In a terminal, start the application:

```bash
./gradlew bootRun
Expand Down
3 changes: 3 additions & 0 deletions examples/servlet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'dev.openfga:openfga-spring-boot-starter:0.0.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.testcontainers:openfga:1.20.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.openfga.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.testcontainers.openfga.OpenFGAContainer;

public class TestServletApp {

public static void main(String[] args) {
SpringApplication.from(ServletApp::main)
.with(TestConfig.class)
.run(args);
}

@TestConfiguration(proxyBeanMethods = false)
static class TestConfig {

@Bean
OpenFGAContainer container(DynamicPropertyRegistry registry) {
OpenFGAContainer container = new OpenFGAContainer("openfga/openfga:v1.4.3");
registry.add("openfga.api-url", container::getHttpEndpoint);
return container;
}

}
}
16 changes: 16 additions & 0 deletions examples/servlet/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="INFO"/>
</configuration>

0 comments on commit 2ba1379

Please sign in to comment.