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
+ }
0 commit comments