Fix mockito deprecation warnings
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / test / java / org / opendaylight / mdsal / binding / javav2 / generator / impl / AugmentToGenTypeTest.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.javav2.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.lang.reflect.Constructor;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.Set;
26 import org.junit.Test;
27 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
28 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
29 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
30 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
31 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
32 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
33 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
34 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
42 import org.opendaylight.yangtools.yang.model.api.Module;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.UsesNode;
48 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
49
50 public class AugmentToGenTypeTest {
51
52     @Test(expected = UnsupportedOperationException.class)
53     public void constructorTest() throws Throwable {
54         final Constructor<AugmentToGenType> constructor = AugmentToGenType.class.getDeclaredConstructor();
55         constructor.setAccessible(true);
56         final Object[] objs = {};
57         try {
58             constructor.newInstance(objs);
59         } catch (final Exception e) {
60             throw e.getCause();
61         }
62     }
63
64     @Test
65     public void generateNullModuleTest() {
66         final SchemaContext context = null;
67         final TypeProvider typeProvider = null;
68         final Map<Module, ModuleContext> genCtx = new HashMap<>();
69         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
70         final Module m = null;
71
72         try {
73             AugmentToGenType.generate(m, context, typeProvider, genCtx, genTypeBuilders, false);
74             fail();
75         } catch (final IllegalArgumentException e) {
76             assertEquals("Module reference cannot be NULL.", e.getMessage());
77         }
78     }
79
80     @Test
81     public void generateNullModuleNameTest() {
82         final SchemaContext context = null;
83         final TypeProvider typeProvider = null;
84         final Map<Module, ModuleContext> genCtx = new HashMap<>();
85         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
86         final Module m = mock(Module.class);
87         when(m.getName()).thenReturn(null);
88
89         try {
90             AugmentToGenType.generate(m, context, typeProvider, genCtx, genTypeBuilders, false);
91             fail();
92         } catch (final IllegalArgumentException e) {
93             assertEquals("Module name cannot be NULL.", e.getMessage());
94         }
95     }
96
97     @Test
98     public void generateNullModuleAugmentationsTest() {
99         final SchemaContext context = null;
100         final TypeProvider typeProvider = null;
101         final Map<Module, ModuleContext> genCtx = new HashMap<>();
102         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
103         final Module m = mock(Module.class);
104         when(m.getName()).thenReturn("name");
105         when(m.getAugmentations()).thenReturn(null);
106
107         try {
108             AugmentToGenType.generate(m, context, typeProvider, genCtx, genTypeBuilders, false);
109             fail();
110         } catch (final IllegalStateException e) {
111             assertEquals("Augmentations Set cannot be NULL.", e.getMessage());
112         }
113     }
114
115     @Test
116     public void generateWithoutAugmentationsTest() {
117         final SchemaContext context = YangParserTestUtils.parseYangResource("/generator/test.yang");
118         final TypeProvider typeProvider = new TypeProviderImpl(context);
119         final Map<Module, ModuleContext> genCtx = new HashMap<>();
120         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
121         final Map<Module, ModuleContext> invoke = AugmentToGenType.generate(context.getModules().iterator().next(),
122             context, typeProvider, genCtx, genTypeBuilders, false);
123         assertNotNull(invoke);
124     }
125
126     @Test
127     public void generateWithAugmentationsTest() {
128         final SchemaContext context = YangParserTestUtils.parseYangResource("/generator/test-augment.yang");
129         final TypeProvider typeProvider = new TypeProviderImpl(context);
130         final Map<Module, ModuleContext> genCtx = mock(Map.class);
131         final Collection<ModuleContext> moduleContexts = new ArrayList<>();
132         final ModuleContext moduleContext = new ModuleContext();
133         moduleContexts.add(moduleContext);
134         final QName create = QName.create("urn:test:simple:test", "2017-02-06", "my-cont");
135         final SchemaNode schemaNode = mock(SchemaNode.class);
136         when(schemaNode.getPath()).thenReturn(SchemaPath.create(true, create));
137         moduleContext.addChildNodeType(schemaNode, new GeneratedTypeBuilderImpl("test", "Test", moduleContext));
138         when(genCtx.values()).thenReturn(moduleContexts);
139         when(genCtx.get(context.getModules().iterator().next())).thenReturn(moduleContext);
140         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
141
142         final Map<Module, ModuleContext> invoke = AugmentToGenType.generate(context.getModules().iterator().next(),
143             context, typeProvider, genCtx, genTypeBuilders, false);
144         assertNotNull(invoke);
145     }
146
147     @Test
148     public void resolveAugmentationsNullModuleTest() {
149         try {
150             AugmentToGenType.resolveAugmentations(null, null);
151             fail();
152         } catch (final IllegalArgumentException e) {
153             assertEquals("Module reference cannot be NULL.", e.getMessage());
154         }
155     }
156
157     @Test
158     public void resolveAugmentationsNullAugmentationsTest() {
159         final Module m = mock(Module.class);
160         when(m.getAugmentations()).thenReturn(null);
161         final SchemaContext schemaContext = mock(SchemaContext.class);
162
163         try {
164             AugmentToGenType.resolveAugmentations(m, schemaContext);
165             fail();
166         } catch (final IllegalStateException e) {
167             assertEquals("Augmentations Set cannot be NULL.", e.getMessage());
168         }
169     }
170
171     @Test
172     public void resolveAugmentationsTest() {
173         final Module m = mock(Module.class);
174         final Module m2 = mock(Module.class);
175         final SchemaContext schemaContext = mock(SchemaContext.class);
176
177         final Set<AugmentationSchemaNode> augmentations = new HashSet<>();
178
179         final QName q1 = QName.create("q1", "2017-04-04", "q1");
180         final QName q2 = QName.create("q2", "2017-04-04", "q2");
181         final QName q3 = QName.create("q3", "2017-04-04", "q3");
182         final QName q4 = QName.create("q4", "2017-04-04", "q4");
183         final QName q5 = QName.create("q5", "2017-04-04", "q5");
184
185         final AugmentationSchemaNode AugmentationSchemaNode1 = mock(AugmentationSchemaNode.class);
186         when(AugmentationSchemaNode1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
187         final AugmentationSchemaNode AugmentationSchemaNode2 = mock(AugmentationSchemaNode.class);
188         when(AugmentationSchemaNode2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
189         augmentations.add(AugmentationSchemaNode1);
190         augmentations.add(AugmentationSchemaNode2);
191
192         when(m.getAugmentations()).thenReturn(augmentations);
193         when(schemaContext.findModule(q1.getModule())).thenReturn(Optional.of(m2));
194         when(schemaContext.findModule(q3.getModule())).thenReturn(Optional.of(m2));
195
196         final List<AugmentationSchemaNode> result = AugmentToGenType.resolveAugmentations(m, schemaContext);
197         assertNotNull(result);
198         assertTrue(!result.isEmpty());
199         assertEquals(result.get(0), AugmentationSchemaNode1);
200         assertEquals(result.get(1), AugmentationSchemaNode2);
201     }
202
203     @Test
204     public void augmentationToGenTypesNullPckgNameTest() {
205         final String augmPackName = null;
206         final Map.Entry<SchemaPath, List<AugmentationSchemaNode>> schemaPathAugmentListEntry = null;
207         final SchemaContext context = null;
208         final TypeProvider typeProvider = null;
209         final Map<Module, ModuleContext> genCtx = new HashMap<>();
210         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
211         final Module m = null;
212
213         try {
214             AugmentToGenType.augmentationToGenTypes(augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx,
215                 genTypeBuilders, typeProvider);
216             fail();
217         } catch (final IllegalArgumentException e) {
218             assertEquals("Package Name cannot be NULL.", e.getMessage());
219         }
220     }
221
222     @Test
223     public void augmentationToGenTypesNullAugSchemaTest() {
224         final String augmPackName = "pckg.name";
225         final Map.Entry<SchemaPath, List<AugmentationSchemaNode>> schemaPathAugmentListEntry = null;
226         final SchemaContext context = null;
227         final TypeProvider typeProvider = null;
228         final Map<Module, ModuleContext> genCtx = new HashMap<>();
229         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
230         final Module m = null;
231
232         try {
233             AugmentToGenType.augmentationToGenTypes(augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx,
234                 genTypeBuilders, typeProvider);
235             fail();
236         } catch (final IllegalArgumentException e) {
237             assertEquals("Augmentation List Entry cannot be NULL.", e.getMessage());
238         }
239     }
240
241     @Test
242     public void augmentationToGenTypesNullAugSchemaTargetPathTest() {
243         final String augmPackName = "pckg.name";
244         final AugmentationSchemaNode augmSchema = mock(AugmentationSchemaNode.class);
245         when(augmSchema.getTargetPath()).thenReturn(null);
246
247         final List<AugmentationSchemaNode> AugmentationSchemaNodeList = new ArrayList<>();
248         final Map.Entry<SchemaPath, List<AugmentationSchemaNode>> schemaPathAugmentListEntry = mock(Map.Entry.class);
249         when(schemaPathAugmentListEntry.getKey()).thenReturn(null);
250         when(schemaPathAugmentListEntry.getValue()).thenReturn(AugmentationSchemaNodeList);
251
252         final SchemaContext context = null;
253         final TypeProvider typeProvider = null;
254         final Map<Module, ModuleContext> genCtx = new HashMap<>();
255         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
256         final Module m = null;
257
258         try {
259             AugmentToGenType.augmentationToGenTypes(augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx,
260                 genTypeBuilders, typeProvider);
261             fail();
262         } catch (final IllegalStateException e) {
263             assertEquals("Augmentation List Entry does not contain Target Path (Target Path is NULL).", e.getMessage());
264         }
265     }
266
267     @Test
268     public void augmentationToGenTypesNullAugSchemaListTest() {
269         final String augmPackName = "pckg.name";
270         final AugmentationSchemaNode augmSchema = mock(AugmentationSchemaNode.class);
271         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
272         final SchemaPath path = SchemaPath.create(true, qnamePath);
273         when(augmSchema.getTargetPath()).thenReturn(path);
274
275         final List<AugmentationSchemaNode> AugmentationSchemaNodeList = new ArrayList<>();
276         final Map.Entry<SchemaPath, List<AugmentationSchemaNode>> schemaPathAugmentListEntry = mock(Map.Entry.class);
277         when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
278         when(schemaPathAugmentListEntry.getValue()).thenReturn(AugmentationSchemaNodeList);
279
280         final SchemaContext context = null;
281         final TypeProvider typeProvider = null;
282         final Map<Module, ModuleContext> genCtx = new HashMap<>();
283         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
284         final Module m = null;
285
286         try {
287             AugmentToGenType.augmentationToGenTypes(augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx,
288                 genTypeBuilders, typeProvider);
289             fail();
290         } catch (final IllegalStateException e) {
291             assertEquals("Augmentation List cannot be empty.", e.getMessage());
292         }
293     }
294
295     @Test
296     public void augmentationToGenTypesNullAugSchemaTargetNodeTest() {
297         final String augmPackName = "pckg.name";
298
299         final AugmentationSchemaNode augmSchema = mock(AugmentationSchemaNode.class);
300         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
301         final SchemaPath path = SchemaPath.create(true, qnamePath);
302         when(augmSchema.getTargetPath()).thenReturn(path);
303         final Set<UsesNode> uses = new HashSet<>();
304         when(augmSchema.getUses()).thenReturn(uses);
305
306         final List<AugmentationSchemaNode> AugmentationSchemaNodeList = new ArrayList<>();
307         AugmentationSchemaNodeList.add(augmSchema);
308         final Map.Entry<SchemaPath, List<AugmentationSchemaNode>> schemaPathAugmentListEntry = mock(Map.Entry.class);
309         when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
310         when(schemaPathAugmentListEntry.getValue()).thenReturn(AugmentationSchemaNodeList);
311
312         final SchemaContext context = mock(SchemaContext.class);
313         final Module moduleAug = mock(Module.class);
314         final DataSchemaNode schNode = null;
315         when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
316         when(context.findModule(qnamePath.getModule())).thenReturn(Optional.of(moduleAug));
317
318         final TypeProvider typeProvider = null;
319         final Map<Module, ModuleContext> genCtx = new HashMap<>();
320         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
321
322         final Module m = mock(Module.class);
323         when(m.getName()).thenReturn("augm-module");
324         when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
325         when(m.getRevision()).thenReturn(qnamePath.getRevision());
326
327         try {
328             AugmentToGenType.augmentationToGenTypes(augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx,
329                 genTypeBuilders, typeProvider);
330             fail();
331         } catch (final IllegalArgumentException e) {
332             assertEquals("augment target not found: " + path, e.getMessage());
333         }
334     }
335
336     @Test
337     public void augmentationToGenTypesNullAugTargetGTBTest() {
338         final String augmPackName = "pckg.name";
339
340         final AugmentationSchemaNode augmSchema = mock(AugmentationSchemaNode.class);
341         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
342         final SchemaPath path = SchemaPath.create(true, qnamePath);
343         when(augmSchema.getTargetPath()).thenReturn(path);
344         final Set<UsesNode> uses = new HashSet<>();
345         when(augmSchema.getUses()).thenReturn(uses);
346
347         final List<AugmentationSchemaNode> AugmentationSchemaNodeList = new ArrayList<>();
348         AugmentationSchemaNodeList.add(augmSchema);
349         final Map.Entry<SchemaPath, List<AugmentationSchemaNode>> schemaPathAugmentListEntry = mock(Map.Entry.class);
350         when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
351         when(schemaPathAugmentListEntry.getValue()).thenReturn(AugmentationSchemaNodeList);
352
353         final SchemaContext context = mock(SchemaContext.class);
354         final Module moduleAug = mock(Module.class);
355         final DataSchemaNode schNode = mock(DataSchemaNode.class);
356         when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
357         when(context.findModule(qnamePath.getModule())).thenReturn(Optional.of(moduleAug));
358
359         final TypeProvider typeProvider = null;
360         final Map<Module, ModuleContext> genCtx = new HashMap<>();
361         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
362
363         final Module m = mock(Module.class);
364         when(m.getName()).thenReturn("augm-module");
365         when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
366         when(m.getRevision()).thenReturn(qnamePath.getRevision());
367
368         try {
369             AugmentToGenType.augmentationToGenTypes(augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx,
370                 genTypeBuilders, typeProvider);
371             fail();
372         } catch (final NullPointerException e) {
373             assertEquals("Target type not yet generated: " + schNode, e.getMessage());
374         }
375     }
376
377     @Test
378     public void augmentationToGenTypesTargetChoicSchemaNodeTest() {
379         final String augmPackName = "pckg.name";
380
381         final AugmentationSchemaNode augmSchema = mock(AugmentationSchemaNode.class);
382         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
383         final SchemaPath path = SchemaPath.create(true, qnamePath);
384         when(augmSchema.getTargetPath()).thenReturn(path);
385         final Set<UsesNode> uses = new HashSet<>();
386         when(augmSchema.getUses()).thenReturn(uses);
387
388         final List<AugmentationSchemaNode> AugmentationSchemaNodeList = new ArrayList<>();
389         AugmentationSchemaNodeList.add(augmSchema);
390         final Map.Entry<SchemaPath, List<AugmentationSchemaNode>> schemaPathAugmentListEntry = mock(Map.Entry.class);
391         when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
392         when(schemaPathAugmentListEntry.getValue()).thenReturn(AugmentationSchemaNodeList);
393
394         final SchemaContext context = mock(SchemaContext.class);
395         final Module moduleAug = mock(Module.class);
396         final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
397         when(targetSchNode.getPath()).thenReturn(path);
398         when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
399         when(context.findModule(qnamePath.getModule())).thenReturn(Optional.of(moduleAug));
400
401         final TypeProvider typeProvider = null;
402
403         final Map<Module, ModuleContext> genCtx = mock(Map.class);
404         final Collection<ModuleContext> moduleContexts = new ArrayList<>();
405         final ModuleContext mc = new ModuleContext();
406         final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm", mc);
407         mc.addChildNodeType(targetSchNode, gtb);
408         moduleContexts.add(mc);
409         when(genCtx.values()).thenReturn(moduleContexts);
410
411         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
412
413         final Module m = mock(Module.class);
414         when(m.getName()).thenReturn("augm-module");
415         when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
416         when(m.getRevision()).thenReturn(qnamePath.getRevision());
417
418         final Map<Module, ModuleContext> result = AugmentToGenType.augmentationToGenTypes(augmPackName,
419             schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider);
420         assertNotNull(result);
421     }
422
423     @Test
424     public void augmentationToGenTypesTest() {
425         final String augmPackName = "pckg.name";
426
427         final AugmentationSchemaNode augmSchema = mock(AugmentationSchemaNode.class);
428         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
429         final SchemaPath path = SchemaPath.create(true, qnamePath);
430         when(augmSchema.getTargetPath()).thenReturn(path);
431         final Set<UsesNode> uses = new HashSet<>();
432         when(augmSchema.getUses()).thenReturn(uses);
433         final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
434         when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
435
436         final List<AugmentationSchemaNode> AugmentationSchemaNodeList = new ArrayList<>();
437         AugmentationSchemaNodeList.add(augmSchema);
438         final Map.Entry<SchemaPath, List<AugmentationSchemaNode>> schemaPathAugmentListEntry = mock(Map.Entry.class);
439         when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
440         when(schemaPathAugmentListEntry.getValue()).thenReturn(AugmentationSchemaNodeList);
441
442         final SchemaContext context = mock(SchemaContext.class);
443         final Module moduleAug = mock(Module.class);
444         final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
445         when(targetSchNode.getPath()).thenReturn(path);
446         when(targetSchNode.isAddedByUses()).thenReturn(true);
447         when(targetSchNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
448         when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
449         when(context.findModule(qnamePath.getModule())).thenReturn(Optional.of(moduleAug));
450
451         final TypeProvider typeProvider = null;
452
453         final Map<Module, ModuleContext> genCtx = new HashMap<>();
454
455         final Collection<ModuleContext> moduleContexts = new ArrayList<>();
456         final ModuleContext mc = new ModuleContext();
457         final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm", mc);
458         mc.addChildNodeType(targetSchNode, gtb);
459         moduleContexts.add(mc);
460         genCtx.put(moduleAug, mc);
461
462         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
463
464         when(moduleAug.getName()).thenReturn("augm-module");
465         when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
466         when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
467
468         final Map<Module, ModuleContext> result =
469                 AugmentToGenType.augmentationToGenTypes(augmPackName, schemaPathAugmentListEntry, moduleAug, context,
470                     false, genCtx, genTypeBuilders, typeProvider);
471         assertNotNull(result);
472         final ModuleContext moduleContext = result.get(moduleAug);
473         assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
474         assertEquals("pckg.name.data", moduleContext.getAugmentations().get(0).getPackageName());
475         assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
476         assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
477     }
478
479     @Test
480     public void findOriginalTargetFromGroupingNonGroupingTest() {
481         final Module module = mock(Module.class);
482         final QName qnamePath = QName.create("test", "2017-04-04", "test");
483         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
484         final DataSchemaNode schNode = mock(DataSchemaNode.class);
485         when(schNode.getPath()).thenReturn(schemaPath);
486         when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
487
488         final SchemaContext context = mock(SchemaContext.class);
489         when(context.findModule(qnamePath.getModule())).thenReturn(Optional.of(module));
490         final UsesNode usesNode = mock(UsesNode.class);
491         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
492
493         try {
494             AugmentToGenType.findOriginalTargetFromGrouping(context, schemaPath, usesNode);
495             fail();
496         } catch (final IllegalArgumentException e) {
497             assertEquals("Failed to generate code for augment in " + usesNode, e.getMessage());
498         }
499     }
500
501     @Test
502     public void findOriginalTargetFromGroupingAsUsesFailedTest() {
503         final Module module = mock(Module.class);
504         final QName qnamePath = QName.create("test", "2017-04-04", "test");
505         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
506         final DataSchemaNode schNode = mock(DataSchemaNode.class);
507         when(schNode.getPath()).thenReturn(schemaPath);
508         when(schNode.isAddedByUses()).thenReturn(true);
509         final Set<GroupingDefinition> groupings = new HashSet<>();
510         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
511         when(groupingDefinition.getQName()).thenReturn(qnamePath);
512         groupings.add(groupingDefinition);
513         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
514         when(module.getGroupings()).thenReturn(groupings);
515
516         final SchemaContext context = mock(SchemaContext.class);
517         when(context.findModule(qnamePath.getModule())).thenReturn(Optional.of(module));
518         final UsesNode usesNode = mock(UsesNode.class);
519         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
520
521         final Object[] args = {  };
522         try {
523             AugmentToGenType.findOriginalTargetFromGrouping(context, schemaPath, usesNode);
524             fail();
525         } catch (final IllegalStateException e) {
526             assertEquals("Failed to generate code for augment in " + usesNode, e.getMessage());
527         }
528     }
529
530     @Test
531     public void findOriginalTargetFromGroupingReturnNullTest() {
532         final Module module = mock(Module.class);
533         final QName qnamePath = QName.create("test", "2017-04-04", "test");
534         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
535         final DataSchemaNode schNode = null;
536         final Set<GroupingDefinition> groupings = new HashSet<>();
537         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
538         when(groupingDefinition.getQName()).thenReturn(qnamePath);
539         groupings.add(groupingDefinition);
540         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
541         when(module.getGroupings()).thenReturn(groupings);
542
543         final SchemaContext context = mock(SchemaContext.class);
544         when(context.findModule(qnamePath.getModule())).thenReturn(Optional.of(module));
545         final UsesNode usesNode = mock(UsesNode.class);
546         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
547
548         final Object[] args = {  };
549         final DataSchemaNode result = AugmentToGenType.findOriginalTargetFromGrouping(context, schemaPath, usesNode);
550         assertEquals(null, result);
551     }
552
553     @Test
554     public void findOriginalTargetFromGroupingTest() {
555         final Module module = mock(Module.class);
556         final QName qnamePath = QName.create("test", "2017-04-04", "test");
557         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
558         final DataSchemaNode schNode = mock(DataSchemaNode.class);
559         when(schNode.getPath()).thenReturn(schemaPath);
560         final Set<GroupingDefinition> groupings = new HashSet<>();
561         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
562         when(groupingDefinition.getQName()).thenReturn(qnamePath);
563         groupings.add(groupingDefinition);
564         final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
565         when(derivSchNode.isAddedByUses()).thenReturn(true);
566         final Optional optional = Optional.of(schNode);
567         when(derivSchNode.getOriginal()).thenReturn(optional);
568         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
569         when(module.getGroupings()).thenReturn(groupings);
570
571         final SchemaContext context = mock(SchemaContext.class);
572         when(context.findModule(qnamePath.getModule())).thenReturn(Optional.of(module));
573         final UsesNode usesNode = mock(UsesNode.class);
574         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
575
576         final DataSchemaNode result = AugmentToGenType.findOriginalTargetFromGrouping(context, schemaPath, usesNode);
577         assertEquals(schNode, result);
578     }
579
580     @Test
581     public void findOriginalTargetFromGroupingChoiceTest() {
582         final Module module = mock(Module.class);
583         final QName qnamePath = QName.create("test", "2017-04-04", "test");
584         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
585         final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
586         when(schNode.getPath()).thenReturn(schemaPath);
587         final Set<GroupingDefinition> groupings = new HashSet<>();
588         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
589         when(groupingDefinition.getQName()).thenReturn(qnamePath);
590         groupings.add(groupingDefinition);
591         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
592         when(module.getGroupings()).thenReturn(groupings);
593
594         final SchemaContext context = mock(SchemaContext.class);
595         when(context.findModule(qnamePath.getModule())).thenReturn(Optional.of(module));
596         final UsesNode usesNode = mock(UsesNode.class);
597         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
598
599         final DataSchemaNode result = AugmentToGenType.findOriginalTargetFromGrouping(context, schemaPath, usesNode);
600         assertEquals(schNode, result);
601     }
602
603     @Test
604     public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() {
605         final SchemaContext schemaContext = null;
606         final Module module = null;
607         final String pckgName = null;
608         final GeneratedType targetType = null;
609         final ChoiceSchemaNode targetNode = null;
610         final List<AugmentationSchemaNode> schemaPathAugmentListEntry = null;
611         final Map<Module, ModuleContext> genCtx = new HashMap<>();
612         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
613
614         try {
615             AugmentToGenType.generateTypesFromAugmentedChoiceCases(schemaContext, module, pckgName, targetType,
616                 targetNode, schemaPathAugmentListEntry, genCtx, false, genTypeBuilder, null,
617                 BindingNamespaceType.Data);
618             fail();
619         } catch (final IllegalArgumentException e) {
620             assertEquals("Base Package Name cannot be NULL.", e.getMessage());
621         }
622     }
623
624     @Test
625     public void generateTypesFromAugmentedChoiceCasesNullTargetType() {
626         final SchemaContext schemaContext = null;
627         final Module module = null;
628         final String pckgName = "";
629         final GeneratedType targetType = null;
630         final ChoiceSchemaNode targetNode = null;
631         final List<AugmentationSchemaNode> schemaPathAugmentListEntry = null;
632         final Map<Module, ModuleContext> genCtx = new HashMap<>();
633         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
634
635         try {
636             AugmentToGenType.generateTypesFromAugmentedChoiceCases(schemaContext, module, pckgName, targetType,
637                 targetNode, schemaPathAugmentListEntry, genCtx, false, genTypeBuilder, null,
638                 BindingNamespaceType.Data);
639             fail();
640         } catch (final IllegalArgumentException e) {
641             assertEquals("Referenced Choice Type cannot be NULL.", e.getMessage());
642         }
643     }
644
645     @Test
646     public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() {
647         final SchemaContext schemaContext = null;
648         final Module module = null;
649         final String pckgName = "";
650         final GeneratedType targetType = mock(GeneratedType.class);
651         final ChoiceSchemaNode targetNode = null;
652         final List<AugmentationSchemaNode> schemaPathAugmentListEntry = null;
653         final Map<Module, ModuleContext> genCtx = new HashMap<>();
654         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
655
656         try {
657             AugmentToGenType.generateTypesFromAugmentedChoiceCases(schemaContext, module, pckgName, targetType,
658                 targetNode, schemaPathAugmentListEntry, genCtx, false, genTypeBuilder, null,
659                 BindingNamespaceType.Data);
660             fail();
661         } catch (final IllegalArgumentException e) {
662             assertEquals("Set of Choice Case Nodes cannot be NULL.", e.getMessage());
663         }
664     }
665
666     @Test
667     public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() {
668         final SchemaContext schemaContext = null;
669         final Module module = null;
670         final String pckgName = "";
671         final GeneratedType targetType = mock(GeneratedType.class);
672         final ChoiceSchemaNode targetNode = null;
673         final Set<DataSchemaNode> augmentNodes = new HashSet<>();
674         final DataSchemaNode caseNode = null;
675         augmentNodes.add(caseNode);
676
677         final AugmentationSchemaNode AugmentationSchemaNode = mock(AugmentationSchemaNode.class);
678         when(AugmentationSchemaNode.getChildNodes()).thenReturn(augmentNodes);
679         final List<AugmentationSchemaNode> schemaPathAugmentListEntry = new ArrayList<>();
680         schemaPathAugmentListEntry.add(AugmentationSchemaNode);
681
682         final Map<Module, ModuleContext> genCtx = new HashMap<>();
683         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
684
685         final Map<Module, ModuleContext> result = AugmentToGenType.generateTypesFromAugmentedChoiceCases(schemaContext,
686             module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, genCtx, false,
687             genTypeBuilder, null, BindingNamespaceType.Data);
688         assertEquals(genCtx, result);
689     }
690
691     @Test
692     public void generateTypesFromAugmentedChoiceCasesTest() {
693         final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
694         final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
695         final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
696
697         final SchemaContext schemaContext = mock(SchemaContext.class);
698         final Module module = mock(Module.class);
699         when(module.getName()).thenReturn("test-module-case");
700         final CaseSchemaNode schemaNode = mock(CaseSchemaNode.class);
701         when(schemaNode.getPath()).thenReturn(path);
702         when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
703         when(module.getRevision()).thenReturn(qnamePath.getRevision());
704         when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
705         final String pckgName = "test.augment.choice.cases";
706         final GeneratedType targetType = mock(GeneratedType.class);
707         when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
708
709         final Set<DataSchemaNode> augmentNodes = new HashSet<>();
710         final CaseSchemaNode caseNode = mock(CaseSchemaNode.class);
711         when(caseNode.getPath()).thenReturn(path);
712         when(caseNode.getQName()).thenReturn(qnamePath);
713         when(caseNode.getDescription()).thenReturn(Optional.empty());
714         when(caseNode.getReference()).thenReturn(Optional.empty());
715         augmentNodes.add(caseNode);
716
717         final AugmentationSchemaNode AugmentationSchemaNode = mock(AugmentationSchemaNode.class);
718         when(AugmentationSchemaNode.getChildNodes()).thenReturn(augmentNodes);
719         final List<AugmentationSchemaNode> schemaPathAugmentListEntry = new ArrayList<>();
720         schemaPathAugmentListEntry.add(AugmentationSchemaNode);
721
722         final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
723         when(targetNode.getPath()).thenReturn(path);
724         when(targetNode.getDescription()).thenReturn(Optional.empty());
725         when(targetNode.getReference()).thenReturn(Optional.empty());
726         final Map<Module, ModuleContext> genCtx = new HashMap<>();
727         final ModuleContext moduleContext = new ModuleContext();
728         final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment", moduleContext);
729         when(targetType.getParentTypeForBuilder()).thenReturn(gtb);
730         genCtx.put(module, moduleContext);
731         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
732
733         when(schemaContext.findModule(qnamePath.getModule())).thenReturn(Optional.of(module));
734
735         final Map<Module, ModuleContext> result = AugmentToGenType.generateTypesFromAugmentedChoiceCases(schemaContext,
736             module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, genCtx, false,
737             genTypeBuilder, null, BindingNamespaceType.Data);
738         assertNotNull(result);
739         assertEquals(result.get(module), moduleContext);
740     }
741 }