2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.javav2.generator.impl;
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;
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;
28 import org.junit.Test;
29 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
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.runtime.BindingNamespaceType;
36 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
39 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
40 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
45 import org.opendaylight.yangtools.yang.model.api.Module;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.UsesNode;
51 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
53 public class AugmentToGenTypeTest {
55 @SuppressWarnings("unchecked")
56 @Test(expected = UnsupportedOperationException.class)
57 public void constructorTest() throws Throwable {
58 final Constructor<AugmentToGenType> constructor =
59 (Constructor<AugmentToGenType>) AugmentToGenType.class.getDeclaredConstructors()[0];
60 constructor.setAccessible(true);
61 final Object[] objs = {};
63 constructor.newInstance(objs);
64 } catch (final Exception e) {
69 @SuppressWarnings("rawtypes")
71 public void generateNullModuleTest() throws Exception {
72 final Class[] parameterTypes =
73 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
74 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
75 assertNotNull(generate);
76 generate.setAccessible(true);
78 final SchemaContext context = null;
79 final TypeProvider typeProvider = null;
80 final Map<Module, ModuleContext> genCtx = new HashMap<>();
81 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
82 final Module m = null;
84 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
86 generate.invoke(AugmentToGenType.class, args);
88 } catch (final Exception e) {
90 assertTrue(e instanceof InvocationTargetException);
91 final Throwable cause = e.getCause();
93 assertTrue(cause instanceof IllegalArgumentException);
94 assertEquals("Module reference cannot be NULL.", cause.getMessage());
98 @SuppressWarnings("rawtypes")
100 public void generateNullModuleNameTest() throws Exception {
101 final Class[] parameterTypes =
102 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
103 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
104 assertNotNull(generate);
105 generate.setAccessible(true);
107 final SchemaContext context = null;
108 final TypeProvider typeProvider = null;
109 final Map<Module, ModuleContext> genCtx = new HashMap<>();
110 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
111 final Module m = mock(Module.class);
112 when(m.getName()).thenReturn(null);
114 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
116 generate.invoke(AugmentToGenType.class, args);
118 } catch (final Exception e) {
120 assertTrue(e instanceof InvocationTargetException);
121 final Throwable cause = e.getCause();
122 assertNotNull(cause);
123 assertTrue(cause instanceof IllegalArgumentException);
124 assertEquals("Module name cannot be NULL.", cause.getMessage());
128 @SuppressWarnings("rawtypes")
130 public void generateNullModuleAugmentationsTest() throws Exception {
131 final Class[] parameterTypes =
132 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
133 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
134 assertNotNull(generate);
135 generate.setAccessible(true);
137 final SchemaContext context = null;
138 final TypeProvider typeProvider = null;
139 final Map<Module, ModuleContext> genCtx = new HashMap<>();
140 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
141 final Module m = mock(Module.class);
142 when(m.getName()).thenReturn("name");
143 when(m.getAugmentations()).thenReturn(null);
145 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
147 generate.invoke(AugmentToGenType.class, args);
149 } catch (final Exception e) {
151 assertTrue(e instanceof InvocationTargetException);
152 final Throwable cause = e.getCause();
153 assertNotNull(cause);
154 assertTrue(cause instanceof IllegalStateException);
155 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
159 @SuppressWarnings("rawtypes")
161 public void generateWithoutAugmentationsTest() throws Exception {
162 final Class[] parameterTypes =
163 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
164 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
165 assertNotNull(generate);
166 generate.setAccessible(true);
168 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test.yang");
169 final TypeProvider typeProvider = new TypeProviderImpl(context);
170 final Map<Module, ModuleContext> genCtx = new HashMap<>();
171 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
173 final Object[] args =
174 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
175 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
176 assertNotNull(invoke);
179 @SuppressWarnings({ "rawtypes", "unchecked" })
181 public void generateWithAugmentationsTest() throws Exception {
182 final Class[] parameterTypes =
183 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
184 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
185 assertNotNull(generate);
186 generate.setAccessible(true);
188 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test-augment.yang");
189 final TypeProvider typeProvider = new TypeProviderImpl(context);
190 final Map<Module, ModuleContext> genCtx = mock(Map.class);
191 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
192 final ModuleContext moduleContext = new ModuleContext();
193 moduleContexts.add(moduleContext);
194 final QName create = QName.create("urn:test:simple:test", "2017-02-06", "my-cont");
195 final SchemaNode schemaNode = mock(SchemaNode.class);
196 when(schemaNode.getPath()).thenReturn(SchemaPath.create(true, create));
197 moduleContext.addChildNodeType(schemaNode, new GeneratedTypeBuilderImpl("test", "Test", moduleContext));
198 when(genCtx.values()).thenReturn(moduleContexts);
199 when(genCtx.get(context.getModules().iterator().next())).thenReturn(moduleContext);
200 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
202 final Object[] args =
203 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
204 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
205 assertNotNull(invoke);
208 @SuppressWarnings("rawtypes")
210 public void resolveAugmentationsNullModuleTest() throws Exception {
211 final Class[] parameterTypes = { Module.class, SchemaContext.class };
212 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
213 assertNotNull(generate);
214 generate.setAccessible(true);
216 final Module m = null;
217 final SchemaContext schemaContext = null;
219 final Object[] args = { m, schemaContext };
221 generate.invoke(AugmentToGenType.class, args);
223 } catch (final Exception e) {
225 assertTrue(e instanceof InvocationTargetException);
226 final Throwable cause = e.getCause();
227 assertNotNull(cause);
228 assertTrue(cause instanceof IllegalArgumentException);
229 assertEquals("Module reference cannot be NULL.", cause.getMessage());
233 @SuppressWarnings("rawtypes")
235 public void resolveAugmentationsNullAugmentationsTest() throws Exception {
236 final Class[] parameterTypes = { Module.class, SchemaContext.class };
237 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
238 assertNotNull(generate);
239 generate.setAccessible(true);
241 final Module m = mock(Module.class);
242 when(m.getAugmentations()).thenReturn(null);
243 final SchemaContext schemaContext = mock(SchemaContext.class);
245 final Object[] args = { m, schemaContext };
247 generate.invoke(AugmentToGenType.class, args);
249 } catch (final Exception e) {
251 assertTrue(e instanceof InvocationTargetException);
252 final Throwable cause = e.getCause();
253 assertNotNull(cause);
254 assertTrue(cause instanceof IllegalStateException);
255 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
259 @SuppressWarnings({ "rawtypes", "unchecked" })
261 public void resolveAugmentationsTest() throws Exception {
262 final Class[] parameterTypes = { Module.class, SchemaContext.class };
263 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
264 assertNotNull(generate);
265 generate.setAccessible(true);
267 final Module m = mock(Module.class);
268 final Module m2 = mock(Module.class);
269 final SchemaContext schemaContext = mock(SchemaContext.class);
271 final Set<AugmentationSchema> augmentations = new HashSet<>();
273 final QName q1 = QName.create("q1", "2017-04-04", "q1");
274 final QName q2 = QName.create("q2", "2017-04-04", "q2");
275 final QName q3 = QName.create("q3", "2017-04-04", "q3");
276 final QName q4 = QName.create("q4", "2017-04-04", "q4");
277 final QName q5 = QName.create("q5", "2017-04-04", "q5");
279 final AugmentationSchema augmentationSchema1 = mock(AugmentationSchema.class);
280 when(augmentationSchema1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
281 final AugmentationSchema augmentationSchema2 = mock(AugmentationSchema.class);
282 when(augmentationSchema2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
283 augmentations.add(augmentationSchema1);
284 augmentations.add(augmentationSchema2);
286 when(m.getAugmentations()).thenReturn(augmentations);
287 when(schemaContext.findModuleByNamespaceAndRevision(q1.getNamespace(), q1.getRevision())).thenReturn(m2);
288 when(schemaContext.findModuleByNamespaceAndRevision(q3.getNamespace(), q3.getRevision())).thenReturn(m2);
290 final Object[] args = { m, schemaContext };
292 final List<AugmentationSchema> result =
293 (List<AugmentationSchema>) generate.invoke(AugmentToGenType.class, args);
294 assertNotNull(result);
295 assertTrue(!result.isEmpty());
296 assertEquals(result.get(0), augmentationSchema1);
297 assertEquals(result.get(1), augmentationSchema2);
300 @SuppressWarnings({ "rawtypes" })
302 public void augmentationToGenTypesNullPckgNameTest() throws Exception {
303 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
304 boolean.class, Map.class, Map.class, TypeProvider.class };
305 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
306 assertNotNull(generate);
307 generate.setAccessible(true);
309 final String augmPackName = null;
310 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
311 final SchemaContext context = null;
312 final TypeProvider typeProvider = null;
313 final Map<Module, ModuleContext> genCtx = new HashMap<>();
314 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
315 final Module m = null;
317 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
319 generate.invoke(AugmentToGenType.class, args);
321 } catch (final Exception e) {
323 assertTrue(e instanceof InvocationTargetException);
324 final Throwable cause = e.getCause();
325 assertNotNull(cause);
326 assertTrue(cause instanceof IllegalArgumentException);
327 assertEquals("Package Name cannot be NULL.", cause.getMessage());
331 @SuppressWarnings({ "rawtypes" })
333 public void augmentationToGenTypesNullAugSchemaTest() throws Exception {
334 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
335 boolean.class, Map.class, Map.class, TypeProvider.class };
336 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
337 assertNotNull(generate);
338 generate.setAccessible(true);
340 final String augmPackName = "pckg.name";
341 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
342 final SchemaContext context = null;
343 final TypeProvider typeProvider = null;
344 final Map<Module, ModuleContext> genCtx = new HashMap<>();
345 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
346 final Module m = null;
348 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
350 generate.invoke(AugmentToGenType.class, args);
352 } catch (final Exception e) {
354 assertTrue(e instanceof InvocationTargetException);
355 final Throwable cause = e.getCause();
356 assertNotNull(cause);
357 assertTrue(cause instanceof IllegalArgumentException);
358 assertEquals("Augmentation List Entry cannot be NULL.", cause.getMessage());
362 @SuppressWarnings({ "rawtypes" })
364 public void augmentationToGenTypesNullAugSchemaTargetPathTest() throws Exception {
365 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
366 boolean.class, Map.class, Map.class, TypeProvider.class };
367 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
368 assertNotNull(generate);
369 generate.setAccessible(true);
371 final String augmPackName = "pckg.name";
372 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
373 when(augmSchema.getTargetPath()).thenReturn(null);
375 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
376 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
377 when(schemaPathAugmentListEntry.getKey()).thenReturn(null);
378 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
380 final SchemaContext context = null;
381 final TypeProvider typeProvider = null;
382 final Map<Module, ModuleContext> genCtx = new HashMap<>();
383 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
384 final Module m = null;
386 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
388 generate.invoke(AugmentToGenType.class, args);
390 } catch (final Exception e) {
392 assertTrue(e instanceof InvocationTargetException);
393 final Throwable cause = e.getCause();
394 assertNotNull(cause);
395 assertTrue(cause instanceof IllegalStateException);
396 assertEquals("Augmentation List Entry does not contain Target Path (Target Path is NULL).", cause.getMessage());
400 @SuppressWarnings({ "rawtypes" })
402 public void augmentationToGenTypesNullAugSchemaListTest() throws Exception {
403 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
404 boolean.class, Map.class, Map.class, TypeProvider.class };
405 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
406 assertNotNull(generate);
407 generate.setAccessible(true);
409 final String augmPackName = "pckg.name";
410 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
411 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
412 final SchemaPath path = SchemaPath.create(true, qnamePath);
413 when(augmSchema.getTargetPath()).thenReturn(path);
415 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
416 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
417 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
418 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
420 final SchemaContext context = null;
421 final TypeProvider typeProvider = null;
422 final Map<Module, ModuleContext> genCtx = new HashMap<>();
423 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
424 final Module m = null;
426 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
428 generate.invoke(AugmentToGenType.class, args);
430 } catch (final Exception e) {
432 assertTrue(e instanceof InvocationTargetException);
433 final Throwable cause = e.getCause();
434 assertNotNull(cause);
435 assertTrue(cause instanceof IllegalStateException);
436 assertEquals("Augmentation List cannot be empty.", cause.getMessage());
440 @SuppressWarnings({ "rawtypes" })
442 public void augmentationToGenTypesNullAugSchemaTargetNodeTest() throws Exception {
443 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
444 boolean.class, Map.class, Map.class, TypeProvider.class };
445 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
446 assertNotNull(generate);
447 generate.setAccessible(true);
449 final String augmPackName = "pckg.name";
451 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
452 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
453 final SchemaPath path = SchemaPath.create(true, qnamePath);
454 when(augmSchema.getTargetPath()).thenReturn(path);
455 final Set<UsesNode> uses = new HashSet<>();
456 when(augmSchema.getUses()).thenReturn(uses);
458 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
459 augmentationSchemaList.add(augmSchema);
460 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
461 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
462 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
464 final SchemaContext context = mock(SchemaContext.class);
465 final Module moduleAug = mock(Module.class);
466 final DataSchemaNode schNode = null;
467 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
468 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
469 .thenReturn(moduleAug);
471 final TypeProvider typeProvider = null;
472 final Map<Module, ModuleContext> genCtx = new HashMap<>();
473 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
475 final Module m = mock(Module.class);
476 when(m.getName()).thenReturn("augm-module");
477 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
478 when(m.getRevision()).thenReturn(qnamePath.getRevision());
480 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
482 generate.invoke(AugmentToGenType.class, args);
484 } catch (final Exception e) {
486 assertTrue(e instanceof InvocationTargetException);
487 final Throwable cause = e.getCause();
488 assertNotNull(cause);
489 assertTrue(cause instanceof IllegalArgumentException);
490 assertEquals("augment target not found: " + path, cause.getMessage());
494 @SuppressWarnings({ "rawtypes" })
496 public void augmentationToGenTypesNullAugTargetGTBTest() throws Exception {
497 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
498 boolean.class, Map.class, Map.class, TypeProvider.class };
499 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
500 assertNotNull(generate);
501 generate.setAccessible(true);
503 final String augmPackName = "pckg.name";
505 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
506 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
507 final SchemaPath path = SchemaPath.create(true, qnamePath);
508 when(augmSchema.getTargetPath()).thenReturn(path);
509 final Set<UsesNode> uses = new HashSet<>();
510 when(augmSchema.getUses()).thenReturn(uses);
512 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
513 augmentationSchemaList.add(augmSchema);
514 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
515 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
516 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
518 final SchemaContext context = mock(SchemaContext.class);
519 final Module moduleAug = mock(Module.class);
520 final DataSchemaNode schNode = mock(DataSchemaNode.class);
521 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
522 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
523 .thenReturn(moduleAug);
525 final TypeProvider typeProvider = null;
526 final Map<Module, ModuleContext> genCtx = new HashMap<>();
527 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
529 final Module m = mock(Module.class);
530 when(m.getName()).thenReturn("augm-module");
531 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
532 when(m.getRevision()).thenReturn(qnamePath.getRevision());
534 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
536 generate.invoke(AugmentToGenType.class, args);
538 } catch (final Exception e) {
540 assertTrue(e instanceof InvocationTargetException);
541 final Throwable cause = e.getCause();
542 assertNotNull(cause);
543 assertTrue(cause instanceof NullPointerException);
544 assertEquals("Target type not yet generated: " + schNode, cause.getMessage());
548 @SuppressWarnings({ "rawtypes", "unchecked" })
550 public void augmentationToGenTypesAugUsesNullOrigTargetTest() throws Exception {
551 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
552 boolean.class, Map.class, Map.class, TypeProvider.class };
553 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
554 assertNotNull(generate);
555 generate.setAccessible(true);
557 final String augmPackName = "pckg.name";
559 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
560 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
561 final SchemaPath path = SchemaPath.create(true, qnamePath);
562 when(augmSchema.getTargetPath()).thenReturn(path);
563 final Set<UsesNode> uses = new HashSet<>();
564 when(augmSchema.getUses()).thenReturn(uses);
566 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
567 augmentationSchemaList.add(augmSchema);
568 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
569 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
570 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
572 final SchemaContext context = mock(SchemaContext.class);
573 final Module moduleAug = mock(Module.class);
574 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
575 when(targetSchNode.getPath()).thenReturn(path);
576 when(targetSchNode.isAddedByUses()).thenReturn(true);
577 final Optional optionalSchemaNode = Optional.absent();
578 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
579 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
580 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
581 .thenReturn(moduleAug);
583 final TypeProvider typeProvider = null;
584 final Map<Module, ModuleContext> genCtx = new HashMap<>();
585 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
587 final Module m = mock(Module.class);
588 when(m.getName()).thenReturn("augm-module");
589 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
590 when(m.getRevision()).thenReturn(qnamePath.getRevision());
592 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
594 generate.invoke(AugmentToGenType.class, args);
596 } catch (final Exception e) {
598 assertTrue(e instanceof InvocationTargetException);
599 final Throwable cause = e.getCause();
600 assertNotNull(cause);
601 assertTrue(cause instanceof IllegalStateException);
602 assertEquals("Failed to find target node from grouping in augmentation " + augmSchema + " in module "
603 + m.getName(), cause.getMessage());
607 @SuppressWarnings({ "rawtypes" })
609 public void augmentationToGenTypesTargetChoicSchemaNodeTest() throws Exception {
610 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
611 boolean.class, Map.class, Map.class, TypeProvider.class };
612 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
613 assertNotNull(generate);
614 generate.setAccessible(true);
616 final String augmPackName = "pckg.name";
618 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
619 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
620 final SchemaPath path = SchemaPath.create(true, qnamePath);
621 when(augmSchema.getTargetPath()).thenReturn(path);
622 final Set<UsesNode> uses = new HashSet<>();
623 when(augmSchema.getUses()).thenReturn(uses);
625 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
626 augmentationSchemaList.add(augmSchema);
627 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
628 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
629 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
631 final SchemaContext context = mock(SchemaContext.class);
632 final Module moduleAug = mock(Module.class);
633 final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
634 when(targetSchNode.getPath()).thenReturn(path);
635 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
636 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
637 .thenReturn(moduleAug);
639 final TypeProvider typeProvider = null;
641 final Map genCtx = mock(Map.class);
642 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
643 final ModuleContext mc = new ModuleContext();
644 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm", mc);
645 mc.addChildNodeType(targetSchNode, gtb);
646 moduleContexts.add(mc);
647 when(genCtx.values()).thenReturn(moduleContexts);
649 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
651 final Module m = mock(Module.class);
652 when(m.getName()).thenReturn("augm-module");
653 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
654 when(m.getRevision()).thenReturn(qnamePath.getRevision());
656 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
657 final Map result = (Map) generate.invoke(AugmentToGenType.class, args);
658 assertNotNull(result);
661 @SuppressWarnings({ "rawtypes", "unchecked" })
663 public void augmentationToGenTypesTest() throws Exception {
664 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
665 boolean.class, Map.class, Map.class, TypeProvider.class };
666 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
667 assertNotNull(generate);
668 generate.setAccessible(true);
670 final String augmPackName = "pckg.name";
672 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
673 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
674 final SchemaPath path = SchemaPath.create(true, qnamePath);
675 when(augmSchema.getTargetPath()).thenReturn(path);
676 final Set<UsesNode> uses = new HashSet<>();
677 when(augmSchema.getUses()).thenReturn(uses);
678 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
679 when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
681 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
682 augmentationSchemaList.add(augmSchema);
683 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
684 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
685 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
687 final SchemaContext context = mock(SchemaContext.class);
688 final Module moduleAug = mock(Module.class);
689 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
690 when(targetSchNode.getPath()).thenReturn(path);
691 when(targetSchNode.isAddedByUses()).thenReturn(true);
692 final DataSchemaNode origSchNode = mock(DataSchemaNode.class);
693 when(origSchNode.getPath()).thenReturn(path);
694 when(origSchNode.isAddedByUses()).thenReturn(true);
695 when(origSchNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
696 final Optional optionalSchemaNode = Optional.of(origSchNode);
697 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
698 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
699 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
700 .thenReturn(moduleAug);
702 final TypeProvider typeProvider = null;
704 final Map<Module, ModuleContext> genCtx = new HashMap<>();
706 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
707 final ModuleContext mc = new ModuleContext();
708 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm", mc);
709 mc.addChildNodeType(targetSchNode, gtb);
710 moduleContexts.add(mc);
711 genCtx.put(moduleAug, mc);
713 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
715 when(moduleAug.getName()).thenReturn("augm-module");
716 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
717 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
719 final Object[] args =
720 { augmPackName, schemaPathAugmentListEntry, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
721 final Map<Module, ModuleContext> result =
722 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
723 assertNotNull(result);
724 final ModuleContext moduleContext = result.get(moduleAug);
725 assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
726 assertEquals("pckg.name.data", moduleContext.getAugmentations().get(0).getPackageName());
727 assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
728 assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
733 public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
735 AugmentToGenType.usesAugmentationToGenTypes(null, null, null, null, null, null, null, null, false, null, null);
736 } catch (final Exception e) {
738 assertTrue(e instanceof IllegalArgumentException);
739 assertEquals(e.getMessage(), "Package Name cannot be NULL.");
745 public void usesAugmentationToGenTypesNullAugSchemaListEntryTest() throws Exception {
747 AugmentToGenType.usesAugmentationToGenTypes(null, "", null, null, null, null, null, null, false, null, null);
748 } catch (final Exception e) {
750 assertTrue(e instanceof IllegalArgumentException);
751 assertEquals(e.getMessage(), "Augmentation Schema List Entry cannot be NULL.");
757 public void usesAugmentationToGenTypesEmptyAugSchemaListTest() throws Exception {
758 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
760 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
762 } catch (final Exception e) {
764 assertTrue(e instanceof IllegalStateException);
765 assertEquals(e.getMessage(), "Augmentation Schema List cannot be empty");
771 public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
772 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
773 when(augmentationSchema.getTargetPath()).thenReturn(null);
774 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
775 augmentationSchemaList.add(augmentationSchema);
777 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
779 } catch (final Exception e) {
781 assertTrue(e instanceof IllegalStateException);
782 assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
788 public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
789 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
790 final SchemaPath path = SchemaPath.create(true, qnamePath);
791 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
792 when(augmentationSchema.getTargetPath()).thenReturn(path);
793 final Set<UsesNode> uses = new HashSet<>();
794 when(augmentationSchema.getUses()).thenReturn(uses);
796 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
797 augmentationSchemaList.add(augmentationSchema);
799 final SchemaContext context = mock(SchemaContext.class);
800 final Module moduleAug = mock(Module.class);
801 when(moduleAug.getName()).thenReturn("augm-module");
802 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
803 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
804 final Set<GroupingDefinition> groupings = new HashSet<>();
805 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
806 when(groupingDefinition.getQName()).thenReturn(qnamePath);
807 groupings.add(groupingDefinition);
808 when(moduleAug.getGroupings()).thenReturn(groupings);
810 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
811 .thenReturn(moduleAug);
813 final Map<Module, ModuleContext> genCtx = new HashMap<>();
814 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
817 final UsesNode usesNode = mock(UsesNode.class);
818 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
820 when(usesNode.getGroupingPath()).thenReturn(path);
823 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
824 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
825 } catch (final Exception e) {
827 assertTrue(e instanceof IllegalArgumentException);
828 assertEquals(e.getMessage(), "augment target not found: " + path);
834 public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
835 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
836 final SchemaPath path = SchemaPath.create(true, qnamePath);
837 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
838 when(augmentationSchema.getTargetPath()).thenReturn(path);
839 final Set<UsesNode> uses = new HashSet<>();
840 when(augmentationSchema.getUses()).thenReturn(uses);
842 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
843 augmentationSchemaList.add(augmentationSchema);
845 final SchemaContext context = mock(SchemaContext.class);
846 final Module moduleAug = mock(Module.class);
847 when(moduleAug.getName()).thenReturn("augm-module");
848 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
849 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
850 final Set<GroupingDefinition> groupings = new HashSet<>();
851 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
852 when(groupingDefinition.getQName()).thenReturn(qnamePath);
853 final DataSchemaNode schNode = mock(DataSchemaNode.class);
854 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
855 groupings.add(groupingDefinition);
856 when(moduleAug.getGroupings()).thenReturn(groupings);
858 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
859 .thenReturn(moduleAug);
861 final Map<Module, ModuleContext> genCtx = new HashMap<>();
862 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
864 final UsesNode usesNode = mock(UsesNode.class);
865 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
867 when(usesNode.getGroupingPath()).thenReturn(path);
870 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
871 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
872 } catch (final Exception e) {
874 assertTrue(e instanceof NullPointerException);
875 assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
881 public void usesAugmentationToGenTypesTest() throws Exception {
882 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
883 final SchemaPath path = SchemaPath.create(true, qnamePath);
884 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
885 when(augmentationSchema.getTargetPath()).thenReturn(path);
886 final Set<UsesNode> uses = new HashSet<>();
887 when(augmentationSchema.getUses()).thenReturn(uses);
889 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
890 augmentationSchemaList.add(augmentationSchema);
892 final SchemaContext context = mock(SchemaContext.class);
893 final Module moduleAug = mock(Module.class);
894 when(moduleAug.getName()).thenReturn("augm-module");
895 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
896 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
897 final Set<GroupingDefinition> groupings = new HashSet<>();
898 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
899 when(groupingDefinition.getQName()).thenReturn(qnamePath);
900 final DataSchemaNode schNode = mock(DataSchemaNode.class);
901 when(schNode.getPath()).thenReturn(path);
902 when(schNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
903 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
904 groupings.add(groupingDefinition);
905 when(moduleAug.getGroupings()).thenReturn(groupings);
907 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
908 .thenReturn(moduleAug);
910 final Map<Module, ModuleContext> genCtx = new HashMap<>();
911 final ModuleContext mc = new ModuleContext();
912 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm", mc);
913 mc.addChildNodeType(schNode, gtb);
914 genCtx.put(moduleAug, mc);
916 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
918 final UsesNode usesNode = mock(UsesNode.class);
919 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
921 when(usesNode.getGroupingPath()).thenReturn(path);
923 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
924 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
925 BindingNamespaceType.Data);
926 assertNotNull(result);
931 public void usesAugmentationToGenTypesChoiceTest() throws Exception {
932 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
933 final SchemaPath path = SchemaPath.create(true, qnamePath);
934 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
935 when(augmentationSchema.getTargetPath()).thenReturn(path);
936 final Set<UsesNode> uses = new HashSet<>();
937 when(augmentationSchema.getUses()).thenReturn(uses);
939 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
940 augmentationSchemaList.add(augmentationSchema);
942 final SchemaContext context = mock(SchemaContext.class);
943 final Module moduleAug = mock(Module.class);
944 when(moduleAug.getName()).thenReturn("augm-module");
945 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
946 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
947 final Set<GroupingDefinition> groupings = new HashSet<>();
948 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
949 when(groupingDefinition.getQName()).thenReturn(qnamePath);
950 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
951 when(schNode.getPath()).thenReturn(path);
952 when(schNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
953 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
954 groupings.add(groupingDefinition);
955 when(moduleAug.getGroupings()).thenReturn(groupings);
957 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
958 .thenReturn(moduleAug);
960 final Map<Module, ModuleContext> genCtx = new HashMap<>();
961 final ModuleContext mc = new ModuleContext();
962 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm", mc);
963 mc.addChildNodeType(schNode, gtb);
964 genCtx.put(moduleAug, mc);
966 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
968 final UsesNode usesNode = mock(UsesNode.class);
969 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
971 when(usesNode.getGroupingPath()).thenReturn(path);
973 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
974 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
975 BindingNamespaceType.Data);
976 assertNotNull(result);
979 @SuppressWarnings({ "rawtypes" })
981 public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
982 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
983 final Method generate =
984 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
985 assertNotNull(generate);
986 generate.setAccessible(true);
988 final Module module = mock(Module.class);
989 final QName qnamePath = QName.create("test", "2017-04-04", "test");
990 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
991 final DataSchemaNode schNode = mock(DataSchemaNode.class);
992 when(schNode.getPath()).thenReturn(schemaPath);
993 when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
995 final SchemaContext context = mock(SchemaContext.class);
996 when(context.findModuleByNamespaceAndRevision(qnamePath .getNamespace(), qnamePath.getRevision()))
998 final UsesNode usesNode = mock(UsesNode.class);
999 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1001 final Object[] args = { context, schemaPath, usesNode };
1003 generate.invoke(AugmentToGenType.class, args);
1005 } catch (final Exception e) {
1007 assertTrue(e instanceof InvocationTargetException);
1008 final Throwable cause = e.getCause();
1009 assertNotNull(cause);
1010 assertTrue(cause instanceof IllegalArgumentException);
1011 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1015 @SuppressWarnings({ "rawtypes" })
1017 public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
1018 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1019 final Method generate =
1020 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1021 assertNotNull(generate);
1022 generate.setAccessible(true);
1024 final Module module = mock(Module.class);
1025 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1026 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1027 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1028 when(schNode.getPath()).thenReturn(schemaPath);
1029 when(schNode.isAddedByUses()).thenReturn(true);
1030 final Set<GroupingDefinition> groupings = new HashSet<>();
1031 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1032 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1033 groupings.add(groupingDefinition);
1034 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1035 when(module.getGroupings()).thenReturn(groupings);
1037 final SchemaContext context = mock(SchemaContext.class);
1038 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1039 .thenReturn(module);
1040 final UsesNode usesNode = mock(UsesNode.class);
1041 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1043 final Object[] args = { context, schemaPath, usesNode };
1045 generate.invoke(AugmentToGenType.class, args);
1047 } catch (final Exception e) {
1049 assertTrue(e instanceof InvocationTargetException);
1050 final Throwable cause = e.getCause();
1051 assertNotNull(cause);
1052 assertTrue(cause instanceof IllegalStateException);
1053 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1057 @SuppressWarnings({ "rawtypes" })
1059 public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
1060 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1061 final Method generate =
1062 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1063 assertNotNull(generate);
1064 generate.setAccessible(true);
1066 final Module module = mock(Module.class);
1067 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1068 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1069 final DataSchemaNode schNode = null;
1070 final Set<GroupingDefinition> groupings = new HashSet<>();
1071 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1072 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1073 groupings.add(groupingDefinition);
1074 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1075 when(module.getGroupings()).thenReturn(groupings);
1077 final SchemaContext context = mock(SchemaContext.class);
1078 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1079 .thenReturn(module);
1080 final UsesNode usesNode = mock(UsesNode.class);
1081 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1083 final Object[] args = { context, schemaPath, usesNode };
1084 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1085 assertEquals(null, result);
1088 @SuppressWarnings({ "rawtypes", "unchecked" })
1090 public void findOriginalTargetFromGroupingTest() throws Exception {
1091 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1092 final Method generate =
1093 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1094 assertNotNull(generate);
1095 generate.setAccessible(true);
1097 final Module module = mock(Module.class);
1098 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1099 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1100 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1101 when(schNode.getPath()).thenReturn(schemaPath);
1102 final Set<GroupingDefinition> groupings = new HashSet<>();
1103 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1104 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1105 groupings.add(groupingDefinition);
1106 final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
1107 when(derivSchNode.isAddedByUses()).thenReturn(true);
1108 final Optional optional = Optional.of(schNode);
1109 when(derivSchNode.getOriginal()).thenReturn(optional);
1110 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
1111 when(module.getGroupings()).thenReturn(groupings);
1113 final SchemaContext context = mock(SchemaContext.class);
1114 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1115 .thenReturn(module);
1116 final UsesNode usesNode = mock(UsesNode.class);
1117 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1119 final Object[] args = { context, schemaPath, usesNode };
1120 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1121 assertEquals(schNode, result);
1124 @SuppressWarnings({ "rawtypes" })
1126 public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1127 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1128 final Method generate =
1129 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1130 assertNotNull(generate);
1131 generate.setAccessible(true);
1133 final Module module = mock(Module.class);
1134 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1135 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1136 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1137 when(schNode.getPath()).thenReturn(schemaPath);
1138 final Set<GroupingDefinition> groupings = new HashSet<>();
1139 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1140 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1141 groupings.add(groupingDefinition);
1142 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1143 when(module.getGroupings()).thenReturn(groupings);
1145 final SchemaContext context = mock(SchemaContext.class);
1146 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1147 .thenReturn(module);
1148 final UsesNode usesNode = mock(UsesNode.class);
1149 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1151 final Object[] args = { context, schemaPath, usesNode };
1152 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1153 assertEquals(schNode, result);
1156 @SuppressWarnings({ "rawtypes" })
1158 public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1159 final Class[] parameterTypes =
1160 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1161 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1162 BindingNamespaceType.class};
1163 final Method generate =
1164 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1165 assertNotNull(generate);
1166 generate.setAccessible(true);
1168 final SchemaContext schemaContext = null;
1169 final Module module = null;
1170 final String pckgName = null;
1171 final Type targetType = null;
1172 final ChoiceSchemaNode targetNode = null;
1173 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1174 final DataNodeContainer usesNodeParent = null;
1175 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1176 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1178 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1179 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1181 generate.invoke(AugmentToGenType.class, args);
1183 } catch (final Exception e) {
1185 assertTrue(e instanceof InvocationTargetException);
1186 final Throwable cause = e.getCause();
1187 assertNotNull(cause);
1188 assertTrue(cause instanceof IllegalArgumentException);
1189 assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1193 @SuppressWarnings({ "rawtypes" })
1195 public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1196 final Class[] parameterTypes =
1197 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1198 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1199 BindingNamespaceType.class };
1200 final Method generate =
1201 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1202 assertNotNull(generate);
1203 generate.setAccessible(true);
1205 final SchemaContext schemaContext = null;
1206 final Module module = null;
1207 final String pckgName = "";
1208 final Type targetType = null;
1209 final ChoiceSchemaNode targetNode = null;
1210 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1211 final DataNodeContainer usesNodeParent = null;
1212 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1213 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1215 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1216 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1218 generate.invoke(AugmentToGenType.class, args);
1220 } catch (final Exception e) {
1222 assertTrue(e instanceof InvocationTargetException);
1223 final Throwable cause = e.getCause();
1224 assertNotNull(cause);
1225 assertTrue(cause instanceof IllegalArgumentException);
1226 assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1230 @SuppressWarnings({ "rawtypes" })
1232 public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1233 final Class[] parameterTypes =
1234 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1235 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1236 BindingNamespaceType.class };
1237 final Method generate =
1238 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1239 assertNotNull(generate);
1240 generate.setAccessible(true);
1242 final SchemaContext schemaContext = null;
1243 final Module module = null;
1244 final String pckgName = "";
1245 final Type targetType = mock(Type.class);
1246 final ChoiceSchemaNode targetNode = null;
1247 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1248 final DataNodeContainer usesNodeParent = null;
1249 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1250 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1252 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1253 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1255 generate.invoke(AugmentToGenType.class, args);
1257 } catch (final Exception e) {
1259 assertTrue(e instanceof InvocationTargetException);
1260 final Throwable cause = e.getCause();
1261 assertNotNull(cause);
1262 assertTrue(cause instanceof IllegalArgumentException);
1263 assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1267 @SuppressWarnings({ "rawtypes", "unchecked" })
1269 public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1270 final Class[] parameterTypes =
1271 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1272 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1273 BindingNamespaceType.class };
1274 final Method generate =
1275 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1276 assertNotNull(generate);
1277 generate.setAccessible(true);
1279 final SchemaContext schemaContext = null;
1280 final Module module = null;
1281 final String pckgName = "";
1282 final Type targetType = mock(Type.class);
1283 final ChoiceSchemaNode targetNode = null;
1284 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1285 final DataSchemaNode caseNode = null;
1286 augmentNodes.add(caseNode);
1288 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1289 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1290 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1291 schemaPathAugmentListEntry.add(augmentationSchema);
1293 final DataNodeContainer usesNodeParent = null;
1294 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1295 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1297 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1298 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1299 final Map<Module, ModuleContext> result =
1300 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1301 assertEquals(genCtx, result);
1304 @SuppressWarnings({ "rawtypes" })
1306 public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1307 final Class[] parameterTypes =
1308 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1309 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1310 BindingNamespaceType.class };
1311 final Method generate =
1312 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1313 assertNotNull(generate);
1314 generate.setAccessible(true);
1316 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1317 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1318 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1320 final SchemaContext schemaContext = mock(SchemaContext.class);
1321 final Module module = mock(Module.class);
1322 when(module.getName()).thenReturn("test-module-case");
1323 final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1324 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1325 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1326 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1327 final String pckgName = "test.augment.choice.cases";
1328 final Type targetType = mock(Type.class);
1329 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1330 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1331 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1332 when(caseNode.getPath()).thenReturn(path);
1333 when(caseNode.getQName()).thenReturn(qnamePath);
1334 augmentNodes.add(caseNode);
1336 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1337 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1338 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1339 schemaPathAugmentListEntry.add(augmentationSchema);
1341 final DataNodeContainer usesNodeParent = null;
1342 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1343 when(targetNode.getPath()).thenReturn(path);
1344 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1345 genCtx.put(module, new ModuleContext());
1346 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment", genCtx.get(module));
1347 genCtx.get(module).addCaseType(path, gtb);
1348 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1350 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1351 .thenReturn(module);
1353 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1354 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1356 generate.invoke(AugmentToGenType.class, args);
1358 } catch (final Exception e) {
1360 assertTrue(e instanceof InvocationTargetException);
1361 final Throwable cause = e.getCause();
1362 assertNotNull(cause);
1363 assertTrue(cause instanceof IllegalArgumentException);
1364 assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1368 @SuppressWarnings({ "rawtypes", "unchecked" })
1370 public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1371 final Class[] parameterTypes =
1372 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1373 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1374 BindingNamespaceType.class };
1375 final Method generate =
1376 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1377 assertNotNull(generate);
1378 generate.setAccessible(true);
1380 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1381 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1382 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1384 final SchemaContext schemaContext = mock(SchemaContext.class);
1385 final Module module = mock(Module.class);
1386 when(module.getName()).thenReturn("test-module-case");
1387 final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1388 when(schemaNode.getPath()).thenReturn(path);
1389 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1390 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1391 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1392 final String pckgName = "test.augment.choice.cases";
1393 final Type targetType = mock(Type.class);
1394 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1395 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1396 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1397 when(caseNode.getPath()).thenReturn(path);
1398 when(caseNode.getQName()).thenReturn(qnamePath);
1399 augmentNodes.add(caseNode);
1401 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1402 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1403 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1404 schemaPathAugmentListEntry.add(augmentationSchema);
1406 final DataNodeContainer usesNodeParent = null;
1407 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1408 when(targetNode.getPath()).thenReturn(path);
1409 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1410 final ModuleContext moduleContext = new ModuleContext();
1411 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment", moduleContext);
1412 moduleContext.addCaseType(path, gtb);
1413 genCtx.put(module, moduleContext);
1414 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1416 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1417 .thenReturn(module);
1419 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1420 genCtx, false, genTypeBuilder, null ,BindingNamespaceType.Data };
1421 final Map<Module, ModuleContext> result =
1422 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1423 assertNotNull(result);
1424 assertEquals(result.get(module), moduleContext);