Skip to content

Commit

Permalink
chore(java-sdk): add example java/kotlin projects
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Dec 28, 2023
1 parent 177c198 commit be91b94
Show file tree
Hide file tree
Showing 16 changed files with 1,002 additions and 0 deletions.
60 changes: 60 additions & 0 deletions config/clients/java/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,66 @@
"gradle-wrapper.properties.mustache" : {
"destinationFilename": "gradle/wrapper/gradle-wrapper.properties",
"templateType": "SupportingFiles"
},
"example/Makefile" : {
"destinationFilename": "example/Makefile",
"templateType": "SupportingFiles"
},
"example/README.md" : {
"destinationFilename": "example/README.md",
"templateType": "SupportingFiles"
},
"example/example1/README.md" : {
"destinationFilename": "example/example1/README.md",
"templateType": "SupportingFiles"
},
"example/example1/gradle/wrapper/gradle-wrapper.jar" : {
"destinationFilename": "example/example1/gradle/wrapper/gradle-wrapper.jar",
"templateType": "SupportingFiles"
},
"example/example1/gradle/wrapper/gradle-wrapper.properties" : {
"destinationFilename": "example/example1/gradle/wrapper/gradle-wrapper.properties",
"templateType": "SupportingFiles"
},
"example/example1/gradlew" : {
"destinationFilename": "example/example1/gradlew",
"templateType": "SupportingFiles"
},
"example/example1/build.gradle" : {
"destinationFilename": "example/example1/build.gradle",
"templateType": "SupportingFiles"
},
"example/example1/gradle.properties" : {
"destinationFilename": "example/example1/gradle.properties",
"templateType": "SupportingFiles"
},
"example/example1/settings.gradle" : {
"destinationFilename": "example/example1/settings.gradle",
"templateType": "SupportingFiles"
},
"example/example1/src/main/resources/example1-auth-model.json" : {
"destinationFilename": "example/example1/src/main/resources/example1-auth-model.json",
"templateType": "SupportingFiles"
},
"example/example1/src/main/java/dev/openfga/sdk/example/Example1.java" : {
"destinationFilename": "example/example1/src/main/java/dev/openfga/sdk/example/Example1.java",
"templateType": "SupportingFiles"
},
"example/example1/src/main/kotlin/dev/openfga/sdk/example/KotlinExample1.kt" : {
"destinationFilename": "example/example1/src/main/kotlin/dev/openfga/sdk/example/KotlinExample1.kt",
"templateType": "SupportingFiles"
},
"ExampleTest.java" : {
"destinationFilename": "src/test-integration/java/dev/openfga/sdk/example/ExampleTest.java",
"templateType": "SupportingFiles"
},
"example/example1/links/Example1.java" : {
"destinationFilename": "src/test-integration/java/dev/openfga/sdk/example/Example1.java",
"templateType": "SupportingFiles"
},
"example/example1/links/example1-auth-model.json" : {
"destinationFilename": "src/test-integration/resources/example1-auth-model.json",
"templateType": "SupportingFiles"
}
}
}
13 changes: 13 additions & 0 deletions config/clients/java/template/ExampleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.openfga.sdk.example;

import org.junit.jupiter.api.Test;

public class ExampleTest {
private final Example1 example1 = new Example1();

@Test
public void example1() throws Exception {
example1.module = "test-integration";
example1.run();
}
}
17 changes: 17 additions & 0 deletions config/clients/java/template/example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
all: build

project_name=example1
openfga_version=latest
language=java

build:
cd "${project_name}" && \
./gradlew -P language=$(language) build

run:
cd "${project_name}" && \
./gradlew -P language=$(language) run

run-openfga:
docker pull docker.io/openfga/openfga:${openfga_version} && \
docker run -p 8080:8080 docker.io/openfga/openfga:${openfga_version} run
49 changes: 49 additions & 0 deletions config/clients/java/template/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Examples of using the OpenFGA Java SDK

A set of Examples on how to call the OpenFGA Java SDK

### Examples
Example 1:
A bare-bones example. It creates a store, and runs a set of calls against it including creating a model, writing tuples and checking for access.
This example is implemented in both Java and Kotlin.


### Running the Examples

Prerequisites:
- `docker`
- `make`
- A Java Runtime Environment (JRE)

#### Run using a published SDK

Steps
1. Clone/Copy the example folder
2. Run `make` to build the project
3. If you have an OpenFGA server running, you can use it, otherwise run `make run-openfga` to spin up an instance (you'll need to switch to a different terminal after - don't forget to close it when done)
4. Run `make run` to run the example

