Skip to content

Commit

Permalink
feat: introducing the block provider contract generator library
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu-pousse authored and Mathieu POUSSE committed Nov 22, 2021
0 parents commit 49d2ec3
Show file tree
Hide file tree
Showing 22 changed files with 1,497 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Java CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'adopt'
- name: Build with Maven
run: mvn --batch-mode --update-snapshots --no-transfer-progress clean test
27 changes: 27 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Publish package to the Maven Central Repository
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Maven Central Repository
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'adopt'
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
- id: install-secret-key
name: Install gpg secret key
run: |
cat <(echo -e "${{ secrets.OSSRH_GPG_SECRET_KEY }}") | gpg --batch --import
gpg --list-secret-keys --keyid-format LONG
- name: Publish package
run: mvn --batch-mode --no-transfer-progress "-Dgpg.passphrase=${{ secrets.OSSRH_GPG_SECRET_KEY_PASSPHRASE }}" clean deploy
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target/
/.idea/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Ouest-France/SIPA Tech

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# bp-contract-generator-java

Here is an easy and programmatic way to generate block provider contracts. Contracts are valid by design and will fit
perfectly with the BMS.

This fluid API helps you to declare multiple block types with theirs endpoint, parameters and associated templates.

You simply need to generate the object and let your favorite JSON framework serialize it.

## How to install

```xml

<dependecy>
<groupId>io.github.ouest-france</groupId>
<artifactId>bp-contract-generator</artifactId>
<version>1.0.0</version>
</dependecy>
```

## Demonstration

Here is an example with a block type that takes an input text, sends it to a remote service (for demonstration only),
and outputs the bolded text with different templates.

```java

@RequestMapping("/block-provider")
@RestController
@RequiredArgsConstructor
public class BlockProviderResource {

@GetMapping("/configurations")
public ResponseEntity<List<BlockType>> configurations() {
return ResponseEntity.ok(
BlockProviderGenerator.create()
// the block type
.addType("bold").withVersion(1, 0, 0)

// declare the endpoint url
.withEndpoint("https://localhost/block-configurations", "GET", "pure")

// the input text
.addParameter("text")
.configurableAsString()
.required()
.withTitle("Text")
.withDescription("Text to display as bold")

// send this parameter to the endpoint as a query parameter
.inEndpoint(QUERY, STRING)
.registerParameter()

// first template to output using html tag
.addTemplate("html-tag")
.withSource("<b>{{ text }}<b>")
.registerTemplate()

// second template to output using css style
.addTemplate("html-css")
.withSource("<span style=\"font-weight: bold;\">{{ text }}</span>")
.registerTemplate()

.registerType()
.generate()
);
}

}
```

124 changes: 124 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.ouest-france</groupId>
<artifactId>bp-contract-generator</artifactId>
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>8</maven.compiler.source>
<gson.version>2.8.9</gson.version>
<junit-jupiter-engine.version>5.1.0</junit-jupiter-engine.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter-engine.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<excludePackageNames>sipa.blockprovider.*</excludePackageNames>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
<configuration>
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<scm>
<connection>scm:git:${project.scm.url}</connection>
<developerConnection>scm:git:${project.scm.url}</developerConnection>
<url>git@github.com:ouest-france/bp-contract-generator-java.git</url>
<tag>HEAD</tag>
</scm>

<developers>
<developer>
<id>poussma</id>
<name>Mathieu POOUSSE</name>
</developer>
</developers>

<licenses>
<license>
<name>MIT</name>
<url>https://opensource.org/licenses/MIT</url>
<distribution>repo</distribution>
</license>
</licenses>

<name>bp-contract-generator-java</name>
<url>https://github.com/ouest-france/bp-contract-generator-java</url>
<description>A library to easily generate block provider contracts</description>

<distributionManagement>
<repository>
<id>ossrh</id>
<name>Central Repository OSSRH</name>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

</project>
49 changes: 49 additions & 0 deletions src/main/java/sipa/blockprovider/BlockProviderGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package sipa.blockprovider;

import sipa.blockprovider.domain.BlockType;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* The main class to generate block provider contracts.
*/
public class BlockProviderGenerator {

/**
* Prepare a new block provider builder.
*
* @return see description
*/
public static BlockProviderGenerator create() {
return new BlockProviderGenerator();
}

private BlockProviderGenerator() {

}

private final List<BlockTypeBuilder> types = new ArrayList<>();

/**
* Add a block type to the block provider.
*
* @param name the name of the block type
* @return the builder
*/
public BlockTypeBuilder addType(final String name) {
final BlockTypeBuilder builder = new BlockTypeBuilder(this, name);
this.types.add(builder);
return builder;
}

/**
* Finalize the block provider.
*
* @return a ready to serialize list of declared block types
*/
public List<BlockType> generate() {
return this.types.stream().map(type -> type.blockType).collect(Collectors.toList());
}
}
Loading

0 comments on commit 49d2ec3

Please sign in to comment.