Binding generator v2 - augments fix #2
[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 com.google.common.base.Optional;
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
31 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
32 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
33 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
34 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
37 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
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     @SuppressWarnings("unchecked")
54     @Test(expected = UnsupportedOperationException.class)
55     public void constructorTest() throws Throwable {
56         final Constructor<AugmentToGenType> constructor =
57                 (Constructor<AugmentToGenType>) AugmentToGenType.class.getDeclaredConstructors()[0];
58         constructor.setAccessible(true);
59         final Object[] objs = {};
60         try {
61             constructor.newInstance(objs);
62         } catch (final Exception e) {
63             throw e.getCause();
64         }
65     }
66
67     @SuppressWarnings("rawtypes")
68     @Test
69     public void generateNullModuleTest() throws Exception {
70         final Class[] parameterTypes =
71                 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
72         final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
73         assertNotNull(generate);
74         generate.setAccessible(true);
75
76         final SchemaContext context = null;
77         final TypeProvider typeProvider = null;
78         final Map<Module, ModuleContext> genCtx = new HashMap<>();
79         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
80         final Module m = null;
81
82         final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
83         try {
84             generate.invoke(AugmentToGenType.class, args);
85             fail();
86         } catch (final Exception e) {
87             assertNotNull(e);
88             assertTrue(e instanceof InvocationTargetException);
89             final Throwable cause = e.getCause();
90             assertNotNull(cause);
91             assertTrue(cause instanceof IllegalArgumentException);
92             assertEquals("Module reference cannot be NULL.", cause.getMessage());
93         }
94     }
95
96     @SuppressWarnings("rawtypes")
97     @Test
98     public void generateNullModuleNameTest() throws Exception {
99         final Class[] parameterTypes =
100                 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
101         final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
102         assertNotNull(generate);
103         generate.setAccessible(true);
104
105         final SchemaContext context = null;
106         final TypeProvider typeProvider = null;
107         final Map<Module, ModuleContext> genCtx = new HashMap<>();
108         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
109         final Module m = mock(Module.class);
110         when(m.getName()).thenReturn(null);
111
112         final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
113         try {
114             generate.invoke(AugmentToGenType.class, args);
115             fail();
116         } catch (final Exception e) {
117             assertNotNull(e);
118             assertTrue(e instanceof InvocationTargetException);
119             final Throwable cause = e.getCause();
120             assertNotNull(cause);
121             assertTrue(cause instanceof IllegalArgumentException);
122             assertEquals("Module name cannot be NULL.", cause.getMessage());
123         }
124     }
125
126     @SuppressWarnings("rawtypes")
127     @Test
128     public void generateNullModuleAugmentationsTest() throws Exception {
129         final Class[] parameterTypes =
130                 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
131         final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
132         assertNotNull(generate);
133         generate.setAccessible(true);
134
135         final SchemaContext context = null;
136         final TypeProvider typeProvider = null;
137         final Map<Module, ModuleContext> genCtx = new HashMap<>();
138         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
139         final Module m = mock(Module.class);
140         when(m.getName()).thenReturn("name");
141         when(m.getAugmentations()).thenReturn(null);
142
143         final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
144         try {
145             generate.invoke(AugmentToGenType.class, args);
146             fail();
147         } catch (final Exception e) {
148             assertNotNull(e);
149             assertTrue(e instanceof InvocationTargetException);
150             final Throwable cause = e.getCause();
151             assertNotNull(cause);
152             assertTrue(cause instanceof IllegalStateException);
153             assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
154         }
155     }
156
157     @SuppressWarnings("rawtypes")
158     @Test
159     public void generateWithoutAugmentationsTest() throws Exception {
160         final Class[] parameterTypes =
161                 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
162         final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
163         assertNotNull(generate);
164         generate.setAccessible(true);
165
166         final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test.yang");
167         final TypeProvider typeProvider = new TypeProviderImpl(context);
168         final Map<Module, ModuleContext> genCtx = new HashMap<>();
169         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
170
171         final Object[] args =
172                 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
173         final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
174         assertNotNull(invoke);
175     }
176
177     @SuppressWarnings({ "rawtypes", "unchecked" })
178     @Test
179     public void generateWithAugmentationsTest() throws Exception {
180         final Class[] parameterTypes =
181                 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
182         final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
183         assertNotNull(generate);
184         generate.setAccessible(true);
185
186         final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test-augment.yang");
187         final TypeProvider typeProvider = new TypeProviderImpl(context);
188         final Map<Module, ModuleContext> genCtx = mock(Map.class);
189         final Collection<ModuleContext> moduleContexts = new ArrayList<>();
190         final ModuleContext moduleContext = new ModuleContext();
191         moduleContexts.add(moduleContext);
192         final QName create = QName.create("urn:test:simple:test", "2017-02-06", "my-cont");
193         final SchemaNode schemaNode = mock(SchemaNode.class);
194         when(schemaNode.getPath()).thenReturn(SchemaPath.create(true, create));
195         moduleContext.addChildNodeType(schemaNode, new GeneratedTypeBuilderImpl("test", "Test"));
196         when(genCtx.values()).thenReturn(moduleContexts);
197         when(genCtx.get(context.getModules().iterator().next())).thenReturn(moduleContext);
198         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
199
200         final Object[] args =
201                 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
202         final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
203         assertNotNull(invoke);
204     }
205
206     @SuppressWarnings("rawtypes")
207     @Test
208     public void resolveAugmentationsNullModuleTest() throws Exception {
209         final Class[] parameterTypes = { Module.class };
210         final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
211         assertNotNull(generate);
212         generate.setAccessible(true);
213
214         final Module m = null;
215
216         final Object[] args = { m };
217         try {
218             generate.invoke(AugmentToGenType.class, args);
219             fail();
220         } catch (final Exception e) {
221             assertNotNull(e);
222             assertTrue(e instanceof InvocationTargetException);
223             final Throwable cause = e.getCause();
224             assertNotNull(cause);
225             assertTrue(cause instanceof IllegalArgumentException);
226             assertEquals("Module reference cannot be NULL.", cause.getMessage());
227         }
228     }
229
230     @SuppressWarnings("rawtypes")
231     @Test
232     public void resolveAugmentationsNullAugmentationsTest() throws Exception {
233         final Class[] parameterTypes = { Module.class };
234         final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
235         assertNotNull(generate);
236         generate.setAccessible(true);
237
238         final Module m = mock(Module.class);
239         when(m.getAugmentations()).thenReturn(null);
240
241         final Object[] args = { m };
242         try {
243             generate.invoke(AugmentToGenType.class, args);
244             fail();
245         } catch (final Exception e) {
246             assertNotNull(e);
247             assertTrue(e instanceof InvocationTargetException);
248             final Throwable cause = e.getCause();
249             assertNotNull(cause);
250             assertTrue(cause instanceof IllegalStateException);
251             assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
252         }
253     }
254
255     @SuppressWarnings({ "rawtypes", "unchecked" })
256     @Test
257     public void resolveAugmentationsTest() throws Exception {
258         final Class[] parameterTypes = { Module.class };
259         final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
260         assertNotNull(generate);
261         generate.setAccessible(true);
262
263         final Module m = mock(Module.class);
264
265         final Set<AugmentationSchema> augmentations = new HashSet<>();
266
267         final QName q1 = QName.create("q1", "2017-04-04", "q1");
268         final QName q2 = QName.create("q2", "2017-04-04", "q2");
269         final QName q3 = QName.create("q3", "2017-04-04", "q3");
270         final QName q4 = QName.create("q4", "2017-04-04", "q4");
271         final QName q5 = QName.create("q5", "2017-04-04", "q5");
272
273         final AugmentationSchema augmentationSchema1 = mock(AugmentationSchema.class);
274         when(augmentationSchema1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
275         final AugmentationSchema augmentationSchema2 = mock(AugmentationSchema.class);
276         when(augmentationSchema2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
277         augmentations.add(augmentationSchema1);
278         augmentations.add(augmentationSchema2);
279
280         when(m.getAugmentations()).thenReturn(augmentations);
281
282         final Object[] args = { m };
283
284         final List<AugmentationSchema> result =
285                 (List<AugmentationSchema>) generate.invoke(AugmentToGenType.class, args);
286         assertNotNull(result);
287         assertTrue(!result.isEmpty());
288         assertEquals(result.get(0), augmentationSchema1);
289         assertEquals(result.get(1), augmentationSchema2);
290     }
291
292     @Ignore
293     @SuppressWarnings({ "rawtypes" })
294     @Test
295     public void augmentationToGenTypesNullPckgNameTest() throws Exception {
296         final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
297                 boolean.class, Map.class, Map.class, TypeProvider.class };
298         final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
299         assertNotNull(generate);
300         generate.setAccessible(true);
301
302         final String augmPackName = null;
303         final AugmentationSchema augmSchema = null;
304         final SchemaContext context = null;
305         final TypeProvider typeProvider = null;
306         final Map<Module, ModuleContext> genCtx = new HashMap<>();
307         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
308         final Module m = null;
309
310         final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
311         try {
312             generate.invoke(AugmentToGenType.class, args);
313             fail();
314         } catch (final Exception e) {
315             assertNotNull(e);
316             assertTrue(e instanceof InvocationTargetException);
317             final Throwable cause = e.getCause();
318             assertNotNull(cause);
319             assertTrue(cause instanceof IllegalArgumentException);
320             assertEquals("Package Name cannot be NULL.", cause.getMessage());
321         }
322     }
323
324     @Ignore
325     @SuppressWarnings({ "rawtypes" })
326     @Test
327     public void augmentationToGenTypesNullAugSchemaTest() throws Exception {
328         final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
329                 boolean.class, Map.class, Map.class, TypeProvider.class };
330         final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
331         assertNotNull(generate);
332         generate.setAccessible(true);
333
334         final String augmPackName = "pckg.name";
335         final AugmentationSchema augmSchema = null;
336         final SchemaContext context = null;
337         final TypeProvider typeProvider = null;
338         final Map<Module, ModuleContext> genCtx = new HashMap<>();
339         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
340         final Module m = null;
341
342         final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
343         try {
344             generate.invoke(AugmentToGenType.class, args);
345             fail();
346         } catch (final Exception e) {
347             assertNotNull(e);
348             assertTrue(e instanceof InvocationTargetException);
349             final Throwable cause = e.getCause();
350             assertNotNull(cause);
351             assertTrue(cause instanceof IllegalArgumentException);
352             assertEquals("Augmentation Schema cannot be NULL.", cause.getMessage());
353         }
354     }
355
356     @Ignore
357     @SuppressWarnings({ "rawtypes" })
358     @Test
359     public void augmentationToGenTypesNullAugSchemaTargetPathTest() throws Exception {
360         final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
361                 boolean.class, Map.class, Map.class, TypeProvider.class };
362         final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
363         assertNotNull(generate);
364         generate.setAccessible(true);
365
366         final String augmPackName = "pckg.name";
367         final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
368         when(augmSchema.getTargetPath()).thenReturn(null);
369
370         final SchemaContext context = null;
371         final TypeProvider typeProvider = null;
372         final Map<Module, ModuleContext> genCtx = new HashMap<>();
373         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
374         final Module m = null;
375
376         final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
377         try {
378             generate.invoke(AugmentToGenType.class, args);
379             fail();
380         } catch (final Exception e) {
381             assertNotNull(e);
382             assertTrue(e instanceof InvocationTargetException);
383             final Throwable cause = e.getCause();
384             assertNotNull(cause);
385             assertTrue(cause instanceof IllegalStateException);
386             assertEquals("Augmentation Schema does not contain Target Path (Target Path is NULL).", cause.getMessage());
387         }
388     }
389
390     @Ignore
391     @SuppressWarnings({ "rawtypes" })
392     @Test
393     public void augmentationToGenTypesNullAugSchemaTargetNodeTest() throws Exception {
394         final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
395                 boolean.class, Map.class, Map.class, TypeProvider.class };
396         final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
397         assertNotNull(generate);
398         generate.setAccessible(true);
399
400         final String augmPackName = "pckg.name";
401
402         final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
403         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
404         final SchemaPath path = SchemaPath.create(true, qnamePath);
405         when(augmSchema.getTargetPath()).thenReturn(path);
406         final Set<UsesNode> uses = new HashSet<>();
407         when(augmSchema.getUses()).thenReturn(uses);
408
409         final SchemaContext context = mock(SchemaContext.class);
410         final Module moduleAug = mock(Module.class);
411         final DataSchemaNode schNode = null;
412         when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
413         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
414                 .thenReturn(moduleAug);
415
416         final TypeProvider typeProvider = null;
417         final Map<Module, ModuleContext> genCtx = new HashMap<>();
418         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
419
420         final Module m = mock(Module.class);
421         when(m.getName()).thenReturn("augm-module");
422         when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
423         when(m.getRevision()).thenReturn(qnamePath.getRevision());
424
425         final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
426         try {
427             generate.invoke(AugmentToGenType.class, args);
428             fail();
429         } catch (final Exception e) {
430             assertNotNull(e);
431             assertTrue(e instanceof InvocationTargetException);
432             final Throwable cause = e.getCause();
433             assertNotNull(cause);
434             assertTrue(cause instanceof IllegalArgumentException);
435             assertEquals("augment target not found: " + path, cause.getMessage());
436         }
437     }
438
439     @Ignore
440     @SuppressWarnings({ "rawtypes" })
441     @Test
442     public void augmentationToGenTypesNullAugTargetGTBTest() throws Exception {
443         final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
444                 boolean.class, Map.class, Map.class, TypeProvider.class };
445         final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
446         assertNotNull(generate);
447         generate.setAccessible(true);
448
449         final String augmPackName = "pckg.name";
450
451         final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
452         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
453         final SchemaPath path = SchemaPath.create(true, qnamePath);
454         when(augmSchema.getTargetPath()).thenReturn(path);
455         final Set<UsesNode> uses = new HashSet<>();
456         when(augmSchema.getUses()).thenReturn(uses);
457
458         final SchemaContext context = mock(SchemaContext.class);
459         final Module moduleAug = mock(Module.class);
460         final DataSchemaNode schNode = mock(DataSchemaNode.class);
461         when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
462         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
463                 .thenReturn(moduleAug);
464
465         final TypeProvider typeProvider = null;
466         final Map<Module, ModuleContext> genCtx = new HashMap<>();
467         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
468
469         final Module m = mock(Module.class);
470         when(m.getName()).thenReturn("augm-module");
471         when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
472         when(m.getRevision()).thenReturn(qnamePath.getRevision());
473
474         final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
475         try {
476             generate.invoke(AugmentToGenType.class, args);
477             fail();
478         } catch (final Exception e) {
479             assertNotNull(e);
480             assertTrue(e instanceof InvocationTargetException);
481             final Throwable cause = e.getCause();
482             assertNotNull(cause);
483             assertTrue(cause instanceof NullPointerException);
484             assertEquals("Target type not yet generated: " + schNode, cause.getMessage());
485         }
486     }
487
488     @Ignore
489     @SuppressWarnings({ "rawtypes", "unchecked" })
490     @Test
491     public void augmentationToGenTypesAugUsesNullOrigTargetTest() throws Exception {
492         final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
493                 boolean.class, Map.class, Map.class, TypeProvider.class };
494         final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
495         assertNotNull(generate);
496         generate.setAccessible(true);
497
498         final String augmPackName = "pckg.name";
499
500         final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
501         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
502         final SchemaPath path = SchemaPath.create(true, qnamePath);
503         when(augmSchema.getTargetPath()).thenReturn(path);
504         final Set<UsesNode> uses = new HashSet<>();
505         when(augmSchema.getUses()).thenReturn(uses);
506
507         final SchemaContext context = mock(SchemaContext.class);
508         final Module moduleAug = mock(Module.class);
509         final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
510         when(targetSchNode.getPath()).thenReturn(path);
511         when(targetSchNode.isAddedByUses()).thenReturn(true);
512         final Optional optionalSchemaNode = Optional.absent();
513         when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
514         when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
515         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
516                 .thenReturn(moduleAug);
517
518         final TypeProvider typeProvider = null;
519         final Map<Module, ModuleContext> genCtx = new HashMap<>();
520         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
521
522         final Module m = mock(Module.class);
523         when(m.getName()).thenReturn("augm-module");
524         when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
525         when(m.getRevision()).thenReturn(qnamePath.getRevision());
526
527         final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
528         try {
529             generate.invoke(AugmentToGenType.class, args);
530             fail();
531         } catch (final Exception e) {
532             assertNotNull(e);
533             assertTrue(e instanceof InvocationTargetException);
534             final Throwable cause = e.getCause();
535             assertNotNull(cause);
536             assertTrue(cause instanceof IllegalStateException);
537             assertEquals("Failed to find target node from grouping in augmentation " + augmSchema + " in module "
538                     + m.getName(), cause.getMessage());
539         }
540     }
541
542     @Ignore
543     @SuppressWarnings({ "rawtypes" })
544     @Test
545     public void augmentationToGenTypesTargetChoicSchemaNodeTest() throws Exception {
546         final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
547                 boolean.class, Map.class, Map.class, TypeProvider.class };
548         final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
549         assertNotNull(generate);
550         generate.setAccessible(true);
551
552         final String augmPackName = "pckg.name";
553
554         final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
555         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
556         final SchemaPath path = SchemaPath.create(true, qnamePath);
557         when(augmSchema.getTargetPath()).thenReturn(path);
558         final Set<UsesNode> uses = new HashSet<>();
559         when(augmSchema.getUses()).thenReturn(uses);
560
561         final SchemaContext context = mock(SchemaContext.class);
562         final Module moduleAug = mock(Module.class);
563         final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
564         when(targetSchNode.getPath()).thenReturn(path);
565         when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
566         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
567                 .thenReturn(moduleAug);
568
569         final TypeProvider typeProvider = null;
570
571         final Map genCtx = mock(Map.class);
572         final Collection<ModuleContext> moduleContexts = new ArrayList<>();
573         final ModuleContext mc = new ModuleContext();
574         final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
575         mc.addChildNodeType(targetSchNode, gtb);
576         moduleContexts.add(mc);
577         when(genCtx.values()).thenReturn(moduleContexts);
578
579         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
580
581         final Module m = mock(Module.class);
582         when(m.getName()).thenReturn("augm-module");
583         when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
584         when(m.getRevision()).thenReturn(qnamePath.getRevision());
585
586         final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
587         final Map result = (Map) generate.invoke(AugmentToGenType.class, args);
588         assertNotNull(result);
589     }
590
591     @Ignore
592     @SuppressWarnings({ "rawtypes", "unchecked" })
593     @Test
594     public void augmentationToGenTypesTest() throws Exception {
595         final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
596                 boolean.class, Map.class, Map.class, TypeProvider.class };
597         final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
598         assertNotNull(generate);
599         generate.setAccessible(true);
600
601         final String augmPackName = "pckg.name";
602
603         final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
604         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
605         final SchemaPath path = SchemaPath.create(true, qnamePath);
606         when(augmSchema.getTargetPath()).thenReturn(path);
607         final Set<UsesNode> uses = new HashSet<>();
608         when(augmSchema.getUses()).thenReturn(uses);
609         final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
610         when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
611
612         final SchemaContext context = mock(SchemaContext.class);
613         final Module moduleAug = mock(Module.class);
614         final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
615         when(targetSchNode.getPath()).thenReturn(path);
616         when(targetSchNode.isAddedByUses()).thenReturn(true);
617         final DataSchemaNode origSchNode = mock(DataSchemaNode.class);
618         when(origSchNode.getPath()).thenReturn(path);
619         when(origSchNode.isAddedByUses()).thenReturn(true);
620         final Optional optionalSchemaNode = Optional.of(origSchNode);
621         when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
622         when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
623         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
624                 .thenReturn(moduleAug);
625
626         final TypeProvider typeProvider = null;
627
628         final Map<Module, ModuleContext> genCtx = new HashMap<>();
629
630         final Collection<ModuleContext> moduleContexts = new ArrayList<>();
631         final ModuleContext mc = new ModuleContext();
632         final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
633         mc.addChildNodeType(targetSchNode, gtb);
634         moduleContexts.add(mc);
635         genCtx.put(moduleAug, mc);
636
637         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
638
639         when(moduleAug.getName()).thenReturn("augm-module");
640         when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
641         when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
642
643         final Object[] args =
644                 { augmPackName, augmSchema, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
645         final Map<Module, ModuleContext> result =
646                 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
647         assertNotNull(result);
648         final ModuleContext moduleContext = result.get(moduleAug);
649         assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
650         assertEquals("pckg.name", moduleContext.getAugmentations().get(0).getPackageName());
651         assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
652         assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
653     }
654
655     @Test
656     public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
657         try {
658             AugmentToGenType.usesAugmentationToGenTypes(null, null, (AugmentationSchema) null, null, null, null, null, null, false, null);
659         } catch (final Exception e) {
660             assertNotNull(e);
661             assertTrue(e instanceof IllegalArgumentException);
662             assertEquals(e.getMessage(), "Package Name cannot be NULL.");
663         }
664     }
665
666     @Test
667     public void usesAugmentationToGenTypesNullAugSchemaNodeTest() throws Exception {
668         try {
669             AugmentToGenType.usesAugmentationToGenTypes(null, "", (AugmentationSchema) null, null, null, null, null, null, false, null);
670         } catch (final Exception e) {
671             assertNotNull(e);
672             assertTrue(e instanceof IllegalArgumentException);
673             assertEquals(e.getMessage(), "Augmentation Schema cannot be NULL.");
674         }
675     }
676
677     @Test
678     public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
679         final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
680         when(augmentationSchema.getTargetPath()).thenReturn(null);
681         try {
682             AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchema, null, null, null, null, null,
683                     false, null);
684         } catch (final Exception e) {
685             assertNotNull(e);
686             assertTrue(e instanceof IllegalStateException);
687             assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
688         }
689     }
690
691     @Test
692     public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
693         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
694         final SchemaPath path = SchemaPath.create(true, qnamePath);
695         final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
696         when(augmentationSchema.getTargetPath()).thenReturn(path);
697         final Set<UsesNode> uses = new HashSet<>();
698         when(augmentationSchema.getUses()).thenReturn(uses);
699
700         final SchemaContext context = mock(SchemaContext.class);
701         final Module moduleAug = mock(Module.class);
702         when(moduleAug.getName()).thenReturn("augm-module");
703         when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
704         when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
705         final Set<GroupingDefinition> groupings = new HashSet<>();
706         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
707         when(groupingDefinition.getQName()).thenReturn(qnamePath);
708         groupings.add(groupingDefinition);
709         when(moduleAug.getGroupings()).thenReturn(groupings);
710
711         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
712                 .thenReturn(moduleAug);
713
714         final Map<Module, ModuleContext> genCtx = new HashMap<>();
715         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
716
717
718         final UsesNode usesNode = mock(UsesNode.class);
719         final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
720
721         when(usesNode.getGroupingPath()).thenReturn(path);
722
723         try {
724             AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchema, moduleAug,
725                     usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
726         } catch (final Exception e) {
727             assertNotNull(e);
728             assertTrue(e instanceof IllegalArgumentException);
729             assertEquals(e.getMessage(), "augment target not found: " + path);
730         }
731     }
732
733     @Test
734     public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
735         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
736         final SchemaPath path = SchemaPath.create(true, qnamePath);
737         final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
738         when(augmentationSchema.getTargetPath()).thenReturn(path);
739         final Set<UsesNode> uses = new HashSet<>();
740         when(augmentationSchema.getUses()).thenReturn(uses);
741
742         final SchemaContext context = mock(SchemaContext.class);
743         final Module moduleAug = mock(Module.class);
744         when(moduleAug.getName()).thenReturn("augm-module");
745         when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
746         when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
747         final Set<GroupingDefinition> groupings = new HashSet<>();
748         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
749         when(groupingDefinition.getQName()).thenReturn(qnamePath);
750         final DataSchemaNode schNode = mock(DataSchemaNode.class);
751         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
752         groupings.add(groupingDefinition);
753         when(moduleAug.getGroupings()).thenReturn(groupings);
754
755         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
756                 .thenReturn(moduleAug);
757
758         final Map<Module, ModuleContext> genCtx = new HashMap<>();
759         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
760
761         final UsesNode usesNode = mock(UsesNode.class);
762         final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
763
764         when(usesNode.getGroupingPath()).thenReturn(path);
765
766         try {
767             AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchema, moduleAug,
768                     usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
769         } catch (final Exception e) {
770             assertNotNull(e);
771             assertTrue(e instanceof NullPointerException);
772             assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
773         }
774     }
775
776     @Test
777     public void usesAugmentationToGenTypesTest() throws Exception {
778         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
779         final SchemaPath path = SchemaPath.create(true, qnamePath);
780         final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
781         when(augmentationSchema.getTargetPath()).thenReturn(path);
782         final Set<UsesNode> uses = new HashSet<>();
783         when(augmentationSchema.getUses()).thenReturn(uses);
784
785         final SchemaContext context = mock(SchemaContext.class);
786         final Module moduleAug = mock(Module.class);
787         when(moduleAug.getName()).thenReturn("augm-module");
788         when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
789         when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
790         final Set<GroupingDefinition> groupings = new HashSet<>();
791         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
792         when(groupingDefinition.getQName()).thenReturn(qnamePath);
793         final DataSchemaNode schNode = mock(DataSchemaNode.class);
794         when(schNode.getPath()).thenReturn(path);
795         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
796         groupings.add(groupingDefinition);
797         when(moduleAug.getGroupings()).thenReturn(groupings);
798
799         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
800                 .thenReturn(moduleAug);
801
802         final Map<Module, ModuleContext> genCtx = new HashMap<>();
803         final ModuleContext mc = new ModuleContext();
804         final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
805         mc.addChildNodeType(schNode, gtb);
806         genCtx.put(moduleAug, mc);
807
808         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
809
810         final UsesNode usesNode = mock(UsesNode.class);
811         final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
812
813         when(usesNode.getGroupingPath()).thenReturn(path);
814
815         final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
816                 augmentationSchema, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
817         assertNotNull(result);
818     }
819
820     @Test
821     public void usesAugmentationToGenTypesChoiceTest() throws Exception {
822         final QName qnamePath = QName.create("test", "2017-04-04", "aug");
823         final SchemaPath path = SchemaPath.create(true, qnamePath);
824         final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
825         when(augmentationSchema.getTargetPath()).thenReturn(path);
826         final Set<UsesNode> uses = new HashSet<>();
827         when(augmentationSchema.getUses()).thenReturn(uses);
828
829         final SchemaContext context = mock(SchemaContext.class);
830         final Module moduleAug = mock(Module.class);
831         when(moduleAug.getName()).thenReturn("augm-module");
832         when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
833         when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
834         final Set<GroupingDefinition> groupings = new HashSet<>();
835         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
836         when(groupingDefinition.getQName()).thenReturn(qnamePath);
837         final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
838         when(schNode.getPath()).thenReturn(path);
839         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
840         groupings.add(groupingDefinition);
841         when(moduleAug.getGroupings()).thenReturn(groupings);
842
843         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
844                 .thenReturn(moduleAug);
845
846         final Map<Module, ModuleContext> genCtx = new HashMap<>();
847         final ModuleContext mc = new ModuleContext();
848         final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
849         mc.addChildNodeType(schNode, gtb);
850         genCtx.put(moduleAug, mc);
851
852         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
853
854         final UsesNode usesNode = mock(UsesNode.class);
855         final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
856
857         when(usesNode.getGroupingPath()).thenReturn(path);
858
859         final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
860                 augmentationSchema, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
861         assertNotNull(result);
862     }
863
864     @SuppressWarnings({ "rawtypes" })
865     @Test
866     public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
867         final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
868         final Method generate =
869                 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
870         assertNotNull(generate);
871         generate.setAccessible(true);
872
873         final Module module = mock(Module.class);
874         final QName qnamePath = QName.create("test", "2017-04-04", "test");
875         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
876         final DataSchemaNode schNode = mock(DataSchemaNode.class);
877         when(schNode.getPath()).thenReturn(schemaPath);
878         when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
879
880         final SchemaContext context = mock(SchemaContext.class);
881         when(context.findModuleByNamespaceAndRevision(qnamePath .getNamespace(), qnamePath.getRevision()))
882                 .thenReturn(module);
883         final UsesNode usesNode = mock(UsesNode.class);
884         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
885
886         final Object[] args = { context, schemaPath, usesNode };
887         try {
888             generate.invoke(AugmentToGenType.class, args);
889             fail();
890         } catch (final Exception e) {
891             assertNotNull(e);
892             assertTrue(e instanceof InvocationTargetException);
893             final Throwable cause = e.getCause();
894             assertNotNull(cause);
895             assertTrue(cause instanceof IllegalArgumentException);
896             assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
897         }
898     }
899
900     @SuppressWarnings({ "rawtypes" })
901     @Test
902     public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
903         final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
904         final Method generate =
905                 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
906         assertNotNull(generate);
907         generate.setAccessible(true);
908
909         final Module module = mock(Module.class);
910         final QName qnamePath = QName.create("test", "2017-04-04", "test");
911         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
912         final DataSchemaNode schNode = mock(DataSchemaNode.class);
913         when(schNode.getPath()).thenReturn(schemaPath);
914         when(schNode.isAddedByUses()).thenReturn(true);
915         final Set<GroupingDefinition> groupings = new HashSet<>();
916         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
917         when(groupingDefinition.getQName()).thenReturn(qnamePath);
918         groupings.add(groupingDefinition);
919         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
920         when(module.getGroupings()).thenReturn(groupings);
921
922         final SchemaContext context = mock(SchemaContext.class);
923         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
924                 .thenReturn(module);
925         final UsesNode usesNode = mock(UsesNode.class);
926         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
927
928         final Object[] args = { context, schemaPath, usesNode };
929         try {
930             generate.invoke(AugmentToGenType.class, args);
931             fail();
932         } catch (final Exception e) {
933             assertNotNull(e);
934             assertTrue(e instanceof InvocationTargetException);
935             final Throwable cause = e.getCause();
936             assertNotNull(cause);
937             assertTrue(cause instanceof IllegalStateException);
938             assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
939         }
940     }
941
942     @SuppressWarnings({ "rawtypes" })
943     @Test
944     public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
945         final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
946         final Method generate =
947                 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
948         assertNotNull(generate);
949         generate.setAccessible(true);
950
951         final Module module = mock(Module.class);
952         final QName qnamePath = QName.create("test", "2017-04-04", "test");
953         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
954         final DataSchemaNode schNode = null;
955         final Set<GroupingDefinition> groupings = new HashSet<>();
956         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
957         when(groupingDefinition.getQName()).thenReturn(qnamePath);
958         groupings.add(groupingDefinition);
959         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
960         when(module.getGroupings()).thenReturn(groupings);
961
962         final SchemaContext context = mock(SchemaContext.class);
963         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
964                 .thenReturn(module);
965         final UsesNode usesNode = mock(UsesNode.class);
966         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
967
968         final Object[] args = { context, schemaPath, usesNode };
969         final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
970         assertEquals(null, result);
971     }
972
973     @SuppressWarnings({ "rawtypes", "unchecked" })
974     @Test
975     public void findOriginalTargetFromGroupingTest() throws Exception {
976         final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
977         final Method generate =
978                 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
979         assertNotNull(generate);
980         generate.setAccessible(true);
981
982         final Module module = mock(Module.class);
983         final QName qnamePath = QName.create("test", "2017-04-04", "test");
984         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
985         final DataSchemaNode schNode = mock(DataSchemaNode.class);
986         when(schNode.getPath()).thenReturn(schemaPath);
987         final Set<GroupingDefinition> groupings = new HashSet<>();
988         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
989         when(groupingDefinition.getQName()).thenReturn(qnamePath);
990         groupings.add(groupingDefinition);
991         final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
992         when(derivSchNode.isAddedByUses()).thenReturn(true);
993         final Optional optional = Optional.of(schNode);
994         when(derivSchNode.getOriginal()).thenReturn(optional);
995         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
996         when(module.getGroupings()).thenReturn(groupings);
997
998         final SchemaContext context = mock(SchemaContext.class);
999         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1000                 .thenReturn(module);
1001         final UsesNode usesNode = mock(UsesNode.class);
1002         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1003
1004         final Object[] args = { context, schemaPath, usesNode };
1005         final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1006         assertEquals(schNode, result);
1007     }
1008
1009     @SuppressWarnings({ "rawtypes" })
1010     @Test
1011     public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1012         final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1013         final Method generate =
1014                 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1015         assertNotNull(generate);
1016         generate.setAccessible(true);
1017
1018         final Module module = mock(Module.class);
1019         final QName qnamePath = QName.create("test", "2017-04-04", "test");
1020         final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1021         final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1022         when(schNode.getPath()).thenReturn(schemaPath);
1023         final Set<GroupingDefinition> groupings = new HashSet<>();
1024         final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1025         when(groupingDefinition.getQName()).thenReturn(qnamePath);
1026         groupings.add(groupingDefinition);
1027         when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1028         when(module.getGroupings()).thenReturn(groupings);
1029
1030         final SchemaContext context = mock(SchemaContext.class);
1031         when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1032                 .thenReturn(module);
1033         final UsesNode usesNode = mock(UsesNode.class);
1034         when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1035
1036         final Object[] args = { context, schemaPath, usesNode };
1037         final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1038         assertEquals(schNode, result);
1039     }
1040
1041     @SuppressWarnings({ "rawtypes" })
1042     @Test
1043     public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1044         final Class[] parameterTypes =
1045                 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1046                         DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1047         final Method generate =
1048                 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1049         assertNotNull(generate);
1050         generate.setAccessible(true);
1051
1052         final SchemaContext schemaContext = null;
1053         final Module module = null;
1054         final String pckgName = null;
1055         final Type targetType = null;
1056         final ChoiceSchemaNode targetNode = null;
1057         final Iterable augmentNodes = null;
1058         final DataNodeContainer usesNodeParent = null;
1059         final Map<Module, ModuleContext> genCtx = new HashMap<>();
1060         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1061
1062         final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1063                 genCtx, false, genTypeBuilder, null };
1064         try {
1065             generate.invoke(AugmentToGenType.class, args);
1066             fail();
1067         } catch (final Exception e) {
1068             assertNotNull(e);
1069             assertTrue(e instanceof InvocationTargetException);
1070             final Throwable cause = e.getCause();
1071             assertNotNull(cause);
1072             assertTrue(cause instanceof IllegalArgumentException);
1073             assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1074         }
1075     }
1076
1077     @SuppressWarnings({ "rawtypes" })
1078     @Test
1079     public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1080         final Class[] parameterTypes =
1081                 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1082                         DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1083         final Method generate =
1084                 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1085         assertNotNull(generate);
1086         generate.setAccessible(true);
1087
1088         final SchemaContext schemaContext = null;
1089         final Module module = null;
1090         final String pckgName = "";
1091         final Type targetType = null;
1092         final ChoiceSchemaNode targetNode = null;
1093         final Iterable augmentNodes = null;
1094         final DataNodeContainer usesNodeParent = null;
1095         final Map<Module, ModuleContext> genCtx = new HashMap<>();
1096         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1097
1098         final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1099                 genCtx, false, genTypeBuilder, null };
1100         try {
1101             generate.invoke(AugmentToGenType.class, args);
1102             fail();
1103         } catch (final Exception e) {
1104             assertNotNull(e);
1105             assertTrue(e instanceof InvocationTargetException);
1106             final Throwable cause = e.getCause();
1107             assertNotNull(cause);
1108             assertTrue(cause instanceof IllegalArgumentException);
1109             assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1110         }
1111     }
1112
1113     @SuppressWarnings({ "rawtypes" })
1114     @Test
1115     public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1116         final Class[] parameterTypes =
1117                 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1118                         DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1119         final Method generate =
1120                 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1121         assertNotNull(generate);
1122         generate.setAccessible(true);
1123
1124         final SchemaContext schemaContext = null;
1125         final Module module = null;
1126         final String pckgName = "";
1127         final Type targetType = mock(Type.class);
1128         final ChoiceSchemaNode targetNode = null;
1129         final Iterable augmentNodes = null;
1130         final DataNodeContainer usesNodeParent = null;
1131         final Map<Module, ModuleContext> genCtx = new HashMap<>();
1132         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1133
1134         final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1135                 genCtx, false, genTypeBuilder, null };
1136         try {
1137             generate.invoke(AugmentToGenType.class, args);
1138             fail();
1139         } catch (final Exception e) {
1140             assertNotNull(e);
1141             assertTrue(e instanceof InvocationTargetException);
1142             final Throwable cause = e.getCause();
1143             assertNotNull(cause);
1144             assertTrue(cause instanceof IllegalArgumentException);
1145             assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1146         }
1147     }
1148
1149     @SuppressWarnings({ "rawtypes", "unchecked" })
1150     @Test
1151     public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1152         final Class[] parameterTypes =
1153                 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1154                         DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1155         final Method generate =
1156                 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1157         assertNotNull(generate);
1158         generate.setAccessible(true);
1159
1160         final SchemaContext schemaContext = null;
1161         final Module module = null;
1162         final String pckgName = "";
1163         final Type targetType = mock(Type.class);
1164         final ChoiceSchemaNode targetNode = null;
1165         final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1166         final DataSchemaNode caseNode = null;
1167         augmentNodes.add(caseNode);
1168         final DataNodeContainer usesNodeParent = null;
1169         final Map<Module, ModuleContext> genCtx = new HashMap<>();
1170         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1171
1172         final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1173                 genCtx, false, genTypeBuilder, null };
1174         final Map<Module, ModuleContext> result =
1175                 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1176         assertEquals(genCtx, result);
1177     }
1178
1179     @SuppressWarnings({ "rawtypes" })
1180     @Test
1181     public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1182         final Class[] parameterTypes =
1183                 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1184                         DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1185         final Method generate =
1186                 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1187         assertNotNull(generate);
1188         generate.setAccessible(true);
1189
1190         final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1191         final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1192         final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1193
1194         final SchemaContext schemaContext = mock(SchemaContext.class);
1195         final Module module = mock(Module.class);
1196         when(module.getName()).thenReturn("test-module-case");
1197         final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1198         when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1199         when(module.getRevision()).thenReturn(qnamePath.getRevision());
1200         when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1201         final String pckgName = "test.augment.choice.cases";
1202         final Type targetType = mock(Type.class);
1203         final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1204         final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1205         when(caseNode.getPath()).thenReturn(path);
1206         when(caseNode.getQName()).thenReturn(qnamePath);
1207         augmentNodes.add(caseNode);
1208         final DataNodeContainer usesNodeParent = null;
1209         final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1210         when(targetNode.getPath()).thenReturn(path);
1211         final Map<Module, ModuleContext> genCtx = new HashMap<>();
1212         final ModuleContext moduleContext = new ModuleContext();
1213         final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1214         moduleContext.addCaseType(path, gtb);
1215         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1216
1217         when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1218                 .thenReturn(module);
1219
1220         final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1221                 genCtx, false, genTypeBuilder, null };
1222         try {
1223             generate.invoke(AugmentToGenType.class, args);
1224             fail();
1225         } catch (final Exception e) {
1226             assertNotNull(e);
1227             assertTrue(e instanceof InvocationTargetException);
1228             final Throwable cause = e.getCause();
1229             assertNotNull(cause);
1230             assertTrue(cause instanceof IllegalArgumentException);
1231             assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1232         }
1233     }
1234
1235     @SuppressWarnings({ "rawtypes", "unchecked" })
1236     @Test
1237     public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1238         final Class[] parameterTypes =
1239                 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1240                         DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1241         final Method generate =
1242                 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1243         assertNotNull(generate);
1244         generate.setAccessible(true);
1245
1246         final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1247         final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1248         final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1249
1250         final SchemaContext schemaContext = mock(SchemaContext.class);
1251         final Module module = mock(Module.class);
1252         when(module.getName()).thenReturn("test-module-case");
1253         final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1254         when(schemaNode.getPath()).thenReturn(path);
1255         when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1256         when(module.getRevision()).thenReturn(qnamePath.getRevision());
1257         when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1258         final String pckgName = "test.augment.choice.cases";
1259         final Type targetType = mock(Type.class);
1260         final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1261         final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1262         when(caseNode.getPath()).thenReturn(path);
1263         when(caseNode.getQName()).thenReturn(qnamePath);
1264         augmentNodes.add(caseNode);
1265         final DataNodeContainer usesNodeParent = null;
1266         final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1267         when(targetNode.getPath()).thenReturn(path);
1268         final Map<Module, ModuleContext> genCtx = new HashMap<>();
1269         final ModuleContext moduleContext = new ModuleContext();
1270         final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1271         moduleContext.addCaseType(path, gtb);
1272         genCtx.put(module, moduleContext);
1273         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1274
1275         when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1276                 .thenReturn(module);
1277
1278         final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1279                 genCtx, false, genTypeBuilder, null };
1280         final Map<Module, ModuleContext> result =
1281                 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1282         assertNotNull(result);
1283         assertEquals(result.get(module), moduleContext);
1284     }
1285 }