Skip to content

Commit

Permalink
clean code, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juancgalvis committed Dec 5, 2023
1 parent 98db9ec commit c90d4bd
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 21 deletions.
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,6 @@ dependencies {
implementation 'com.googlecode.lambdaj:lambdaj:2.3.3'
implementation 'com.google.googlejavaformat:google-java-format:1.18.1'

// implementation 'io.openapiprocessor:'
// implementation 'io.openapiprocessor:openapi-processor-spring:2023.4'

api 'commons-io:commons-io:2.12.0'
api gradleApi()
testImplementation 'org.mockito:mockito-core:4.5.1'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package=co.com.bancolombia
systemProp.version=3.6.3-LOCAL
systemProp.version=3.6.3
simulateRest=true
15 changes: 4 additions & 11 deletions src/main/java/co/com/bancolombia/utils/swagger/Swagger.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@
import java.io.File;
import java.util.List;
import java.util.Map;
import lombok.experimental.UtilityClass;
import org.gradle.api.logging.Logger;

@UtilityClass
public class Swagger {

public static void main(String[] args) { // TODO: Remove this method
Map<String, Object> params = Map.of("async", true, "lombok", true, "router", true);
generateEntrypoint(
"co.com.bancolombia",
"swagger3.yaml",
"build/clean/infrastructure/entry-points/reactive-web",
params);
}

public static void fromBuilder(ModuleBuilder builder, String outputDir) {
String swaggerFile = builder.getStringParam("swagger-file");
if (swaggerFile != null) {
Expand All @@ -31,7 +24,7 @@ public static void fromBuilder(ModuleBuilder builder, String outputDir) {
"lombok",
builder.isEnableLombok(),
"router",
builder.getBooleanParam("task-param-router"));
builder.isReactive() && builder.getBooleanParam("task-param-router"));
Logger logger = builder.getProject().getLogger();
generateEntrypoint(
packageName, swaggerFile, builder.getProject().getRootDir() + "/" + outputDir, params)
Expand All @@ -48,7 +41,7 @@ public static List<File> generateEntrypoint(
configurator.addAdditionalProperty("invokerPackage", packageName + ".api");
configurator.addAdditionalProperty("fullController", true);
configurator.addAdditionalProperty("jakarta", true);
configurator.addAdditionalProperty("useBeanValidation", false); // TODO: make it to works
configurator.addAdditionalProperty("useBeanValidation", false);
configurator.setInputSpecURL(swagger);
configurator.setOutputDir(outputDir);
configurator.setApiPackage(packageName + ".api");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected String getTemplateDir() {
@Override
public void processOpts() {
super.processOpts();
apiTestTemplateFiles.clear(); // TODO: implement it
apiTestTemplateFiles.clear();
apiDocTemplateFiles.clear();
apiTemplateFiles.clear();
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
Expand All @@ -68,11 +68,7 @@ public void processOpts() {
additionalProperties.put(
"lambdaScapeVoid",
(Mustache.Lambda)
(fragment, writer) -> {
System.out.println(fragment);
System.out.println(fragment.context());
writer.write(fragment.execute().replaceAll("\\r|\\n", ""));
});
(fragment, writer) -> writer.write(fragment.execute().replaceAll("\\r|\\n", "")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.Test;

public class GenerateEntryPointTaskTest {
public static final String SWAGGER_FILE = "src/test/resources/swagger/pet-store.yaml";
private GenerateEntryPointTask task;

@Before
Expand Down Expand Up @@ -184,6 +185,25 @@ public void generateEntryPointApiRestWithDefaultServer() throws IOException, Cle
.exists());
}

@Test
public void generateEntryPointApiRestWithDefaultServerFromSwaggerFile()
throws IOException, CleanException {
// Arrange
task.setType("RESTMVC");
task.setFromSwagger(SWAGGER_FILE);
// Act
task.execute();
// Assert
assertTrue(
new File(
"build/unitTest/infrastructure/entry-points/api-rest/src/main/java/co/com/bancolombia/api/PetApiController.java")
.exists());
assertTrue(
new File(
"build/unitTest/infrastructure/entry-points/api-rest/src/main/java/co/com/bancolombia/api/model/Pet.java")
.exists());
}

@Test
public void generateEntryPointApiRestWithDefaultServerAndSwagger()
throws IOException, CleanException {
Expand Down Expand Up @@ -330,6 +350,27 @@ public void generateEntryPointReactiveWebWithoutRouterFunctions()
.exists());
}

@Test
public void generateEntryPointReactiveWebWithoutRouterFunctionsFromSwagger()
throws IOException, CleanException {
// Arrange
setup(GenerateStructureTask.ProjectType.REACTIVE);
task.setType("WEBFLUX");
task.setFromSwagger(SWAGGER_FILE);
task.setRouter(Constants.BooleanOption.FALSE);
// Act
task.execute();
// Assert
assertTrue(
new File(
"build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/PetApiController.java")
.exists());
assertTrue(
new File(
"build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/model/Pet.java")
.exists());
}

@Test
public void generateEntryPointReactiveWebWithoutRouterFunctionsAndSwagger()
throws IOException, CleanException {
Expand Down Expand Up @@ -392,6 +433,32 @@ public void generateEntryPointReactiveWebWithRouterFunctions()
.exists());
}

@Test
public void generateEntryPointReactiveWebWithRouterFunctionsFromSwagger()
throws IOException, CleanException {
// Arrange
setup(GenerateStructureTask.ProjectType.REACTIVE);
task.setType("WEBFLUX");
task.setFromSwagger(SWAGGER_FILE);
task.setRouter(Constants.BooleanOption.TRUE);

// Act
task.execute();
// Assert
assertTrue(
new File(
"build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/PetApiRouter.java")
.exists());
assertTrue(
new File(
"build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/PetApiHandler.java")
.exists());
assertTrue(
new File(
"build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/model/Pet.java")
.exists());
}

@Test
public void generateEntryPointReactiveWebWithDefaultOptionFunctions()
throws IOException, CleanException {
Expand Down
103 changes: 103 additions & 0 deletions src/test/resources/swagger/pet-store.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
openapi: 3.0.1
info:
title: Swagger Petstore 2.0
description: 'This is a sample server Petstore server. You can find out more about Swagger
at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For
this sample, you can use the api key `special-key` to test the authorization filters.'
termsOfService: http://swagger.io/terms/
contact:
email: [email protected]
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.0
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
servers:
- url: https://petstore.swagger.io/v2
- url: http://petstore.swagger.io/v2
tags:
- name: pet
description: Everything about your Pets
externalDocs:
description: Find out more
url: http://swagger.io
paths:
/pet:
post:
tags:
- pet
summary: Add a new pet to the store
operationId: addPet
requestBody:
description: Pet object that needs to be added to the store
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
schema:
$ref: '#/components/schemas/Pet'
required: true
responses:
201:
description: Pet created
405:
description: Invalid input
content: {}
/pet/{petId}:
get:
tags:
- pet
summary: Find pet by ID
description: Returns a single pet
operationId: getPetById
parameters:
- name: petId
in: path
description: ID of pet to return
required: true
schema:
type: integer
format: int64
responses:
200:
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
400:
description: Invalid ID supplied
content: {}
404:
description: Pet not found
content: {}
components:
schemas:
Pet:
required:
- name
- photoUrls
type: object
properties:
id:
type: integer
format: int64
# example: 1
name:
type: string
# example: doggie
photoUrls:
type: array
items:
type: string
# example: https://some.com/image.png
status:
type: string
description: pet status in the store
enum:
- AVAILABLE
- PENDING
- SOLD

0 comments on commit c90d4bd

Please sign in to comment.