Skip to content

Commit a3b67b5

Browse files
authored
Code coverage for MappingConfig (#17)
* Introduce model annotations (for Lombok) #10 * Unit tests for MappingConfig * Remove unused import in MappingConfig * Remove code related to modelAnnotations
1 parent 8e332ed commit a3b67b5

File tree

5 files changed

+252
-28
lines changed

5 files changed

+252
-28
lines changed

src/main/java/com/kobylynskyi/graphql/codegen/mapper/InputDefinitionToDataModelMapper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public static Map<String, Object> map(MappingConfig mappingConfig, InputObjectTy
3232
dataModel.put(FIELDS, InputValueDefinitionToParameterMapper.map(mappingConfig, typeDefinition.getInputValueDefinitions(), typeDefinition.getName()));
3333
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.getGenerateEqualsAndHashCode());
3434
dataModel.put(TO_STRING, mappingConfig.getGenerateToString());
35-
3635
return dataModel;
3736
}
3837

src/main/java/com/kobylynskyi/graphql/codegen/mapper/TypeDefinitionToDataModelMapper.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ public static Map<String, Object> map(MappingConfig mappingConfig, ObjectTypeDef
3333
dataModel.put(PACKAGE, packageName);
3434
dataModel.put(IMPORTS, MapperUtils.getImports(mappingConfig, packageName));
3535
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, typeDefinition));
36-
Set<String> allInterfaces = new LinkedHashSet<>();
37-
allInterfaces.addAll(MapperUtils.getUnionsHavingType(mappingConfig, typeDefinition, document));
36+
Set<String> allInterfaces = new LinkedHashSet<>(
37+
MapperUtils.getUnionsHavingType(mappingConfig, typeDefinition, document));
3838
typeDefinition.getImplements().stream()
3939
.map(anImplement -> GraphqlTypeToJavaTypeMapper.getJavaType(mappingConfig, anImplement))
4040
.forEach(allInterfaces::add);
4141
dataModel.put(IMPLEMENTS, allInterfaces);
4242

43-
Set<ParameterDefinition> allParameters = new LinkedHashSet<>();
4443
// Merge attributes from the type and attributes from the interface
45-
allParameters.addAll(FieldDefinitionToParameterMapper.map(mappingConfig, typeDefinition.getFieldDefinitions(), typeDefinition.getName()));
44+
Set<ParameterDefinition> allParameters = new LinkedHashSet<>(FieldDefinitionToParameterMapper
45+
.map(mappingConfig, typeDefinition.getFieldDefinitions(), typeDefinition.getName()));
4646
List<InterfaceTypeDefinition> interfaces = getInterfacesOfType(mappingConfig, typeDefinition, document);
4747
interfaces.stream()
4848
.map(i -> FieldDefinitionToParameterMapper.map(mappingConfig, i.getFieldDefinitions(), i.getName()))
@@ -51,7 +51,6 @@ public static Map<String, Object> map(MappingConfig mappingConfig, ObjectTypeDef
5151
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.getGenerateEqualsAndHashCode());
5252
dataModel.put(TO_STRING, mappingConfig.getGenerateToString());
5353

54-
5554
return dataModel;
5655
}
5756

src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.kobylynskyi.graphql.codegen.model;
22

33
import java.util.HashMap;
4+
import java.util.HashSet;
45
import java.util.Map;
6+
import java.util.Set;
57

68
import lombok.Data;
79

@@ -53,30 +55,29 @@ public void putCustomTypeMappingIfAbsent(String from, String to) {
5355
}
5456
}
5557

