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