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