Skip to content

Commit

Permalink
Rename value to items in array pojo
Browse files Browse the repository at this point in the history
Issue: #187
  • Loading branch information
muehmar committed Jan 23, 2024
1 parent 4c401fe commit 7ff7845
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static Generator<JavaArrayPojo, PojoSettings> isValidMethod() {
.methodName("isValid")
.noArguments()
.doesNotThrow()
.content(isValidMethodContent(valueValidationCondition()))
.content(isValidMethodContent(itemsValidationCondition()))
.build();
}

Expand All @@ -61,15 +61,12 @@ private static Generator<JavaArrayPojo, PojoSettings> isValidMethodContent(
};
}

private static Condition valueValidationCondition() {
return Condition.constant("isValueValid()");
private static Condition itemsValidationCondition() {
return (pojo, settings, writer) ->
writer.print("is%sValid()", pojo.getArrayPojoMember().getName().startUpperCase());
}

private interface Condition extends Generator<JavaArrayPojo, PojoSettings> {
static Condition constant(String constant) {
return (p, s, w) -> w.print(constant);
}

@Override
default Condition filter(Predicate<JavaArrayPojo> predicate) {
final Generator<JavaArrayPojo, PojoSettings> self = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static JavaArrayPojo wrap(ArrayPojo arrayPojo, TypeMappings typeMappings)

private static JavaPojoMember createItemTypeMember(
ArrayPojo arrayPojo, JavaPojoName pojoName, JavaArrayType javaArrayType) {
final JavaName name = JavaName.fromString("value");
final JavaName name = JavaName.fromString("items");
return javaPojoMemberBuilder()
.pojoName(pojoName)
.name(name)
Expand All @@ -95,8 +95,7 @@ private static JavaPojoMember createItemTypeMember(
private static JavaArrayType createJavaArrayType(ArrayPojo arrayPojo, TypeMappings typeMappings) {
final ArrayType arrayType =
ArrayType.ofItemType(arrayPojo.getItemType()).withConstraints(arrayPojo.getConstraints());
final JavaArrayType javaArrayType = JavaArrayType.wrap(arrayType, typeMappings);
return javaArrayType;
return JavaArrayType.wrap(arrayType, typeMappings);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import javax.validation.constraints.NotNull;
*/
public class PosologyDto {
@JsonValue
private final List<Double> value;
private final List<Double> items;

@JsonCreator
public PosologyDto(
List<Double> value
List<Double> items
) {
this.value = value;
this.items = items;
}

public static PosologyDto fromItems(List<Double> items) {
Expand All @@ -34,36 +34,36 @@ public class PosologyDto {
* Doses to be taken
*/
@NotNull
public List<Double> getValue() {
return value;
public List<Double> getItems() {
return items;
}

/**
* Doses to be taken
*/
public PosologyDto withValue(List<Double> value) {
return new PosologyDto(value);
public PosologyDto withItems(List<Double> items) {
return new PosologyDto(items);
}

boolean isValid() {
return new Validator().isValid();
}

private class Validator {
private boolean isValueValid() {
if(value != null) {
return value.stream().allMatch(this::isValueValueValid);
private boolean isItemsValid() {
if(items != null) {
return items.stream().allMatch(this::isItemsValueValid);
}

return false;
}

private boolean isValueValueValid(Double valueValue) {
private boolean isItemsValueValid(Double itemsValue) {
return true;
}

private boolean isValid() {
return isValueValid();
return isItemsValid();
}
}

Expand All @@ -72,20 +72,20 @@ public class PosologyDto {
if (this == obj) return true;
if (obj == null || this.getClass() != obj.getClass()) return false;
final PosologyDto other = (PosologyDto) obj;
return Objects.deepEquals(this.value, other.value);
return Objects.deepEquals(this.items, other.items);
}

@Override
public int hashCode() {
return Objects.hash(
value
items
);
}

@Override
public String toString() {
return "PosologyDto{" +
"value=" + value +
"items=" + items +
"}";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import javax.validation.constraints.NotNull;
*/
public class PosologyDto {
@JsonValue
private final List<Double> value;
private final List<Double> items;

@JsonCreator
public PosologyDto(
List<Double> value
List<Double> items
) {
this.value = value;
this.items = items;
}

public static PosologyDto fromItems(List<Double> items) {
Expand All @@ -36,42 +36,42 @@ public class PosologyDto {
* Doses to be taken
*/
@NotNull
public List<Double> getValue() {
return value;
public List<Double> getItems() {
return items;
}

/**
* Doses to be taken
*/
public PosologyDto withValue(List<Double> value) {
return new PosologyDto(value);
public PosologyDto withItems(List<Double> items) {
return new PosologyDto(items);
}

@AssertTrue(message = "value does not contain unique items")
private boolean hasValueUniqueItems() {
return new HashSet<>(value).size() == value.size();
@AssertTrue(message = "items does not contain unique items")
private boolean hasItemsUniqueItems() {
return new HashSet<>(items).size() == items.size();
}

boolean isValid() {
return new Validator().isValid();
}

private class Validator {
private boolean isValueValid() {
if(value != null) {
return hasValueUniqueItems()
&& value.stream().allMatch(this::isValueValueValid);
private boolean isItemsValid() {
if(items != null) {
return hasItemsUniqueItems()
&& items.stream().allMatch(this::isItemsValueValid);
}

return false;
}

private boolean isValueValueValid(Double valueValue) {
private boolean isItemsValueValid(Double itemsValue) {
return true;
}

private boolean isValid() {
return isValueValid();
return isItemsValid();
}
}

Expand All @@ -80,20 +80,20 @@ public class PosologyDto {
if (this == obj) return true;
if (obj == null || this.getClass() != obj.getClass()) return false;
final PosologyDto other = (PosologyDto) obj;
return Objects.deepEquals(this.value, other.value);
return Objects.deepEquals(this.items, other.items);
}

@Override
public int hashCode() {
return Objects.hash(
value
items
);
}

@Override
public String toString() {
return "PosologyDto{" +
"value=" + value +
"items=" + items +
"}";
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
arrayPojo=[
private class Validator {
private boolean isValueValid() {
if(value != null) {
return value.stream().allMatch(this::isValueValueValid);
private boolean isItemsValid() {
if(items != null) {
return items.stream().allMatch(this::isItemsValueValid);
}

return false;
}

private boolean isValueValueValid(Double valueValue) {
private boolean isItemsValueValid(Double itemsValue) {
return true;
}

private boolean isValid() {
return isValueValid();
return isItemsValid();
}
}
]


arrayPojoWithUniqueItems=[
private class Validator {
private boolean isValueValid() {
if(value != null) {
return hasValueUniqueItems()
&& value.stream().allMatch(this::isValueValueValid);
private boolean isItemsValid() {
if(items != null) {
return hasItemsUniqueItems()
&& items.stream().allMatch(this::isItemsValueValid);
}

return false;
}

private boolean isValueValueValid(Double valueValue) {
private boolean isItemsValueValid(Double itemsValue) {
return true;
}

private boolean isValid() {
return isValueValid();
return isItemsValid();
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
import static com.github.muehmar.gradle.openapi.generator.java.generator.pojo.MemberGenerator.memberGenerator;
import static com.github.muehmar.gradle.openapi.generator.settings.TestPojoSettings.defaultTestSettings;
import static io.github.muehmar.codegenerator.writer.Writer.javaWriter;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import au.com.origin.snapshots.Expect;
import au.com.origin.snapshots.annotations.SnapshotName;
import com.github.muehmar.gradle.openapi.generator.java.generator.pojo.MemberGenerator.MemberContent;
import com.github.muehmar.gradle.openapi.generator.java.model.pojo.JavaPojos;
import com.github.muehmar.gradle.openapi.generator.java.ref.JacksonRefs;
import com.github.muehmar.gradle.openapi.generator.java.ref.JavaRefs;
import com.github.muehmar.gradle.openapi.generator.settings.PojoSettings;
import com.github.muehmar.gradle.openapi.snapshot.SnapshotTest;
import com.github.muehmar.gradle.openapi.snapshot.SnapshotUtil;
import io.github.muehmar.codegenerator.Generator;
import io.github.muehmar.codegenerator.writer.Writer;
import org.junit.jupiter.api.Test;
Expand All @@ -33,10 +30,7 @@ void generate_when_samplePojo_then_correctOutputAndRef() {
defaultTestSettings(),
javaWriter());

assertTrue(writer.getRefs().exists(JavaRefs.JAVA_UTIL_MAP::equals));

final String output = writer.asString();
expect.toMatchSnapshot(output);
expect.toMatchSnapshot(SnapshotUtil.writerSnapshot(writer));
}

@Test
Expand All @@ -50,22 +44,17 @@ void generate_when_illegalIdentifierPojo_then_matchSnapshot() {
defaultTestSettings(),
javaWriter());

assertTrue(writer.getRefs().exists(JavaRefs.JAVA_UTIL_MAP::equals));

final String output = writer.asString();
expect.toMatchSnapshot(output);
expect.toMatchSnapshot(SnapshotUtil.writerSnapshot(writer));
}

@Test
@SnapshotName("arrayPojo")
void generate_when_arrayPojo_then_correctOutputAndRef() {
final Generator<MemberContent, PojoSettings> gen = memberGenerator();

final Writer writer =
gen.generate(JavaPojos.arrayPojo().getMemberContent(), defaultTestSettings(), javaWriter());

assertTrue(writer.getRefs().exists(JacksonRefs.JSON_VALUE::equals));

final String output = writer.asString();
assertEquals("@JsonValue\n" + "private final List<Double> value;", output);
expect.toMatchSnapshot(SnapshotUtil.writerSnapshot(writer));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
allNecessityAndNullabilityVariants=[
java.util.Map

private final String requiredStringVal;
private final String requiredNullableStringVal;
private final boolean isRequiredNullableStringValPresent;
Expand All @@ -10,7 +12,18 @@ private final Map<String, Object> additionalProperties;
]


arrayPojo=[
com.fasterxml.jackson.annotation.JsonValue
java.util.List

@JsonValue
private final List<Double> items;
]


illegalIdentifierPojo=[
java.util.Map

private final String switch_;
private final boolean isSwitchNull;
private final String point_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || this.getClass() != obj.getClass()) return false;
final PosologyDto other = (PosologyDto) obj;
return Objects.deepEquals(this.value, other.value);
return Objects.deepEquals(this.items, other.items);
}
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ java.util.Objects
@Override
public int hashCode() {
return Objects.hash(
value
items
);
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
arrayPojo=[
@JsonCreator
public PosologyDto(
List<Double> value
List<Double> items
) {
this.value = value;
this.items = items;
}
]

Expand Down
Loading

0 comments on commit 7ff7845

Please sign in to comment.