#### Run using a local unpublished SDK build

Steps
1. Build the SDK
2. In the Example project file (e.g. `build.gradle`), comment out the part that specifies the remote SDK, e.g.
```groovy
dependencies {
implementation("dev.openfga:openfga-sdk:0.3.+")
// ...etc
}
```
and replace it with one pointing to the local gradle project, e.g.
```groovy
dependencies {
// implementation("dev.openfga:openfga-sdk:0.3.+")
implementation project(path: ':')
// ...etc
}
```
3. Run `make` to build the project
4. If you have an OpenFGA server running, you can use it, otherwise run `make run-openfga` to spin up an instance (you'll need to switch to a different terminal after - don't forget to close it when done)
5. Run `make run` to run the example
50 changes: 50 additions & 0 deletions config/clients/java/template/example/example1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Examples of using the OpenFGA Java SDK

A set of Examples on how to call the OpenFGA Java SDK

### Examples
Example 1:
A bare-bones example. It creates a store, and runs a set of calls against it including creating a model, writing tuples and checking for access.
This example is implemented in both Java and Kotlin.


### Running the Examples

Prerequisites:
- `docker`
- `make`
- A Java Runtime Environment (JRE)

#### Run using a published SDK

Steps
1. Clone/Copy the example folder
2. Run `make` to build the project
3. If you have an OpenFGA server running, you can use it, otherwise run `make run-openfga` to spin up an instance (you'll need to switch to a different terminal after - don't forget to close it when done)
4. Run `make run` to run the example
* This should run a Java example by default. Where implemented, it's possible to specify an alternate JVM language too, like `make run language=kotlin`.

#### Run using a local unpublished SDK build

Steps
1. Build the SDK
2. In the Example project file (e.g. `build.gradle`), comment out the part that specifies the remote SDK, e.g.
```groovy
dependencies {
implementation("dev.openfga:openfga-sdk:0.3.+")
// ...etc
}
```
and replace it with one pointing to the local gradle project, e.g.
```groovy
dependencies {
// implementation("dev.openfga:openfga-sdk:0.3.+")
implementation project(path: ':')
// ...etc
}
```
3. Run `make` to build the project
4. If you have an OpenFGA server running, you can use it, otherwise run `make run-openfga` to spin up an instance (you'll need to switch to a different terminal after - don't forget to close it when done)
5. Run `make run` to run the example
77 changes: 77 additions & 0 deletions config/clients/java/template/example/example1/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
plugins {
id 'application'
id 'com.diffplug.spotless' version '6.23.3'
id 'org.jetbrains.kotlin.jvm' version '2.0.0-Beta2'
}

application {
switch (language) {
case 'kotlin':
mainClass = 'dev.openfga.sdk.example.KotlinExample1'
break
default:
mainClass = 'dev.openfga.sdk.example.Example1'
}
}

repositories {
mavenCentral()
}

ext {
jacksonVersion = "2.16.0"
}

dependencies {
implementation("dev.openfga:openfga-sdk:0.3.+")

// Serialization
implementation("com.fasterxml.jackson.core:jackson-core:$jacksonVersion")
implementation("com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion")
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion")
implementation("org.openapitools:jackson-databind-nullable:0.2.+")

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

// Use spotless plugin to automatically format code, remove unused import, etc
// To apply changes directly to the file, run `gradlew spotlessApply`
// Ref: https://github.com/diffplug/spotless/tree/main/plugin-gradle
spotless {
// comment out below to run spotless as part of the `check` task
enforceCheck false
format 'misc', {
// define the files (e.g. '*.gradle', '*.md') to apply `misc` to
target '.gitignore'
// define the steps to apply to those files
trimTrailingWhitespace()
indentWithSpaces() // Takes an integer argument if you don't like 4
endWithNewline()
}
java {
palantirJavaFormat()
removeUnusedImports()
importOrder()
}
}

// Use spotless plugin to automatically format code, remove unused import, etc
// To apply changes directly to the file, run `gradlew spotlessApply`
// Ref: https://github.com/diffplug/spotless/tree/main/plugin-gradle
tasks.register('fmt') {
dependsOn 'spotlessApply'
}

compileKotlin {
kotlinOptions {
jvmTarget = "17"
}
}

compileTestKotlin {
kotlinOptions {
jvmTarget = "17"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language=java
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit be91b94

Please sign in to comment.