56-
5758
@Override
5859
public void combine(MappingConfig source) {
59-
if (source != null) {
60-
if (this.customTypesMapping != null && source.customTypesMapping != null) {
61-
this.customTypesMapping.putAll(source.customTypesMapping);
62-
} else if (this.customTypesMapping == null && source.customTypesMapping != null) {
63-
this.customTypesMapping = source.customTypesMapping;
64-
}
65-
if (this.customAnnotationsMapping != null && source.customAnnotationsMapping != null) {
66-
this.customAnnotationsMapping.putAll(source.customAnnotationsMapping);
67-
} else if (this.customAnnotationsMapping == null && source.customAnnotationsMapping != null) {
68-
this.customAnnotationsMapping = source.customAnnotationsMapping;
69-
}
70-
this.generateApis = source.generateApis != null ? source.generateApis : this.generateApis;
71-
this.packageName = source.packageName != null ? source.packageName : this.packageName;
72-
this.apiPackageName = source.apiPackageName != null ? source.apiPackageName : this.apiPackageName;
73-
this.modelPackageName = source.modelPackageName != null ? source.modelPackageName : this.modelPackageName;
74-
this.modelNamePrefix = source.modelNamePrefix != null ? source.modelNamePrefix : this.modelNamePrefix;
75-
this.modelNameSuffix = source.modelNameSuffix != null ? source.modelNameSuffix : this.modelNameSuffix;
76-
this.modelValidationAnnotation = source.modelValidationAnnotation != null ? source.modelValidationAnnotation : this.modelValidationAnnotation;
77-
this.generateEqualsAndHashCode = source.generateEqualsAndHashCode != null ? source.generateEqualsAndHashCode : this.generateEqualsAndHashCode;
78-
this.generateToString = source.generateToString != null ? source.generateToString : this.generateToString;
79-
60+
if (source == null) {
61+
return;
62+
}
63+
if (this.customTypesMapping != null && source.customTypesMapping != null) {
64+
this.customTypesMapping.putAll(source.customTypesMapping);
65+
} else if (this.customTypesMapping == null) {
66+
this.customTypesMapping = source.customTypesMapping;
67+
}
68+
if (this.customAnnotationsMapping != null && source.customAnnotationsMapping != null) {
69+
this.customAnnotationsMapping.putAll(source.customAnnotationsMapping);
70+
} else if (this.customAnnotationsMapping == null) {
71+
this.customAnnotationsMapping = source.customAnnotationsMapping;
8072
}
73+
this.generateApis = source.generateApis != null ? source.generateApis : this.generateApis;
74+
this.packageName = source.packageName != null ? source.packageName : this.packageName;
75+
this.apiPackageName = source.apiPackageName != null ? source.apiPackageName : this.apiPackageName;
76+
this.modelPackageName = source.modelPackageName != null ? source.modelPackageName : this.modelPackageName;
77+
this.modelNamePrefix = source.modelNamePrefix != null ? source.modelNamePrefix : this.modelNamePrefix;
78+
this.modelNameSuffix = source.modelNameSuffix != null ? source.modelNameSuffix : this.modelNameSuffix;
79+
this.modelValidationAnnotation = source.modelValidationAnnotation != null ? source.modelValidationAnnotation : this.modelValidationAnnotation;
80+
this.generateEqualsAndHashCode = source.generateEqualsAndHashCode != null ? source.generateEqualsAndHashCode : this.generateEqualsAndHashCode;
81+
this.generateToString = source.generateToString != null ? source.generateToString : this.generateToString;
8182
}
8283
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.kobylynskyi.graphql.codegen.model;
2+
3+
import static java.util.Arrays.asList;
4+
import static java.util.Collections.singletonMap;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
import java.util.AbstractMap;
10+
import java.util.Arrays;
11+
import java.util.HashMap;
12+
import java.util.HashSet;
13+
import java.util.Map;
14+
import java.util.stream.Collectors;
15+
16+
import org.junit.jupiter.api.Test;
17+
18+
@SuppressWarnings("unchecked")
19+
class MappingConfigTest {
20+
21+
@Test
22+
public void combineDefaultWithNull() {
23+
MappingConfig mappingConfig = buildEmptyMappingConfig();
24+
mappingConfig.combine(null);
25+
26+
assertEquals(buildEmptyMappingConfig(), mappingConfig);
27+
}
28+
29+
@Test
30+
void combineDefaultWithDefault() {
31+
MappingConfig mappingConfig = buildEmptyMappingConfig();
32+
mappingConfig.combine(buildEmptyMappingConfig());
33+
34+
assertEquals(buildEmptyMappingConfig(), mappingConfig);
35+
}
36+
37+
@Test
38+
void combineDefaultWithCustom() {
39+
MappingConfig mappingConfig = buildEmptyMappingConfig();
40+
mappingConfig.combine(buildMappingConfig());
41+
42+
assertEquals(singletonMap("1", "2"), mappingConfig.getCustomTypesMapping());
43+
assertEquals(singletonMap("3", "4"), mappingConfig.getCustomAnnotationsMapping());
44+
assertEquals("ApiPackageName", mappingConfig.getApiPackageName());
45+
assertTrue(mappingConfig.getGenerateApis());
46+
assertTrue(mappingConfig.getGenerateEqualsAndHashCode());
47+
assertTrue(mappingConfig.getGenerateToString());
48+
assertEquals("ModelNamePrefix", mappingConfig.getModelNamePrefix());
49+
assertEquals("ModelNameSuffix", mappingConfig.getModelNameSuffix());
50+
assertEquals("ModelPackageName", mappingConfig.getModelPackageName());
51+
assertEquals("ModelValidationAnnotation", mappingConfig.getModelValidationAnnotation());
52+
assertEquals("PackageName", mappingConfig.getPackageName());
53+
}
54+
55+
@Test
56+
void combineCustomWithDefault() {
57+
MappingConfig mappingConfig = buildMappingConfig();
58+
mappingConfig.combine(buildEmptyMappingConfig());
59+
60+
assertEquals(singletonMap("1", "2"), mappingConfig.getCustomTypesMapping());
61+
assertEquals(singletonMap("3", "4"), mappingConfig.getCustomAnnotationsMapping());
62+
assertEquals("ApiPackageName", mappingConfig.getApiPackageName());
63+
assertTrue(mappingConfig.getGenerateApis());
64+
assertTrue(mappingConfig.getGenerateEqualsAndHashCode());
65+
assertTrue(mappingConfig.getGenerateToString());
66+
assertEquals("ModelNamePrefix", mappingConfig.getModelNamePrefix());
67+
assertEquals("ModelNameSuffix", mappingConfig.getModelNameSuffix());
68+
assertEquals("ModelPackageName", mappingConfig.getModelPackageName());
69+
assertEquals("ModelValidationAnnotation", mappingConfig.getModelValidationAnnotation());
70+
assertEquals("PackageName", mappingConfig.getPackageName());
71+
}
72+
73+
@Test
74+
void combineCustomWithCustom() {
75+
MappingConfig mappingConfig = buildMappingConfig();
76+
mappingConfig.combine(buildMappingConfig2());
77+
78+
assertEquals(hashMap(new HashMap.SimpleEntry<>("1", "2"), new HashMap.SimpleEntry<>("11", "22")),
79+
mappingConfig.getCustomTypesMapping());
80+
assertEquals(hashMap(new HashMap.SimpleEntry<>("3", "4"), new HashMap.SimpleEntry<>("33", "44")),
81+
mappingConfig.getCustomAnnotationsMapping());
82+
assertEquals("ApiPackageName2", mappingConfig.getApiPackageName());
83+
assertFalse(mappingConfig.getGenerateApis());
84+
assertFalse(mappingConfig.getGenerateEqualsAndHashCode());
85+
assertFalse(mappingConfig.getGenerateToString());
86+
assertEquals("ModelNamePrefix2", mappingConfig.getModelNamePrefix());
87+
assertEquals("ModelNameSuffix2", mappingConfig.getModelNameSuffix());
88+
assertEquals("ModelPackageName2", mappingConfig.getModelPackageName());
89+
assertEquals("ModelValidationAnnotation2", mappingConfig.getModelValidationAnnotation());
90+
assertEquals("PackageName2", mappingConfig.getPackageName());
91+
}
92+
93+
private static Map<String, String> hashMap(AbstractMap.SimpleEntry<String, String>... entries) {
94+
return Arrays.stream(entries).collect(
95+
Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue, (a, b) -> b));
96+
}
97+
98+
private static MappingConfig buildMappingConfig() {
99+
MappingConfig config = new MappingConfig();
100+
config.setCustomTypesMapping(new HashMap<>(singletonMap("1", "2")));
101+
config.setCustomAnnotationsMapping(new HashMap<>(singletonMap("3", "4")));
102+
config.setApiPackageName("ApiPackageName");
103+
config.setGenerateApis(true);
104+
config.setGenerateEqualsAndHashCode(true);
105+
config.setGenerateToString(true);
106+
config.setModelNamePrefix("ModelNamePrefix");
107+
config.setModelNameSuffix("ModelNameSuffix");
108+
config.setModelPackageName("ModelPackageName");
109+
config.setModelValidationAnnotation("ModelValidationAnnotation");
110+
config.setPackageName("PackageName");
111+
return config;
112+
}
113+
114+
private static MappingConfig buildMappingConfig2() {
115+
MappingConfig config = new MappingConfig();
116+
config.setCustomTypesMapping(new HashMap<>(singletonMap("11", "22")));
117+
config.setCustomAnnotationsMapping(new HashMap<>(singletonMap("33", "44")));
118+
config.setApiPackageName("ApiPackageName2");
119+
config.setGenerateApis(false);
120+
config.setGenerateEqualsAndHashCode(false);
121+
config.setGenerateToString(false);
122+
config.setModelNamePrefix("ModelNamePrefix2");
123+
config.setModelNameSuffix("ModelNameSuffix2");
124+
config.setModelPackageName("ModelPackageName2");
125+
config.setModelValidationAnnotation("ModelValidationAnnotation2");
126+
config.setPackageName("PackageName2");
127+
return config;
128+
}
129+
130+
private static MappingConfig buildEmptyMappingConfig() {
131+
MappingConfig mappingConfig = new MappingConfig();
132+
mappingConfig.setCustomTypesMapping(null);
133+
mappingConfig.setCustomAnnotationsMapping(null);
134+
return mappingConfig;
135+
}
136+
137+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
import java.util.*;
4+
5+
@lombok.Builder
6+
@lombok.Data
7+
public class Event {
8+
9+
private String id;
10+
private String categoryId;
11+
private Collection<EventProperty> properties;
12+
private EventStatus status;
13+
private String createdBy;
14+
private String createdDateTime;
15+
private Boolean active;
16+
private Integer rating;
17+
18+
public Event() {
19+
}
20+
21+
public Event(String id, String categoryId, Collection<EventProperty> properties, EventStatus status, String createdBy, String createdDateTime, Boolean active, Integer rating) {
22+
this.id = id;
23+
this.categoryId = categoryId;
24+
this.properties = properties;
25+
this.status = status;
26+
this.createdBy = createdBy;
27+
this.createdDateTime = createdDateTime;
28+
this.active = active;
29+
this.rating = rating;
30+
}
31+
32+
public String getId() {
33+
return id;
34+
}
35+
public void setId(String id) {
36+
this.id = id;
37+
}
38+
39+
public String getCategoryId() {
40+
return categoryId;
41+
}
42+
public void setCategoryId(String categoryId) {
43+
this.categoryId = categoryId;
44+
}
45+
46+
public Collection<EventProperty> getProperties() {
47+
return properties;
48+
}
49+
public void setProperties(Collection<EventProperty> properties) {
50+
this.properties = properties;
51+
}
52+
53+
public EventStatus getStatus() {
54+
return status;
55+
}
56+
public void setStatus(EventStatus status) {
57+
this.status = status;
58+
}
59+
60+
public String getCreatedBy() {
61+
return createdBy;
62+
}
63+
public void setCreatedBy(String createdBy) {
64+
this.createdBy = createdBy;
65+
}
66+
67+
public String getCreatedDateTime() {
68+
return createdDateTime;
69+
}
70+
public void setCreatedDateTime(String createdDateTime) {
71+
this.createdDateTime = createdDateTime;
72+
}
73+
74+
public Boolean getActive() {
75+
return active;
76+
}
77+
public void setActive(Boolean active) {
78+
this.active = active;
79+
}
80+
81+
public Integer getRating() {
82+
return rating;
83+
}
84+
public void setRating(Integer rating) {
85+
this.rating = rating;
86+
}
87+
88+
}

0 commit comments

Comments
 (0)