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.spi.TypeProvider;
30 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
31 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
32 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
33 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
37 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
42 import org.opendaylight.yangtools.yang.model.api.Module;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.UsesNode;
48 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
50 public class AugmentToGenTypeTest {
52 @SuppressWarnings("unchecked")
53 @Test(expected = UnsupportedOperationException.class)
54 public void constructorTest() throws Throwable {
55 final Constructor<AugmentToGenType> constructor =
56 (Constructor<AugmentToGenType>) AugmentToGenType.class.getDeclaredConstructors()[0];
57 constructor.setAccessible(true);
58 final Object[] objs = {};
60 constructor.newInstance(objs);
61 } catch (final Exception e) {
66 @SuppressWarnings("rawtypes")
68 public void generateNullModuleTest() throws Exception {
69 final Class[] parameterTypes =
70 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
71 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
72 assertNotNull(generate);
73 generate.setAccessible(true);
75 final SchemaContext context = null;
76 final TypeProvider typeProvider = null;
77 final Map<Module, ModuleContext> genCtx = new HashMap<>();
78 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
79 final Module m = null;
81 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
83 generate.invoke(AugmentToGenType.class, args);
85 } catch (final Exception e) {
87 assertTrue(e instanceof InvocationTargetException);
88 final Throwable cause = e.getCause();
90 assertTrue(cause instanceof IllegalArgumentException);
91 assertEquals("Module reference cannot be NULL.", cause.getMessage());
95 @SuppressWarnings("rawtypes")
97 public void generateNullModuleNameTest() throws Exception {
98 final Class[] parameterTypes =
99 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
100 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
101 assertNotNull(generate);
102 generate.setAccessible(true);
104 final SchemaContext context = null;
105 final TypeProvider typeProvider = null;
106 final Map<Module, ModuleContext> genCtx = new HashMap<>();
107 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
108 final Module m = mock(Module.class);
109 when(m.getName()).thenReturn(null);
111 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
113 generate.invoke(AugmentToGenType.class, args);
115 } catch (final Exception e) {
117 assertTrue(e instanceof InvocationTargetException);
118 final Throwable cause = e.getCause();
119 assertNotNull(cause);
120 assertTrue(cause instanceof IllegalArgumentException);
121 assertEquals("Module name cannot be NULL.", cause.getMessage());
125 @SuppressWarnings("rawtypes")
127 public void generateNullModuleAugmentationsTest() throws Exception {
128 final Class[] parameterTypes =
129 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
130 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
131 assertNotNull(generate);
132 generate.setAccessible(true);
134 final SchemaContext context = null;
135 final TypeProvider typeProvider = null;
136 final Map<Module, ModuleContext> genCtx = new HashMap<>();
137 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
138 final Module m = mock(Module.class);
139 when(m.getName()).thenReturn("name");
140 when(m.getAugmentations()).thenReturn(null);
142 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
144 generate.invoke(AugmentToGenType.class, args);
146 } catch (final Exception e) {
148 assertTrue(e instanceof InvocationTargetException);
149 final Throwable cause = e.getCause();
150 assertNotNull(cause);
151 assertTrue(cause instanceof IllegalStateException);
152 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
156 @SuppressWarnings("rawtypes")
158 public void generateWithoutAugmentationsTest() throws Exception {
159 final Class[] parameterTypes =
160 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
161 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
162 assertNotNull(generate);
163 generate.setAccessible(true);
165 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test.yang");
166 final TypeProvider typeProvider = new TypeProviderImpl(context);
167 final Map<Module, ModuleContext> genCtx = new HashMap<>();
168 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
170 final Object[] args =
171 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
172 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
173 assertNotNull(invoke);
176 @SuppressWarnings({ "rawtypes", "unchecked" })
178 public void generateWithAugmentationsTest() throws Exception {
179 final Class[] parameterTypes =
180 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
181 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
182 assertNotNull(generate);
183 generate.setAccessible(true);
185 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test-augment.yang");
186 final TypeProvider typeProvider = new TypeProviderImpl(context);
187 final Map<Module, ModuleContext> genCtx = mock(Map.class);
188 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
189 final ModuleContext moduleContext = new ModuleContext();
190 moduleContexts.add(moduleContext);
191 final QName create = QName.create("urn:test:simple:test", "2017-02-06", "my-cont");
192 final SchemaNode schemaNode = mock(SchemaNode.class);
193 when(schemaNode.getPath()).thenReturn(SchemaPath.create(true, create));
194 moduleContext.addChildNodeType(schemaNode, new GeneratedTypeBuilderImpl("test", "Test"));
195 when(genCtx.values()).thenReturn(moduleContexts);
196 when(genCtx.get(context.getModules().iterator().next())).thenReturn(moduleContext);
197 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
199 final Object[] args =
200 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
201 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
202 assertNotNull(invoke);
205 @SuppressWarnings("rawtypes")
207 public void resolveAugmentationsNullModuleTest() throws Exception {
208 final Class[] parameterTypes = { Module.class };
209 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
210 assertNotNull(generate);
211 generate.setAccessible(true);
213 final Module m = null;
215 final Object[] args = { m };
217 generate.invoke(AugmentToGenType.class, args);
219 } catch (final Exception e) {
221 assertTrue(e instanceof InvocationTargetException);
222 final Throwable cause = e.getCause();
223 assertNotNull(cause);
224 assertTrue(cause instanceof IllegalArgumentException);
225 assertEquals("Module reference cannot be NULL.", cause.getMessage());
229 @SuppressWarnings("rawtypes")
231 public void resolveAugmentationsNullAugmentationsTest() throws Exception {
232 final Class[] parameterTypes = { Module.class };
233 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
234 assertNotNull(generate);
235 generate.setAccessible(true);
237 final Module m = mock(Module.class);
238 when(m.getAugmentations()).thenReturn(null);
240 final Object[] args = { m };
242 generate.invoke(AugmentToGenType.class, args);
244 } catch (final Exception e) {
246 assertTrue(e instanceof InvocationTargetException);
247 final Throwable cause = e.getCause();
248 assertNotNull(cause);
249 assertTrue(cause instanceof IllegalStateException);
250 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
254 @SuppressWarnings({ "rawtypes", "unchecked" })
256 public void resolveAugmentationsTest() throws Exception {
257 final Class[] parameterTypes = { Module.class };
258 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
259 assertNotNull(generate);
260 generate.setAccessible(true);
262 final Module m = mock(Module.class);
264 final Set<AugmentationSchema> augmentations = new HashSet<>();
266 final QName q1 = QName.create("q1", "2017-04-04", "q1");
267 final QName q2 = QName.create("q2", "2017-04-04", "q2");
268 final QName q3 = QName.create("q3", "2017-04-04", "q3");
269 final QName q4 = QName.create("q4", "2017-04-04", "q4");
270 final QName q5 = QName.create("q5", "2017-04-04", "q5");
272 final AugmentationSchema augmentationSchema1 = mock(AugmentationSchema.class);
273 when(augmentationSchema1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
274 final AugmentationSchema augmentationSchema2 = mock(AugmentationSchema.class);
275 when(augmentationSchema2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
276 augmentations.add(augmentationSchema1);
277 augmentations.add(augmentationSchema2);
279 when(m.getAugmentations()).thenReturn(augmentations);
281 final Object[] args = { m };
283 final List<AugmentationSchema> result =
284 (List<AugmentationSchema>) generate.invoke(AugmentToGenType.class, args);
285 assertNotNull(result);
286 assertTrue(!result.isEmpty());
287 assertEquals(result.get(0), augmentationSchema1);
288 assertEquals(result.get(1), augmentationSchema2);
291 @SuppressWarnings({ "rawtypes" })
293 public void augmentationToGenTypesNullPckgNameTest() throws Exception {
294 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
295 boolean.class, Map.class, Map.class, TypeProvider.class };
296 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
297 assertNotNull(generate);
298 generate.setAccessible(true);
300 final String augmPackName = null;
301 final AugmentationSchema augmSchema = null;
302 final SchemaContext context = null;
303 final TypeProvider typeProvider = null;
304 final Map<Module, ModuleContext> genCtx = new HashMap<>();
305 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
306 final Module m = null;
308 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
310 generate.invoke(AugmentToGenType.class, args);
312 } catch (final Exception e) {
314 assertTrue(e instanceof InvocationTargetException);
315 final Throwable cause = e.getCause();
316 assertNotNull(cause);
317 assertTrue(cause instanceof IllegalArgumentException);
318 assertEquals("Package Name cannot be NULL.", cause.getMessage());
322 @SuppressWarnings({ "rawtypes" })
324 public void augmentationToGenTypesNullAugSchemaTest() throws Exception {
325 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
326 boolean.class, Map.class, Map.class, TypeProvider.class };
327 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
328 assertNotNull(generate);
329 generate.setAccessible(true);
331 final String augmPackName = "pckg.name";
332 final AugmentationSchema augmSchema = null;
333 final SchemaContext context = null;
334 final TypeProvider typeProvider = null;
335 final Map<Module, ModuleContext> genCtx = new HashMap<>();
336 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
337 final Module m = null;
339 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
341 generate.invoke(AugmentToGenType.class, args);
343 } catch (final Exception e) {
345 assertTrue(e instanceof InvocationTargetException);
346 final Throwable cause = e.getCause();
347 assertNotNull(cause);
348 assertTrue(cause instanceof IllegalArgumentException);
349 assertEquals("Augmentation Schema cannot be NULL.", cause.getMessage());
353 @SuppressWarnings({ "rawtypes" })
355 public void augmentationToGenTypesNullAugSchemaTargetPathTest() throws Exception {
356 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
357 boolean.class, Map.class, Map.class, TypeProvider.class };
358 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
359 assertNotNull(generate);
360 generate.setAccessible(true);
362 final String augmPackName = "pckg.name";
363 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
364 when(augmSchema.getTargetPath()).thenReturn(null);
366 final SchemaContext context = null;
367 final TypeProvider typeProvider = null;
368 final Map<Module, ModuleContext> genCtx = new HashMap<>();
369 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
370 final Module m = null;
372 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
374 generate.invoke(AugmentToGenType.class, args);
376 } catch (final Exception e) {
378 assertTrue(e instanceof InvocationTargetException);
379 final Throwable cause = e.getCause();
380 assertNotNull(cause);
381 assertTrue(cause instanceof IllegalStateException);
382 assertEquals("Augmentation Schema does not contain Target Path (Target Path is NULL).", cause.getMessage());
386 @SuppressWarnings({ "rawtypes" })
388 public void augmentationToGenTypesNullAugSchemaTargetNodeTest() throws Exception {
389 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
390 boolean.class, Map.class, Map.class, TypeProvider.class };
391 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
392 assertNotNull(generate);
393 generate.setAccessible(true);
395 final String augmPackName = "pckg.name";
397 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
398 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
399 final SchemaPath path = SchemaPath.create(true, qnamePath);
400 when(augmSchema.getTargetPath()).thenReturn(path);
401 final Set<UsesNode> uses = new HashSet<>();
402 when(augmSchema.getUses()).thenReturn(uses);
404 final SchemaContext context = mock(SchemaContext.class);
405 final Module moduleAug = mock(Module.class);
406 final DataSchemaNode schNode = null;
407 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
408 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
409 .thenReturn(moduleAug);
411 final TypeProvider typeProvider = null;
412 final Map<Module, ModuleContext> genCtx = new HashMap<>();
413 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
415 final Module m = mock(Module.class);
416 when(m.getName()).thenReturn("augm-module");
417 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
418 when(m.getRevision()).thenReturn(qnamePath.getRevision());
420 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
422 generate.invoke(AugmentToGenType.class, args);
424 } catch (final Exception e) {
426 assertTrue(e instanceof InvocationTargetException);
427 final Throwable cause = e.getCause();
428 assertNotNull(cause);
429 assertTrue(cause instanceof IllegalArgumentException);
430 assertEquals("augment target not found: " + path, cause.getMessage());
434 @SuppressWarnings({ "rawtypes" })
436 public void augmentationToGenTypesNullAugTargetGTBTest() throws Exception {
437 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
438 boolean.class, Map.class, Map.class, TypeProvider.class };
439 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
440 assertNotNull(generate);
441 generate.setAccessible(true);
443 final String augmPackName = "pckg.name";
445 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
446 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
447 final SchemaPath path = SchemaPath.create(true, qnamePath);
448 when(augmSchema.getTargetPath()).thenReturn(path);
449 final Set<UsesNode> uses = new HashSet<>();
450 when(augmSchema.getUses()).thenReturn(uses);
452 final SchemaContext context = mock(SchemaContext.class);
453 final Module moduleAug = mock(Module.class);
454 final DataSchemaNode schNode = mock(DataSchemaNode.class);
455 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
456 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
457 .thenReturn(moduleAug);
459 final TypeProvider typeProvider = null;
460 final Map<Module, ModuleContext> genCtx = new HashMap<>();
461 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
463 final Module m = mock(Module.class);
464 when(m.getName()).thenReturn("augm-module");
465 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
466 when(m.getRevision()).thenReturn(qnamePath.getRevision());
468 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
470 generate.invoke(AugmentToGenType.class, args);
472 } catch (final Exception e) {
474 assertTrue(e instanceof InvocationTargetException);
475 final Throwable cause = e.getCause();
476 assertNotNull(cause);
477 assertTrue(cause instanceof NullPointerException);
478 assertEquals("Target type not yet generated: " + schNode, cause.getMessage());
482 @SuppressWarnings({ "rawtypes", "unchecked" })
484 public void augmentationToGenTypesAugUsesNullOrigTargetTest() throws Exception {
485 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
486 boolean.class, Map.class, Map.class, TypeProvider.class };
487 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
488 assertNotNull(generate);
489 generate.setAccessible(true);
491 final String augmPackName = "pckg.name";
493 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
494 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
495 final SchemaPath path = SchemaPath.create(true, qnamePath);
496 when(augmSchema.getTargetPath()).thenReturn(path);
497 final Set<UsesNode> uses = new HashSet<>();
498 when(augmSchema.getUses()).thenReturn(uses);
500 final SchemaContext context = mock(SchemaContext.class);
501 final Module moduleAug = mock(Module.class);
502 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
503 when(targetSchNode.getPath()).thenReturn(path);
504 when(targetSchNode.isAddedByUses()).thenReturn(true);
505 final Optional optionalSchemaNode = Optional.absent();
506 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
507 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
508 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
509 .thenReturn(moduleAug);
511 final TypeProvider typeProvider = null;
512 final Map<Module, ModuleContext> genCtx = new HashMap<>();
513 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
515 final Module m = mock(Module.class);
516 when(m.getName()).thenReturn("augm-module");
517 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
518 when(m.getRevision()).thenReturn(qnamePath.getRevision());
520 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
522 generate.invoke(AugmentToGenType.class, args);
524 } catch (final Exception e) {
526 assertTrue(e instanceof InvocationTargetException);
527 final Throwable cause = e.getCause();
528 assertNotNull(cause);
529 assertTrue(cause instanceof IllegalStateException);
530 assertEquals("Failed to find target node from grouping in augmentation " + augmSchema + " in module "
531 + m.getName(), cause.getMessage());
535 @SuppressWarnings({ "rawtypes" })
537 public void augmentationToGenTypesTargetChoicSchemaNodeTest() throws Exception {
538 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
539 boolean.class, Map.class, Map.class, TypeProvider.class };
540 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
541 assertNotNull(generate);
542 generate.setAccessible(true);
544 final String augmPackName = "pckg.name";
546 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
547 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
548 final SchemaPath path = SchemaPath.create(true, qnamePath);
549 when(augmSchema.getTargetPath()).thenReturn(path);
550 final Set<UsesNode> uses = new HashSet<>();
551 when(augmSchema.getUses()).thenReturn(uses);
553 final SchemaContext context = mock(SchemaContext.class);
554 final Module moduleAug = mock(Module.class);
555 final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
556 when(targetSchNode.getPath()).thenReturn(path);
557 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
558 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
559 .thenReturn(moduleAug);
561 final TypeProvider typeProvider = null;
563 final Map genCtx = mock(Map.class);
564 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
565 final ModuleContext mc = new ModuleContext();
566 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
567 mc.addChildNodeType(targetSchNode, gtb);
568 moduleContexts.add(mc);
569 when(genCtx.values()).thenReturn(moduleContexts);
571 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
573 final Module m = mock(Module.class);
574 when(m.getName()).thenReturn("augm-module");
575 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
576 when(m.getRevision()).thenReturn(qnamePath.getRevision());
578 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
579 final Map result = (Map) generate.invoke(AugmentToGenType.class, args);
580 assertNotNull(result);
583 @SuppressWarnings({ "rawtypes", "unchecked" })
585 public void augmentationToGenTypesTest() throws Exception {
586 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
587 boolean.class, Map.class, Map.class, TypeProvider.class };
588 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
589 assertNotNull(generate);
590 generate.setAccessible(true);
592 final String augmPackName = "pckg.name";
594 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
595 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
596 final SchemaPath path = SchemaPath.create(true, qnamePath);
597 when(augmSchema.getTargetPath()).thenReturn(path);
598 final Set<UsesNode> uses = new HashSet<>();
599 when(augmSchema.getUses()).thenReturn(uses);
600 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
601 when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
603 final SchemaContext context = mock(SchemaContext.class);
604 final Module moduleAug = mock(Module.class);
605 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
606 when(targetSchNode.getPath()).thenReturn(path);
607 when(targetSchNode.isAddedByUses()).thenReturn(true);
608 final DataSchemaNode origSchNode = mock(DataSchemaNode.class);
609 when(origSchNode.getPath()).thenReturn(path);
610 when(origSchNode.isAddedByUses()).thenReturn(true);
611 final Optional optionalSchemaNode = Optional.of(origSchNode);
612 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
613 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
614 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
615 .thenReturn(moduleAug);
617 final TypeProvider typeProvider = null;
619 final Map<Module, ModuleContext> genCtx = new HashMap<>();
621 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
622 final ModuleContext mc = new ModuleContext();
623 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
624 mc.addChildNodeType(targetSchNode, gtb);
625 moduleContexts.add(mc);
626 genCtx.put(moduleAug, mc);
628 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
630 when(moduleAug.getName()).thenReturn("augm-module");
631 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
632 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
634 final Object[] args =
635 { augmPackName, augmSchema, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
636 final Map<Module, ModuleContext> result =
637 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
638 assertNotNull(result);
639 final ModuleContext moduleContext = result.get(moduleAug);
640 assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
641 assertEquals("pckg.name", moduleContext.getAugmentations().get(0).getPackageName());
642 assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
643 assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
647 public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
649 AugmentToGenType.usesAugmentationToGenTypes(null, null, null, null, null, null, null, null, false, null);
650 } catch (final Exception e) {
652 assertTrue(e instanceof IllegalArgumentException);
653 assertEquals(e.getMessage(), "Package Name cannot be NULL.");
658 public void usesAugmentationToGenTypesNullAugSchemaNodeTest() throws Exception {
660 AugmentToGenType.usesAugmentationToGenTypes(null, "", null, null, null, null, null, null, false, null);
661 } catch (final Exception e) {
663 assertTrue(e instanceof IllegalArgumentException);
664 assertEquals(e.getMessage(), "Augmentation Schema cannot be NULL.");
669 public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
670 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
671 when(augmentationSchema.getTargetPath()).thenReturn(null);
673 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchema, null, null, null, null, null,
675 } catch (final Exception e) {
677 assertTrue(e instanceof IllegalStateException);
678 assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
683 public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
684 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
685 final SchemaPath path = SchemaPath.create(true, qnamePath);
686 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
687 when(augmentationSchema.getTargetPath()).thenReturn(path);
688 final Set<UsesNode> uses = new HashSet<>();
689 when(augmentationSchema.getUses()).thenReturn(uses);
691 final SchemaContext context = mock(SchemaContext.class);
692 final Module moduleAug = mock(Module.class);
693 when(moduleAug.getName()).thenReturn("augm-module");
694 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
695 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
696 final Set<GroupingDefinition> groupings = new HashSet<>();
697 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
698 when(groupingDefinition.getQName()).thenReturn(qnamePath);
699 groupings.add(groupingDefinition);
700 when(moduleAug.getGroupings()).thenReturn(groupings);
702 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
703 .thenReturn(moduleAug);
705 final Map<Module, ModuleContext> genCtx = new HashMap<>();
706 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
709 final UsesNode usesNode = mock(UsesNode.class);
710 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
712 when(usesNode.getGroupingPath()).thenReturn(path);
715 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchema, moduleAug,
716 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
717 } catch (final Exception e) {
719 assertTrue(e instanceof IllegalArgumentException);
720 assertEquals(e.getMessage(), "augment target not found: " + path);
725 public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
726 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
727 final SchemaPath path = SchemaPath.create(true, qnamePath);
728 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
729 when(augmentationSchema.getTargetPath()).thenReturn(path);
730 final Set<UsesNode> uses = new HashSet<>();
731 when(augmentationSchema.getUses()).thenReturn(uses);
733 final SchemaContext context = mock(SchemaContext.class);
734 final Module moduleAug = mock(Module.class);
735 when(moduleAug.getName()).thenReturn("augm-module");
736 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
737 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
738 final Set<GroupingDefinition> groupings = new HashSet<>();
739 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
740 when(groupingDefinition.getQName()).thenReturn(qnamePath);
741 final DataSchemaNode schNode = mock(DataSchemaNode.class);
742 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
743 groupings.add(groupingDefinition);
744 when(moduleAug.getGroupings()).thenReturn(groupings);
746 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
747 .thenReturn(moduleAug);
749 final Map<Module, ModuleContext> genCtx = new HashMap<>();
750 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
752 final UsesNode usesNode = mock(UsesNode.class);
753 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
755 when(usesNode.getGroupingPath()).thenReturn(path);
758 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchema, moduleAug,
759 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
760 } catch (final Exception e) {
762 assertTrue(e instanceof NullPointerException);
763 assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
768 public void usesAugmentationToGenTypesTest() throws Exception {
769 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
770 final SchemaPath path = SchemaPath.create(true, qnamePath);
771 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
772 when(augmentationSchema.getTargetPath()).thenReturn(path);
773 final Set<UsesNode> uses = new HashSet<>();
774 when(augmentationSchema.getUses()).thenReturn(uses);
776 final SchemaContext context = mock(SchemaContext.class);
777 final Module moduleAug = mock(Module.class);
778 when(moduleAug.getName()).thenReturn("augm-module");
779 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
780 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
781 final Set<GroupingDefinition> groupings = new HashSet<>();
782 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
783 when(groupingDefinition.getQName()).thenReturn(qnamePath);
784 final DataSchemaNode schNode = mock(DataSchemaNode.class);
785 when(schNode.getPath()).thenReturn(path);
786 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
787 groupings.add(groupingDefinition);
788 when(moduleAug.getGroupings()).thenReturn(groupings);
790 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
791 .thenReturn(moduleAug);
793 final Map<Module, ModuleContext> genCtx = new HashMap<>();
794 final ModuleContext mc = new ModuleContext();
795 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
796 mc.addChildNodeType(schNode, gtb);
797 genCtx.put(moduleAug, mc);
799 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
801 final UsesNode usesNode = mock(UsesNode.class);
802 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
804 when(usesNode.getGroupingPath()).thenReturn(path);
806 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
807 augmentationSchema, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
808 assertNotNull(result);
812 public void usesAugmentationToGenTypesChoiceTest() throws Exception {
813 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
814 final SchemaPath path = SchemaPath.create(true, qnamePath);
815 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
816 when(augmentationSchema.getTargetPath()).thenReturn(path);
817 final Set<UsesNode> uses = new HashSet<>();
818 when(augmentationSchema.getUses()).thenReturn(uses);
820 final SchemaContext context = mock(SchemaContext.class);
821 final Module moduleAug = mock(Module.class);
822 when(moduleAug.getName()).thenReturn("augm-module");
823 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
824 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
825 final Set<GroupingDefinition> groupings = new HashSet<>();
826 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
827 when(groupingDefinition.getQName()).thenReturn(qnamePath);
828 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
829 when(schNode.getPath()).thenReturn(path);
830 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
831 groupings.add(groupingDefinition);
832 when(moduleAug.getGroupings()).thenReturn(groupings);
834 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
835 .thenReturn(moduleAug);
837 final Map<Module, ModuleContext> genCtx = new HashMap<>();
838 final ModuleContext mc = new ModuleContext();
839 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
840 mc.addChildNodeType(schNode, gtb);
841 genCtx.put(moduleAug, mc);
843 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
845 final UsesNode usesNode = mock(UsesNode.class);
846 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
848 when(usesNode.getGroupingPath()).thenReturn(path);
850 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
851 augmentationSchema, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
852 assertNotNull(result);
855 @SuppressWarnings({ "rawtypes" })
857 public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
858 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
859 final Method generate =
860 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
861 assertNotNull(generate);
862 generate.setAccessible(true);
864 final Module module = mock(Module.class);
865 final QName qnamePath = QName.create("test", "2017-04-04", "test");
866 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
867 final DataSchemaNode schNode = mock(DataSchemaNode.class);
868 when(schNode.getPath()).thenReturn(schemaPath);
869 when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
871 final SchemaContext context = mock(SchemaContext.class);
872 when(context.findModuleByNamespaceAndRevision(qnamePath .getNamespace(), qnamePath.getRevision()))
874 final UsesNode usesNode = mock(UsesNode.class);
875 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
877 final Object[] args = { context, schemaPath, usesNode };
879 generate.invoke(AugmentToGenType.class, args);
881 } catch (final Exception e) {
883 assertTrue(e instanceof InvocationTargetException);
884 final Throwable cause = e.getCause();
885 assertNotNull(cause);
886 assertTrue(cause instanceof IllegalArgumentException);
887 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
891 @SuppressWarnings({ "rawtypes" })
893 public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
894 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
895 final Method generate =
896 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
897 assertNotNull(generate);
898 generate.setAccessible(true);
900 final Module module = mock(Module.class);
901 final QName qnamePath = QName.create("test", "2017-04-04", "test");
902 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
903 final DataSchemaNode schNode = mock(DataSchemaNode.class);
904 when(schNode.getPath()).thenReturn(schemaPath);
905 when(schNode.isAddedByUses()).thenReturn(true);
906 final Set<GroupingDefinition> groupings = new HashSet<>();
907 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
908 when(groupingDefinition.getQName()).thenReturn(qnamePath);
909 groupings.add(groupingDefinition);
910 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
911 when(module.getGroupings()).thenReturn(groupings);
913 final SchemaContext context = mock(SchemaContext.class);
914 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
916 final UsesNode usesNode = mock(UsesNode.class);
917 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
919 final Object[] args = { context, schemaPath, usesNode };
921 generate.invoke(AugmentToGenType.class, args);
923 } catch (final Exception e) {
925 assertTrue(e instanceof InvocationTargetException);
926 final Throwable cause = e.getCause();
927 assertNotNull(cause);
928 assertTrue(cause instanceof IllegalStateException);
929 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
933 @SuppressWarnings({ "rawtypes" })
935 public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
936 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
937 final Method generate =
938 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
939 assertNotNull(generate);
940 generate.setAccessible(true);
942 final Module module = mock(Module.class);
943 final QName qnamePath = QName.create("test", "2017-04-04", "test");
944 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
945 final DataSchemaNode schNode = null;
946 final Set<GroupingDefinition> groupings = new HashSet<>();
947 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
948 when(groupingDefinition.getQName()).thenReturn(qnamePath);
949 groupings.add(groupingDefinition);
950 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
951 when(module.getGroupings()).thenReturn(groupings);
953 final SchemaContext context = mock(SchemaContext.class);
954 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
956 final UsesNode usesNode = mock(UsesNode.class);
957 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
959 final Object[] args = { context, schemaPath, usesNode };
960 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
961 assertEquals(null, result);
964 @SuppressWarnings({ "rawtypes", "unchecked" })
966 public void findOriginalTargetFromGroupingTest() throws Exception {
967 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
968 final Method generate =
969 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
970 assertNotNull(generate);
971 generate.setAccessible(true);
973 final Module module = mock(Module.class);
974 final QName qnamePath = QName.create("test", "2017-04-04", "test");
975 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
976 final DataSchemaNode schNode = mock(DataSchemaNode.class);
977 when(schNode.getPath()).thenReturn(schemaPath);
978 final Set<GroupingDefinition> groupings = new HashSet<>();
979 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
980 when(groupingDefinition.getQName()).thenReturn(qnamePath);
981 groupings.add(groupingDefinition);
982 final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
983 when(derivSchNode.isAddedByUses()).thenReturn(true);
984 final Optional optional = Optional.of(schNode);
985 when(derivSchNode.getOriginal()).thenReturn(optional);
986 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
987 when(module.getGroupings()).thenReturn(groupings);
989 final SchemaContext context = mock(SchemaContext.class);
990 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
992 final UsesNode usesNode = mock(UsesNode.class);
993 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
995 final Object[] args = { context, schemaPath, usesNode };
996 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
997 assertEquals(schNode, result);
1000 @SuppressWarnings({ "rawtypes" })
1002 public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1003 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1004 final Method generate =
1005 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1006 assertNotNull(generate);
1007 generate.setAccessible(true);
1009 final Module module = mock(Module.class);
1010 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1011 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1012 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1013 when(schNode.getPath()).thenReturn(schemaPath);
1014 final Set<GroupingDefinition> groupings = new HashSet<>();
1015 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1016 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1017 groupings.add(groupingDefinition);
1018 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1019 when(module.getGroupings()).thenReturn(groupings);
1021 final SchemaContext context = mock(SchemaContext.class);
1022 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1023 .thenReturn(module);
1024 final UsesNode usesNode = mock(UsesNode.class);
1025 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1027 final Object[] args = { context, schemaPath, usesNode };
1028 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1029 assertEquals(schNode, result);
1032 @SuppressWarnings({ "rawtypes" })
1034 public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1035 final Class[] parameterTypes =
1036 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1037 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1038 final Method generate =
1039 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1040 assertNotNull(generate);
1041 generate.setAccessible(true);
1043 final SchemaContext schemaContext = null;
1044 final Module module = null;
1045 final String pckgName = null;
1046 final Type targetType = null;
1047 final ChoiceSchemaNode targetNode = null;
1048 final Iterable augmentNodes = null;
1049 final DataNodeContainer usesNodeParent = null;
1050 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1051 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1053 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1054 genCtx, false, genTypeBuilder, null };
1056 generate.invoke(AugmentToGenType.class, args);
1058 } catch (final Exception e) {
1060 assertTrue(e instanceof InvocationTargetException);
1061 final Throwable cause = e.getCause();
1062 assertNotNull(cause);
1063 assertTrue(cause instanceof IllegalArgumentException);
1064 assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1068 @SuppressWarnings({ "rawtypes" })
1070 public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1071 final Class[] parameterTypes =
1072 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1073 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1074 final Method generate =
1075 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1076 assertNotNull(generate);
1077 generate.setAccessible(true);
1079 final SchemaContext schemaContext = null;
1080 final Module module = null;
1081 final String pckgName = "";
1082 final Type targetType = null;
1083 final ChoiceSchemaNode targetNode = null;
1084 final Iterable augmentNodes = null;
1085 final DataNodeContainer usesNodeParent = null;
1086 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1087 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1089 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1090 genCtx, false, genTypeBuilder, null };
1092 generate.invoke(AugmentToGenType.class, args);
1094 } catch (final Exception e) {
1096 assertTrue(e instanceof InvocationTargetException);
1097 final Throwable cause = e.getCause();
1098 assertNotNull(cause);
1099 assertTrue(cause instanceof IllegalArgumentException);
1100 assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1104 @SuppressWarnings({ "rawtypes" })
1106 public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1107 final Class[] parameterTypes =
1108 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1109 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1110 final Method generate =
1111 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1112 assertNotNull(generate);
1113 generate.setAccessible(true);
1115 final SchemaContext schemaContext = null;
1116 final Module module = null;
1117 final String pckgName = "";
1118 final Type targetType = mock(Type.class);
1119 final ChoiceSchemaNode targetNode = null;
1120 final Iterable augmentNodes = null;
1121 final DataNodeContainer usesNodeParent = null;
1122 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1123 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1125 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1126 genCtx, false, genTypeBuilder, null };
1128 generate.invoke(AugmentToGenType.class, args);
1130 } catch (final Exception e) {
1132 assertTrue(e instanceof InvocationTargetException);
1133 final Throwable cause = e.getCause();
1134 assertNotNull(cause);
1135 assertTrue(cause instanceof IllegalArgumentException);
1136 assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1140 @SuppressWarnings({ "rawtypes", "unchecked" })
1142 public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1143 final Class[] parameterTypes =
1144 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1145 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1146 final Method generate =
1147 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1148 assertNotNull(generate);
1149 generate.setAccessible(true);
1151 final SchemaContext schemaContext = null;
1152 final Module module = null;
1153 final String pckgName = "";
1154 final Type targetType = mock(Type.class);
1155 final ChoiceSchemaNode targetNode = null;
1156 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1157 final DataSchemaNode caseNode = null;
1158 augmentNodes.add(caseNode);
1159 final DataNodeContainer usesNodeParent = null;
1160 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1161 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1163 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1164 genCtx, false, genTypeBuilder, null };
1165 final Map<Module, ModuleContext> result =
1166 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1167 assertEquals(genCtx, result);
1170 @SuppressWarnings({ "rawtypes" })
1172 public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1173 final Class[] parameterTypes =
1174 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1175 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1176 final Method generate =
1177 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1178 assertNotNull(generate);
1179 generate.setAccessible(true);
1181 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1182 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1183 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1185 final SchemaContext schemaContext = mock(SchemaContext.class);
1186 final Module module = mock(Module.class);
1187 when(module.getName()).thenReturn("test-module-case");
1188 final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1189 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1190 final String pckgName = "test.augment.choice.cases";
1191 final Type targetType = mock(Type.class);
1192 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1193 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1194 when(caseNode.getPath()).thenReturn(path);
1195 when(caseNode.getQName()).thenReturn(qnamePath);
1196 augmentNodes.add(caseNode);
1197 final DataNodeContainer usesNodeParent = null;
1198 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1199 when(targetNode.getPath()).thenReturn(path);
1200 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1201 final ModuleContext moduleContext = new ModuleContext();
1202 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1203 moduleContext.addCaseType(path, gtb);
1204 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1206 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1207 .thenReturn(module);
1209 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1210 genCtx, false, genTypeBuilder, null };
1212 generate.invoke(AugmentToGenType.class, args);
1214 } catch (final Exception e) {
1216 assertTrue(e instanceof InvocationTargetException);
1217 final Throwable cause = e.getCause();
1218 assertNotNull(cause);
1219 assertTrue(cause instanceof IllegalArgumentException);
1220 assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1224 @SuppressWarnings({ "rawtypes", "unchecked" })
1226 public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1227 final Class[] parameterTypes =
1228 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1229 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1230 final Method generate =
1231 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1232 assertNotNull(generate);
1233 generate.setAccessible(true);
1235 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1236 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1237 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1239 final SchemaContext schemaContext = mock(SchemaContext.class);
1240 final Module module = mock(Module.class);
1241 when(module.getName()).thenReturn("test-module-case");
1242 final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1243 when(schemaNode.getPath()).thenReturn(path);
1244 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1245 final String pckgName = "test.augment.choice.cases";
1246 final Type targetType = mock(Type.class);
1247 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1248 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1249 when(caseNode.getPath()).thenReturn(path);
1250 when(caseNode.getQName()).thenReturn(qnamePath);
1251 augmentNodes.add(caseNode);
1252 final DataNodeContainer usesNodeParent = null;
1253 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1254 when(targetNode.getPath()).thenReturn(path);
1255 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1256 final ModuleContext moduleContext = new ModuleContext();
1257 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1258 moduleContext.addCaseType(path, gtb);
1259 genCtx.put(module, moduleContext);
1260 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1262 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1263 .thenReturn(module);
1265 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1266 genCtx, false, genTypeBuilder, null };
1267 final Map<Module, ModuleContext> result =
1268 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1269 assertNotNull(result);
1270 assertEquals(result.get(module), moduleContext);