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