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.Ignore;
29 import org.junit.Test;
30 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
31 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
32 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
33 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
34 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
37 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
38 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.UsesNode;
49 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
51 public class AugmentToGenTypeTest {
53 @SuppressWarnings("unchecked")
54 @Test(expected = UnsupportedOperationException.class)
55 public void constructorTest() throws Throwable {
56 final Constructor<AugmentToGenType> constructor =
57 (Constructor<AugmentToGenType>) AugmentToGenType.class.getDeclaredConstructors()[0];
58 constructor.setAccessible(true);
59 final Object[] objs = {};
61 constructor.newInstance(objs);
62 } catch (final Exception e) {
67 @SuppressWarnings("rawtypes")
69 public void generateNullModuleTest() throws Exception {
70 final Class[] parameterTypes =
71 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
72 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
73 assertNotNull(generate);
74 generate.setAccessible(true);
76 final SchemaContext context = null;
77 final TypeProvider typeProvider = null;
78 final Map<Module, ModuleContext> genCtx = new HashMap<>();
79 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
80 final Module m = null;
82 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
84 generate.invoke(AugmentToGenType.class, args);
86 } catch (final Exception e) {
88 assertTrue(e instanceof InvocationTargetException);
89 final Throwable cause = e.getCause();
91 assertTrue(cause instanceof IllegalArgumentException);
92 assertEquals("Module reference cannot be NULL.", cause.getMessage());
96 @SuppressWarnings("rawtypes")
98 public void generateNullModuleNameTest() throws Exception {
99 final Class[] parameterTypes =
100 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
101 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
102 assertNotNull(generate);
103 generate.setAccessible(true);
105 final SchemaContext context = null;
106 final TypeProvider typeProvider = null;
107 final Map<Module, ModuleContext> genCtx = new HashMap<>();
108 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
109 final Module m = mock(Module.class);
110 when(m.getName()).thenReturn(null);
112 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
114 generate.invoke(AugmentToGenType.class, args);
116 } catch (final Exception e) {
118 assertTrue(e instanceof InvocationTargetException);
119 final Throwable cause = e.getCause();
120 assertNotNull(cause);
121 assertTrue(cause instanceof IllegalArgumentException);
122 assertEquals("Module name cannot be NULL.", cause.getMessage());
126 @SuppressWarnings("rawtypes")
128 public void generateNullModuleAugmentationsTest() throws Exception {
129 final Class[] parameterTypes =
130 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
131 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
132 assertNotNull(generate);
133 generate.setAccessible(true);
135 final SchemaContext context = null;
136 final TypeProvider typeProvider = null;
137 final Map<Module, ModuleContext> genCtx = new HashMap<>();
138 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
139 final Module m = mock(Module.class);
140 when(m.getName()).thenReturn("name");
141 when(m.getAugmentations()).thenReturn(null);
143 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
145 generate.invoke(AugmentToGenType.class, args);
147 } catch (final Exception e) {
149 assertTrue(e instanceof InvocationTargetException);
150 final Throwable cause = e.getCause();
151 assertNotNull(cause);
152 assertTrue(cause instanceof IllegalStateException);
153 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
157 @SuppressWarnings("rawtypes")
159 public void generateWithoutAugmentationsTest() throws Exception {
160 final Class[] parameterTypes =
161 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
162 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
163 assertNotNull(generate);
164 generate.setAccessible(true);
166 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test.yang");
167 final TypeProvider typeProvider = new TypeProviderImpl(context);
168 final Map<Module, ModuleContext> genCtx = new HashMap<>();
169 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
171 final Object[] args =
172 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
173 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
174 assertNotNull(invoke);
177 @SuppressWarnings({ "rawtypes", "unchecked" })
179 public void generateWithAugmentationsTest() throws Exception {
180 final Class[] parameterTypes =
181 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
182 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
183 assertNotNull(generate);
184 generate.setAccessible(true);
186 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test-augment.yang");
187 final TypeProvider typeProvider = new TypeProviderImpl(context);
188 final Map<Module, ModuleContext> genCtx = mock(Map.class);
189 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
190 final ModuleContext moduleContext = new ModuleContext();
191 moduleContexts.add(moduleContext);
192 final QName create = QName.create("urn:test:simple:test", "2017-02-06", "my-cont");
193 final SchemaNode schemaNode = mock(SchemaNode.class);
194 when(schemaNode.getPath()).thenReturn(SchemaPath.create(true, create));
195 moduleContext.addChildNodeType(schemaNode, new GeneratedTypeBuilderImpl("test", "Test"));
196 when(genCtx.values()).thenReturn(moduleContexts);
197 when(genCtx.get(context.getModules().iterator().next())).thenReturn(moduleContext);
198 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
200 final Object[] args =
201 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
202 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
203 assertNotNull(invoke);
206 @SuppressWarnings("rawtypes")
208 public void resolveAugmentationsNullModuleTest() throws Exception {
209 final Class[] parameterTypes = { Module.class };
210 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
211 assertNotNull(generate);
212 generate.setAccessible(true);
214 final Module m = null;
216 final Object[] args = { m };
218 generate.invoke(AugmentToGenType.class, args);
220 } catch (final Exception e) {
222 assertTrue(e instanceof InvocationTargetException);
223 final Throwable cause = e.getCause();
224 assertNotNull(cause);
225 assertTrue(cause instanceof IllegalArgumentException);
226 assertEquals("Module reference cannot be NULL.", cause.getMessage());
230 @SuppressWarnings("rawtypes")
232 public void resolveAugmentationsNullAugmentationsTest() throws Exception {
233 final Class[] parameterTypes = { Module.class };
234 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
235 assertNotNull(generate);
236 generate.setAccessible(true);
238 final Module m = mock(Module.class);
239 when(m.getAugmentations()).thenReturn(null);
241 final Object[] args = { m };
243 generate.invoke(AugmentToGenType.class, args);
245 } catch (final Exception e) {
247 assertTrue(e instanceof InvocationTargetException);
248 final Throwable cause = e.getCause();
249 assertNotNull(cause);
250 assertTrue(cause instanceof IllegalStateException);
251 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
255 @SuppressWarnings({ "rawtypes", "unchecked" })
257 public void resolveAugmentationsTest() throws Exception {
258 final Class[] parameterTypes = { Module.class };
259 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
260 assertNotNull(generate);
261 generate.setAccessible(true);
263 final Module m = mock(Module.class);
265 final Set<AugmentationSchema> augmentations = new HashSet<>();
267 final QName q1 = QName.create("q1", "2017-04-04", "q1");
268 final QName q2 = QName.create("q2", "2017-04-04", "q2");
269 final QName q3 = QName.create("q3", "2017-04-04", "q3");
270 final QName q4 = QName.create("q4", "2017-04-04", "q4");
271 final QName q5 = QName.create("q5", "2017-04-04", "q5");
273 final AugmentationSchema augmentationSchema1 = mock(AugmentationSchema.class);
274 when(augmentationSchema1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
275 final AugmentationSchema augmentationSchema2 = mock(AugmentationSchema.class);
276 when(augmentationSchema2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
277 augmentations.add(augmentationSchema1);
278 augmentations.add(augmentationSchema2);
280 when(m.getAugmentations()).thenReturn(augmentations);
282 final Object[] args = { m };
284 final List<AugmentationSchema> result =
285 (List<AugmentationSchema>) generate.invoke(AugmentToGenType.class, args);
286 assertNotNull(result);
287 assertTrue(!result.isEmpty());
288 assertEquals(result.get(0), augmentationSchema1);
289 assertEquals(result.get(1), augmentationSchema2);
293 @SuppressWarnings({ "rawtypes" })
295 public void augmentationToGenTypesNullPckgNameTest() throws Exception {
296 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
297 boolean.class, Map.class, Map.class, TypeProvider.class };
298 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
299 assertNotNull(generate);
300 generate.setAccessible(true);
302 final String augmPackName = null;
303 final AugmentationSchema augmSchema = null;
304 final SchemaContext context = null;
305 final TypeProvider typeProvider = null;
306 final Map<Module, ModuleContext> genCtx = new HashMap<>();
307 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
308 final Module m = null;
310 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
312 generate.invoke(AugmentToGenType.class, args);
314 } catch (final Exception e) {
316 assertTrue(e instanceof InvocationTargetException);
317 final Throwable cause = e.getCause();
318 assertNotNull(cause);
319 assertTrue(cause instanceof IllegalArgumentException);
320 assertEquals("Package Name cannot be NULL.", cause.getMessage());
325 @SuppressWarnings({ "rawtypes" })
327 public void augmentationToGenTypesNullAugSchemaTest() throws Exception {
328 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
329 boolean.class, Map.class, Map.class, TypeProvider.class };
330 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
331 assertNotNull(generate);
332 generate.setAccessible(true);
334 final String augmPackName = "pckg.name";
335 final AugmentationSchema augmSchema = null;
336 final SchemaContext context = null;
337 final TypeProvider typeProvider = null;
338 final Map<Module, ModuleContext> genCtx = new HashMap<>();
339 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
340 final Module m = null;
342 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
344 generate.invoke(AugmentToGenType.class, args);
346 } catch (final Exception e) {
348 assertTrue(e instanceof InvocationTargetException);
349 final Throwable cause = e.getCause();
350 assertNotNull(cause);
351 assertTrue(cause instanceof IllegalArgumentException);
352 assertEquals("Augmentation Schema cannot be NULL.", cause.getMessage());
357 @SuppressWarnings({ "rawtypes" })
359 public void augmentationToGenTypesNullAugSchemaTargetPathTest() throws Exception {
360 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
361 boolean.class, Map.class, Map.class, TypeProvider.class };
362 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
363 assertNotNull(generate);
364 generate.setAccessible(true);
366 final String augmPackName = "pckg.name";
367 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
368 when(augmSchema.getTargetPath()).thenReturn(null);
370 final SchemaContext context = null;
371 final TypeProvider typeProvider = null;
372 final Map<Module, ModuleContext> genCtx = new HashMap<>();
373 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
374 final Module m = null;
376 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
378 generate.invoke(AugmentToGenType.class, args);
380 } catch (final Exception e) {
382 assertTrue(e instanceof InvocationTargetException);
383 final Throwable cause = e.getCause();
384 assertNotNull(cause);
385 assertTrue(cause instanceof IllegalStateException);
386 assertEquals("Augmentation Schema does not contain Target Path (Target Path is NULL).", cause.getMessage());
391 @SuppressWarnings({ "rawtypes" })
393 public void augmentationToGenTypesNullAugSchemaTargetNodeTest() throws Exception {
394 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
395 boolean.class, Map.class, Map.class, TypeProvider.class };
396 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
397 assertNotNull(generate);
398 generate.setAccessible(true);
400 final String augmPackName = "pckg.name";
402 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
403 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
404 final SchemaPath path = SchemaPath.create(true, qnamePath);
405 when(augmSchema.getTargetPath()).thenReturn(path);
406 final Set<UsesNode> uses = new HashSet<>();
407 when(augmSchema.getUses()).thenReturn(uses);
409 final SchemaContext context = mock(SchemaContext.class);
410 final Module moduleAug = mock(Module.class);
411 final DataSchemaNode schNode = null;
412 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
413 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
414 .thenReturn(moduleAug);
416 final TypeProvider typeProvider = null;
417 final Map<Module, ModuleContext> genCtx = new HashMap<>();
418 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
420 final Module m = mock(Module.class);
421 when(m.getName()).thenReturn("augm-module");
422 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
423 when(m.getRevision()).thenReturn(qnamePath.getRevision());
425 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
427 generate.invoke(AugmentToGenType.class, args);
429 } catch (final Exception e) {
431 assertTrue(e instanceof InvocationTargetException);
432 final Throwable cause = e.getCause();
433 assertNotNull(cause);
434 assertTrue(cause instanceof IllegalArgumentException);
435 assertEquals("augment target not found: " + path, cause.getMessage());
440 @SuppressWarnings({ "rawtypes" })
442 public void augmentationToGenTypesNullAugTargetGTBTest() throws Exception {
443 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
444 boolean.class, Map.class, Map.class, TypeProvider.class };
445 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
446 assertNotNull(generate);
447 generate.setAccessible(true);
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 SchemaContext context = mock(SchemaContext.class);
459 final Module moduleAug = mock(Module.class);
460 final DataSchemaNode schNode = mock(DataSchemaNode.class);
461 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
462 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
463 .thenReturn(moduleAug);
465 final TypeProvider typeProvider = null;
466 final Map<Module, ModuleContext> genCtx = new HashMap<>();
467 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
469 final Module m = mock(Module.class);
470 when(m.getName()).thenReturn("augm-module");
471 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
472 when(m.getRevision()).thenReturn(qnamePath.getRevision());
474 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
476 generate.invoke(AugmentToGenType.class, args);
478 } catch (final Exception e) {
480 assertTrue(e instanceof InvocationTargetException);
481 final Throwable cause = e.getCause();
482 assertNotNull(cause);
483 assertTrue(cause instanceof NullPointerException);
484 assertEquals("Target type not yet generated: " + schNode, cause.getMessage());
489 @SuppressWarnings({ "rawtypes", "unchecked" })
491 public void augmentationToGenTypesAugUsesNullOrigTargetTest() throws Exception {
492 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
493 boolean.class, Map.class, Map.class, TypeProvider.class };
494 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
495 assertNotNull(generate);
496 generate.setAccessible(true);
498 final String augmPackName = "pckg.name";
500 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
501 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
502 final SchemaPath path = SchemaPath.create(true, qnamePath);
503 when(augmSchema.getTargetPath()).thenReturn(path);
504 final Set<UsesNode> uses = new HashSet<>();
505 when(augmSchema.getUses()).thenReturn(uses);
507 final SchemaContext context = mock(SchemaContext.class);
508 final Module moduleAug = mock(Module.class);
509 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
510 when(targetSchNode.getPath()).thenReturn(path);
511 when(targetSchNode.isAddedByUses()).thenReturn(true);
512 final Optional optionalSchemaNode = Optional.absent();
513 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
514 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
515 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
516 .thenReturn(moduleAug);
518 final TypeProvider typeProvider = null;
519 final Map<Module, ModuleContext> genCtx = new HashMap<>();
520 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
522 final Module m = mock(Module.class);
523 when(m.getName()).thenReturn("augm-module");
524 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
525 when(m.getRevision()).thenReturn(qnamePath.getRevision());
527 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
529 generate.invoke(AugmentToGenType.class, args);
531 } catch (final Exception e) {
533 assertTrue(e instanceof InvocationTargetException);
534 final Throwable cause = e.getCause();
535 assertNotNull(cause);
536 assertTrue(cause instanceof IllegalStateException);
537 assertEquals("Failed to find target node from grouping in augmentation " + augmSchema + " in module "
538 + m.getName(), cause.getMessage());
543 @SuppressWarnings({ "rawtypes" })
545 public void augmentationToGenTypesTargetChoicSchemaNodeTest() throws Exception {
546 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
547 boolean.class, Map.class, Map.class, TypeProvider.class };
548 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
549 assertNotNull(generate);
550 generate.setAccessible(true);
552 final String augmPackName = "pckg.name";
554 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
555 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
556 final SchemaPath path = SchemaPath.create(true, qnamePath);
557 when(augmSchema.getTargetPath()).thenReturn(path);
558 final Set<UsesNode> uses = new HashSet<>();
559 when(augmSchema.getUses()).thenReturn(uses);
561 final SchemaContext context = mock(SchemaContext.class);
562 final Module moduleAug = mock(Module.class);
563 final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
564 when(targetSchNode.getPath()).thenReturn(path);
565 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
566 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
567 .thenReturn(moduleAug);
569 final TypeProvider typeProvider = null;
571 final Map genCtx = mock(Map.class);
572 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
573 final ModuleContext mc = new ModuleContext();
574 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
575 mc.addChildNodeType(targetSchNode, gtb);
576 moduleContexts.add(mc);
577 when(genCtx.values()).thenReturn(moduleContexts);
579 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
581 final Module m = mock(Module.class);
582 when(m.getName()).thenReturn("augm-module");
583 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
584 when(m.getRevision()).thenReturn(qnamePath.getRevision());
586 final Object[] args = { augmPackName, augmSchema, m, context, false, genCtx, genTypeBuilders, typeProvider };
587 final Map result = (Map) generate.invoke(AugmentToGenType.class, args);
588 assertNotNull(result);
592 @SuppressWarnings({ "rawtypes", "unchecked" })
594 public void augmentationToGenTypesTest() throws Exception {
595 final Class[] parameterTypes = { String.class, AugmentationSchema.class, Module.class, SchemaContext.class,
596 boolean.class, Map.class, Map.class, TypeProvider.class };
597 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
598 assertNotNull(generate);
599 generate.setAccessible(true);
601 final String augmPackName = "pckg.name";
603 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
604 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
605 final SchemaPath path = SchemaPath.create(true, qnamePath);
606 when(augmSchema.getTargetPath()).thenReturn(path);
607 final Set<UsesNode> uses = new HashSet<>();
608 when(augmSchema.getUses()).thenReturn(uses);
609 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
610 when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
612 final SchemaContext context = mock(SchemaContext.class);
613 final Module moduleAug = mock(Module.class);
614 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
615 when(targetSchNode.getPath()).thenReturn(path);
616 when(targetSchNode.isAddedByUses()).thenReturn(true);
617 final DataSchemaNode origSchNode = mock(DataSchemaNode.class);
618 when(origSchNode.getPath()).thenReturn(path);
619 when(origSchNode.isAddedByUses()).thenReturn(true);
620 final Optional optionalSchemaNode = Optional.of(origSchNode);
621 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
622 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
623 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
624 .thenReturn(moduleAug);
626 final TypeProvider typeProvider = null;
628 final Map<Module, ModuleContext> genCtx = new HashMap<>();
630 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
631 final ModuleContext mc = new ModuleContext();
632 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
633 mc.addChildNodeType(targetSchNode, gtb);
634 moduleContexts.add(mc);
635 genCtx.put(moduleAug, mc);
637 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
639 when(moduleAug.getName()).thenReturn("augm-module");
640 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
641 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
643 final Object[] args =
644 { augmPackName, augmSchema, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
645 final Map<Module, ModuleContext> result =
646 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
647 assertNotNull(result);
648 final ModuleContext moduleContext = result.get(moduleAug);
649 assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
650 assertEquals("pckg.name", moduleContext.getAugmentations().get(0).getPackageName());
651 assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
652 assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
656 public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
658 AugmentToGenType.usesAugmentationToGenTypes(null, null, null, null, null, null, null, null, false, null);
659 } catch (final Exception e) {
661 assertTrue(e instanceof IllegalArgumentException);
662 assertEquals(e.getMessage(), "Package Name cannot be NULL.");
667 public void usesAugmentationToGenTypesNullAugSchemaNodeTest() throws Exception {
669 AugmentToGenType.usesAugmentationToGenTypes(null, "", null, null, null, null, null, null, false, null);
670 } catch (final Exception e) {
672 assertTrue(e instanceof IllegalArgumentException);
673 assertEquals(e.getMessage(), "Augmentation Schema cannot be NULL.");
678 public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
679 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
680 when(augmentationSchema.getTargetPath()).thenReturn(null);
682 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchema, null, null, null, null, null,
684 } catch (final Exception e) {
686 assertTrue(e instanceof IllegalStateException);
687 assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
692 public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
693 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
694 final SchemaPath path = SchemaPath.create(true, qnamePath);
695 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
696 when(augmentationSchema.getTargetPath()).thenReturn(path);
697 final Set<UsesNode> uses = new HashSet<>();
698 when(augmentationSchema.getUses()).thenReturn(uses);
700 final SchemaContext context = mock(SchemaContext.class);
701 final Module moduleAug = mock(Module.class);
702 when(moduleAug.getName()).thenReturn("augm-module");
703 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
704 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
705 final Set<GroupingDefinition> groupings = new HashSet<>();
706 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
707 when(groupingDefinition.getQName()).thenReturn(qnamePath);
708 groupings.add(groupingDefinition);
709 when(moduleAug.getGroupings()).thenReturn(groupings);
711 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
712 .thenReturn(moduleAug);
714 final Map<Module, ModuleContext> genCtx = new HashMap<>();
715 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
718 final UsesNode usesNode = mock(UsesNode.class);
719 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
721 when(usesNode.getGroupingPath()).thenReturn(path);
724 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchema, moduleAug,
725 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
726 } catch (final Exception e) {
728 assertTrue(e instanceof IllegalArgumentException);
729 assertEquals(e.getMessage(), "augment target not found: " + path);
734 public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
735 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
736 final SchemaPath path = SchemaPath.create(true, qnamePath);
737 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
738 when(augmentationSchema.getTargetPath()).thenReturn(path);
739 final Set<UsesNode> uses = new HashSet<>();
740 when(augmentationSchema.getUses()).thenReturn(uses);
742 final SchemaContext context = mock(SchemaContext.class);
743 final Module moduleAug = mock(Module.class);
744 when(moduleAug.getName()).thenReturn("augm-module");
745 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
746 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
747 final Set<GroupingDefinition> groupings = new HashSet<>();
748 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
749 when(groupingDefinition.getQName()).thenReturn(qnamePath);
750 final DataSchemaNode schNode = mock(DataSchemaNode.class);
751 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
752 groupings.add(groupingDefinition);
753 when(moduleAug.getGroupings()).thenReturn(groupings);
755 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
756 .thenReturn(moduleAug);
758 final Map<Module, ModuleContext> genCtx = new HashMap<>();
759 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
761 final UsesNode usesNode = mock(UsesNode.class);
762 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
764 when(usesNode.getGroupingPath()).thenReturn(path);
767 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchema, moduleAug,
768 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
769 } catch (final Exception e) {
771 assertTrue(e instanceof NullPointerException);
772 assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
777 public void usesAugmentationToGenTypesTest() throws Exception {
778 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
779 final SchemaPath path = SchemaPath.create(true, qnamePath);
780 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
781 when(augmentationSchema.getTargetPath()).thenReturn(path);
782 final Set<UsesNode> uses = new HashSet<>();
783 when(augmentationSchema.getUses()).thenReturn(uses);
785 final SchemaContext context = mock(SchemaContext.class);
786 final Module moduleAug = mock(Module.class);
787 when(moduleAug.getName()).thenReturn("augm-module");
788 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
789 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
790 final Set<GroupingDefinition> groupings = new HashSet<>();
791 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
792 when(groupingDefinition.getQName()).thenReturn(qnamePath);
793 final DataSchemaNode schNode = mock(DataSchemaNode.class);
794 when(schNode.getPath()).thenReturn(path);
795 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
796 groupings.add(groupingDefinition);
797 when(moduleAug.getGroupings()).thenReturn(groupings);
799 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
800 .thenReturn(moduleAug);
802 final Map<Module, ModuleContext> genCtx = new HashMap<>();
803 final ModuleContext mc = new ModuleContext();
804 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
805 mc.addChildNodeType(schNode, gtb);
806 genCtx.put(moduleAug, mc);
808 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
810 final UsesNode usesNode = mock(UsesNode.class);
811 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
813 when(usesNode.getGroupingPath()).thenReturn(path);
815 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
816 augmentationSchema, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
817 assertNotNull(result);
821 public void usesAugmentationToGenTypesChoiceTest() throws Exception {
822 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
823 final SchemaPath path = SchemaPath.create(true, qnamePath);
824 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
825 when(augmentationSchema.getTargetPath()).thenReturn(path);
826 final Set<UsesNode> uses = new HashSet<>();
827 when(augmentationSchema.getUses()).thenReturn(uses);
829 final SchemaContext context = mock(SchemaContext.class);
830 final Module moduleAug = mock(Module.class);
831 when(moduleAug.getName()).thenReturn("augm-module");
832 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
833 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
834 final Set<GroupingDefinition> groupings = new HashSet<>();
835 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
836 when(groupingDefinition.getQName()).thenReturn(qnamePath);
837 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
838 when(schNode.getPath()).thenReturn(path);
839 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
840 groupings.add(groupingDefinition);
841 when(moduleAug.getGroupings()).thenReturn(groupings);
843 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
844 .thenReturn(moduleAug);
846 final Map<Module, ModuleContext> genCtx = new HashMap<>();
847 final ModuleContext mc = new ModuleContext();
848 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
849 mc.addChildNodeType(schNode, gtb);
850 genCtx.put(moduleAug, mc);
852 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
854 final UsesNode usesNode = mock(UsesNode.class);
855 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
857 when(usesNode.getGroupingPath()).thenReturn(path);
859 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
860 augmentationSchema, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
861 assertNotNull(result);
864 @SuppressWarnings({ "rawtypes" })
866 public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
867 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
868 final Method generate =
869 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
870 assertNotNull(generate);
871 generate.setAccessible(true);
873 final Module module = mock(Module.class);
874 final QName qnamePath = QName.create("test", "2017-04-04", "test");
875 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
876 final DataSchemaNode schNode = mock(DataSchemaNode.class);
877 when(schNode.getPath()).thenReturn(schemaPath);
878 when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
880 final SchemaContext context = mock(SchemaContext.class);
881 when(context.findModuleByNamespaceAndRevision(qnamePath .getNamespace(), qnamePath.getRevision()))
883 final UsesNode usesNode = mock(UsesNode.class);
884 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
886 final Object[] args = { context, schemaPath, usesNode };
888 generate.invoke(AugmentToGenType.class, args);
890 } catch (final Exception e) {
892 assertTrue(e instanceof InvocationTargetException);
893 final Throwable cause = e.getCause();
894 assertNotNull(cause);
895 assertTrue(cause instanceof IllegalArgumentException);
896 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
900 @SuppressWarnings({ "rawtypes" })
902 public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
903 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
904 final Method generate =
905 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
906 assertNotNull(generate);
907 generate.setAccessible(true);
909 final Module module = mock(Module.class);
910 final QName qnamePath = QName.create("test", "2017-04-04", "test");
911 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
912 final DataSchemaNode schNode = mock(DataSchemaNode.class);
913 when(schNode.getPath()).thenReturn(schemaPath);
914 when(schNode.isAddedByUses()).thenReturn(true);
915 final Set<GroupingDefinition> groupings = new HashSet<>();
916 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
917 when(groupingDefinition.getQName()).thenReturn(qnamePath);
918 groupings.add(groupingDefinition);
919 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
920 when(module.getGroupings()).thenReturn(groupings);
922 final SchemaContext context = mock(SchemaContext.class);
923 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
925 final UsesNode usesNode = mock(UsesNode.class);
926 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
928 final Object[] args = { context, schemaPath, usesNode };
930 generate.invoke(AugmentToGenType.class, args);
932 } catch (final Exception e) {
934 assertTrue(e instanceof InvocationTargetException);
935 final Throwable cause = e.getCause();
936 assertNotNull(cause);
937 assertTrue(cause instanceof IllegalStateException);
938 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
942 @SuppressWarnings({ "rawtypes" })
944 public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
945 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
946 final Method generate =
947 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
948 assertNotNull(generate);
949 generate.setAccessible(true);
951 final Module module = mock(Module.class);
952 final QName qnamePath = QName.create("test", "2017-04-04", "test");
953 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
954 final DataSchemaNode schNode = null;
955 final Set<GroupingDefinition> groupings = new HashSet<>();
956 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
957 when(groupingDefinition.getQName()).thenReturn(qnamePath);
958 groupings.add(groupingDefinition);
959 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
960 when(module.getGroupings()).thenReturn(groupings);
962 final SchemaContext context = mock(SchemaContext.class);
963 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
965 final UsesNode usesNode = mock(UsesNode.class);
966 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
968 final Object[] args = { context, schemaPath, usesNode };
969 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
970 assertEquals(null, result);
973 @SuppressWarnings({ "rawtypes", "unchecked" })
975 public void findOriginalTargetFromGroupingTest() throws Exception {
976 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
977 final Method generate =
978 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
979 assertNotNull(generate);
980 generate.setAccessible(true);
982 final Module module = mock(Module.class);
983 final QName qnamePath = QName.create("test", "2017-04-04", "test");
984 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
985 final DataSchemaNode schNode = mock(DataSchemaNode.class);
986 when(schNode.getPath()).thenReturn(schemaPath);
987 final Set<GroupingDefinition> groupings = new HashSet<>();
988 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
989 when(groupingDefinition.getQName()).thenReturn(qnamePath);
990 groupings.add(groupingDefinition);
991 final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
992 when(derivSchNode.isAddedByUses()).thenReturn(true);
993 final Optional optional = Optional.of(schNode);
994 when(derivSchNode.getOriginal()).thenReturn(optional);
995 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
996 when(module.getGroupings()).thenReturn(groupings);
998 final SchemaContext context = mock(SchemaContext.class);
999 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1000 .thenReturn(module);
1001 final UsesNode usesNode = mock(UsesNode.class);
1002 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1004 final Object[] args = { context, schemaPath, usesNode };
1005 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1006 assertEquals(schNode, result);
1009 @SuppressWarnings({ "rawtypes" })
1011 public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1012 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1013 final Method generate =
1014 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1015 assertNotNull(generate);
1016 generate.setAccessible(true);
1018 final Module module = mock(Module.class);
1019 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1020 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1021 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1022 when(schNode.getPath()).thenReturn(schemaPath);
1023 final Set<GroupingDefinition> groupings = new HashSet<>();
1024 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1025 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1026 groupings.add(groupingDefinition);
1027 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1028 when(module.getGroupings()).thenReturn(groupings);
1030 final SchemaContext context = mock(SchemaContext.class);
1031 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1032 .thenReturn(module);
1033 final UsesNode usesNode = mock(UsesNode.class);
1034 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1036 final Object[] args = { context, schemaPath, usesNode };
1037 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1038 assertEquals(schNode, result);
1041 @SuppressWarnings({ "rawtypes" })
1043 public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1044 final Class[] parameterTypes =
1045 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1046 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1047 final Method generate =
1048 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1049 assertNotNull(generate);
1050 generate.setAccessible(true);
1052 final SchemaContext schemaContext = null;
1053 final Module module = null;
1054 final String pckgName = null;
1055 final Type targetType = null;
1056 final ChoiceSchemaNode targetNode = null;
1057 final Iterable augmentNodes = null;
1058 final DataNodeContainer usesNodeParent = null;
1059 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1060 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1062 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1063 genCtx, false, genTypeBuilder, null };
1065 generate.invoke(AugmentToGenType.class, args);
1067 } catch (final Exception e) {
1069 assertTrue(e instanceof InvocationTargetException);
1070 final Throwable cause = e.getCause();
1071 assertNotNull(cause);
1072 assertTrue(cause instanceof IllegalArgumentException);
1073 assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1077 @SuppressWarnings({ "rawtypes" })
1079 public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1080 final Class[] parameterTypes =
1081 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1082 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1083 final Method generate =
1084 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1085 assertNotNull(generate);
1086 generate.setAccessible(true);
1088 final SchemaContext schemaContext = null;
1089 final Module module = null;
1090 final String pckgName = "";
1091 final Type targetType = null;
1092 final ChoiceSchemaNode targetNode = null;
1093 final Iterable augmentNodes = null;
1094 final DataNodeContainer usesNodeParent = null;
1095 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1096 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1098 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1099 genCtx, false, genTypeBuilder, null };
1101 generate.invoke(AugmentToGenType.class, args);
1103 } catch (final Exception e) {
1105 assertTrue(e instanceof InvocationTargetException);
1106 final Throwable cause = e.getCause();
1107 assertNotNull(cause);
1108 assertTrue(cause instanceof IllegalArgumentException);
1109 assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1113 @SuppressWarnings({ "rawtypes" })
1115 public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1116 final Class[] parameterTypes =
1117 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1118 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1119 final Method generate =
1120 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1121 assertNotNull(generate);
1122 generate.setAccessible(true);
1124 final SchemaContext schemaContext = null;
1125 final Module module = null;
1126 final String pckgName = "";
1127 final Type targetType = mock(Type.class);
1128 final ChoiceSchemaNode targetNode = null;
1129 final Iterable augmentNodes = null;
1130 final DataNodeContainer usesNodeParent = null;
1131 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1132 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1134 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1135 genCtx, false, genTypeBuilder, null };
1137 generate.invoke(AugmentToGenType.class, args);
1139 } catch (final Exception e) {
1141 assertTrue(e instanceof InvocationTargetException);
1142 final Throwable cause = e.getCause();
1143 assertNotNull(cause);
1144 assertTrue(cause instanceof IllegalArgumentException);
1145 assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1149 @SuppressWarnings({ "rawtypes", "unchecked" })
1151 public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1152 final Class[] parameterTypes =
1153 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1154 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1155 final Method generate =
1156 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1157 assertNotNull(generate);
1158 generate.setAccessible(true);
1160 final SchemaContext schemaContext = null;
1161 final Module module = null;
1162 final String pckgName = "";
1163 final Type targetType = mock(Type.class);
1164 final ChoiceSchemaNode targetNode = null;
1165 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1166 final DataSchemaNode caseNode = null;
1167 augmentNodes.add(caseNode);
1168 final DataNodeContainer usesNodeParent = null;
1169 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1170 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1172 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1173 genCtx, false, genTypeBuilder, null };
1174 final Map<Module, ModuleContext> result =
1175 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1176 assertEquals(genCtx, result);
1179 @SuppressWarnings({ "rawtypes" })
1181 public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1182 final Class[] parameterTypes =
1183 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1184 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1185 final Method generate =
1186 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1187 assertNotNull(generate);
1188 generate.setAccessible(true);
1190 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1191 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1192 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1194 final SchemaContext schemaContext = mock(SchemaContext.class);
1195 final Module module = mock(Module.class);
1196 when(module.getName()).thenReturn("test-module-case");
1197 final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1198 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1199 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1200 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1201 final String pckgName = "test.augment.choice.cases";
1202 final Type targetType = mock(Type.class);
1203 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1204 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1205 when(caseNode.getPath()).thenReturn(path);
1206 when(caseNode.getQName()).thenReturn(qnamePath);
1207 augmentNodes.add(caseNode);
1208 final DataNodeContainer usesNodeParent = null;
1209 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1210 when(targetNode.getPath()).thenReturn(path);
1211 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1212 final ModuleContext moduleContext = new ModuleContext();
1213 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1214 moduleContext.addCaseType(path, gtb);
1215 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1217 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1218 .thenReturn(module);
1220 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1221 genCtx, false, genTypeBuilder, null };
1223 generate.invoke(AugmentToGenType.class, args);
1225 } catch (final Exception e) {
1227 assertTrue(e instanceof InvocationTargetException);
1228 final Throwable cause = e.getCause();
1229 assertNotNull(cause);
1230 assertTrue(cause instanceof IllegalArgumentException);
1231 assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1235 @SuppressWarnings({ "rawtypes", "unchecked" })
1237 public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1238 final Class[] parameterTypes =
1239 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, Iterable.class,
1240 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1241 final Method generate =
1242 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1243 assertNotNull(generate);
1244 generate.setAccessible(true);
1246 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1247 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1248 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1250 final SchemaContext schemaContext = mock(SchemaContext.class);
1251 final Module module = mock(Module.class);
1252 when(module.getName()).thenReturn("test-module-case");
1253 final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1254 when(schemaNode.getPath()).thenReturn(path);
1255 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1256 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1257 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1258 final String pckgName = "test.augment.choice.cases";
1259 final Type targetType = mock(Type.class);
1260 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1261 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1262 when(caseNode.getPath()).thenReturn(path);
1263 when(caseNode.getQName()).thenReturn(qnamePath);
1264 augmentNodes.add(caseNode);
1265 final DataNodeContainer usesNodeParent = null;
1266 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1267 when(targetNode.getPath()).thenReturn(path);
1268 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1269 final ModuleContext moduleContext = new ModuleContext();
1270 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1271 moduleContext.addCaseType(path, gtb);
1272 genCtx.put(module, moduleContext);
1273 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1275 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1276 .thenReturn(module);
1278 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, augmentNodes, usesNodeParent,
1279 genCtx, false, genTypeBuilder, null };
1280 final Map<Module, ModuleContext> result =
1281 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1282 assertNotNull(result);
1283 assertEquals(result.get(module), moduleContext);