Skip to content

Commit

Permalink
Add tests for #227 and #244
Browse files Browse the repository at this point in the history
Issue: #227
Issue: #244
  • Loading branch information
muehmar committed Jan 30, 2024
1 parent b053def commit b34d5cd
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 23 deletions.
2 changes: 1 addition & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ openApiGenerator {
inputSpec = "$projectDir/src/main/resources/openapi-oneof-enum-discriminator.yml"
packageName = "com.github.muehmar.gradle.openapi.oneofenumdiscriminator"
}
['71', '182', '185', '190', '191', '192', '193', '195', '209', '234', '238'].forEach {issueNumber -> {
['71', '182', '185', '190', '191', '192', '193', '195', '209', '227', '234', '238', '244'].forEach {issueNumber -> {
"${issueNumber}" {
inputSpec = "$projectDir/src/main/resources/issues/openapi-issue-${issueNumber}.yml"
packageName = "com.github.muehmar.gradle.openapi.issue${issueNumber}"
Expand Down
43 changes: 43 additions & 0 deletions example/src/main/resources/issues/openapi-issue-227.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
openapi: "3.0.0"
info: { }

paths: { }

components:
schemas:
Patient:
allOf:
- $ref: "#/components/schemas/Person"
- $ref: "#/components/schemas/Employee"
required:
- patientId
properties:
patientId:
type: string

Person:
required:
- firstName
- lastName
- birthDate
properties:
firstName:
type: string
lastName:
type: string
birthDate:
type: string
format: date

Employee:
required:
- employeeId
- firstName
- lastName
properties:
employeeId:
type: string
firstName:
type: string
lastName:
type: string
45 changes: 45 additions & 0 deletions example/src/main/resources/issues/openapi-issue-244.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
openapi: "3.0.0"
info: { }

paths: { }

components:
schemas:
Patient:
allOf:
- $ref: "#/components/schemas/Person"
required:
- patientId
- birthDate
properties:
patientId:
type: string
birthDate:
type: string
format: date

Person:
allOf:
- $ref: "#/components/schemas/Addressable"
required:
- firstName
- lastName
- birthDate
properties:
firstName:
type: string
lastName:
type: string
birthDate:
type: string
format: date

Addressable:
required:
- street
- city
properties:
street:
type: string
city:
type: string
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package com.github.muehmar.gradle.openapi.issue182;

import static com.github.muehmar.gradle.openapi.issue182.UserDataDto.userDataDtoBuilder;
import static com.github.muehmar.gradle.openapi.issue182.UserDto.userDtoBuilder;
import static com.github.muehmar.gradle.openapi.issue182.UserNameDto.userNameDtoBuilder;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;

public class Issue182Test {
@Test
void userDtoBuilder_when_used_thenCompilesAndCorrectAllOfStages() {
void userDtoBuilder_when_onlyPropertyStagesUsed_thenCompilesAndCorrectAllOfStages() {
final UserDto userDto =
userDtoBuilder().setFirstname("Dexter").setLastname("Morgan").setId(123).build();

assertNotNull(userDto);
}

@Test
void userDtoBuilder_when_onlyDtoStagesUsed_thenCompilesAndCorrectAllOfStages() {
final UserDto userDto;
userDto =
userDtoBuilder()
.setFirstname("Dexter")
.setLastname("Morgan")
.setUserDataDto(userDataDtoBuilder().build())
.setUserNameDto(
userNameDtoBuilder().setFirstname("Dexter").setLastname("Morgan").build())
.setUserDataDto(UserDataDto.userDataDtoBuilder().build())
.setId(123)
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.github.muehmar.gradle.openapi.issue192;

import static com.github.muehmar.gradle.openapi.util.MethodList.listMethodNames;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;

public class Issue192Test {
Expand All @@ -15,24 +11,14 @@ void rootObject1DtoBuilder_when_getMethodsOfStage_then_noSinglePropertySettersPr

final Class<?> builderStageClass = RootObject1Dto.rootObject1DtoBuilder().getClass();

final Set<String> methodNames =
Arrays.stream(builderStageClass.getDeclaredMethods())
.map(Method::getName)
.collect(Collectors.toSet());

assertEquals(Collections.singleton("setAllOfObjectWithOneOfDto"), methodNames);
assertEquals("setAllOfObjectWithOneOfDto", listMethodNames(builderStageClass));
}

@Test
void rootObject2DtoBuilder_when_getMethodsOfStage_then_noSinglePropertySettersPresent() {

final Class<?> builderStageClass = RootObject2Dto.rootObject2DtoBuilder().getClass();

final Set<String> methodNames =
Arrays.stream(builderStageClass.getDeclaredMethods())
.map(Method::getName)
.collect(Collectors.toSet());

assertEquals(Collections.singleton("setAllOfObjectWithAnyOfDto"), methodNames);
assertEquals("setAllOfObjectWithAnyOfDto", listMethodNames(builderStageClass));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.github.muehmar.gradle.openapi.issue227;

import static com.github.muehmar.gradle.openapi.issue227.PatientDto.fullPatientDtoBuilder;
import static com.github.muehmar.gradle.openapi.issue227.PatientDto.patientDtoBuilder;
import static com.github.muehmar.gradle.openapi.util.MethodList.listMethodNames;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.time.LocalDate;
import org.junit.jupiter.api.Test;

public class Issue227Test {

private static final PersonDto PERSON_DTO =
PersonDto.personDtoBuilder()
.setFirstName("John")
.setLastName("Doe")
.setBirthDate(LocalDate.of(1990, 1, 1))
.andAllOptionals()
.build();

@Test
void fullPatientDtoBuilder_when_propertyStagesUsed_then_noPropertiesTwice() {
final PatientDto patientDto =
fullPatientDtoBuilder()
.setFirstName("John")
.setLastName("Doe")
.setBirthDate(LocalDate.of(1990, 1, 1))
.setEmployeeId("employeeId")
.setPatientId("patientId")
.build();
assertNotNull(patientDto);
}

@Test
void patientDtoBuilder_when_propertyStagesUsed_then_noPropertiesTwice() {
final PatientDto patientDto =
patientDtoBuilder()
.setFirstName("John")
.setLastName("Doe")
.setBirthDate(LocalDate.of(1990, 1, 1))
.setEmployeeId("employeeId")
.setPatientId("patientId")
.andAllOptionals()
.build();
assertNotNull(patientDto);
}

@Test
void fullPatientDtoBuilder_when_propertyStagesUsed_then_noDtoSetterInReturnedStage() {
final Object stage =
fullPatientDtoBuilder()
.setFirstName("John")
.setLastName("Doe")
.setBirthDate(LocalDate.of(1990, 1, 1));

assertEquals("setEmployeeId", listMethodNames(stage.getClass()));
}

@Test
void patientDtoBuilder_when_propertyStagesUsed_then_noDtoSetterInReturnedStage() {
final Object stage =
patientDtoBuilder()
.setFirstName("John")
.setLastName("Doe")
.setBirthDate(LocalDate.of(1990, 1, 1));

assertEquals("setEmployeeId", listMethodNames(stage.getClass()));
}

@Test
void fullPatientDtoBuilder_when_dtoStagesUsed_then_noPropertySetterInReturnedStage() {
final Object stage = fullPatientDtoBuilder().setPersonDto(PERSON_DTO);

assertEquals("setEmployeeDto", listMethodNames(stage.getClass()));
}

@Test
void patientDtoBuilder_when_dtoStagesUsed_then_noPropertySetterInReturnedStage() {
final Object stage = patientDtoBuilder().setPersonDto(PERSON_DTO);

assertEquals("setEmployeeDto", listMethodNames(stage.getClass()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.muehmar.gradle.openapi.issue244;

import static com.github.muehmar.gradle.openapi.issue244.PatientDto.fullPatientDtoBuilder;
import static com.github.muehmar.gradle.openapi.issue244.PatientDto.patientDtoBuilder;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.time.LocalDate;
import org.junit.jupiter.api.Test;

public class Issue244Test {
@Test
void personDtoBuilder_when_used_then_allNestedAllOfPropertiesPresent() {
final PatientDto patientDto =
patientDtoBuilder()
.setFirstName("John")
.setLastName("Doe")
.setBirthDate(LocalDate.of(1990, 1, 1))
.setStreet("Wallstreet")
.setCity("New York")
.setPatientId("patientId")
.andAllOptionals()
.build();

assertNotNull(patientDto);
}

@Test
void fullPatientDtoBuilder_when_used_then_allNestedAllOfPropertiesPresent() {
final PatientDto patientDto =
fullPatientDtoBuilder()
.setFirstName("John")
.setLastName("Doe")
.setBirthDate(LocalDate.of(1990, 1, 1))
.setStreet("Wallstreet")
.setCity("New York")
.setPatientId("patientId")
.build();

assertNotNull(patientDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.muehmar.gradle.openapi.util;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;

public class MethodList {
private MethodList() {}

public static String listMethodNames(Class<?> clazz) {
return Arrays.stream(clazz.getDeclaredMethods())
.map(Method::getName)
.collect(Collectors.joining(", "));
}
}

0 comments on commit b34d5cd

Please sign in to comment.