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.mdsal.binding.javav2.spec.structural.Augmentation;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
38 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
39 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
48 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.UsesNode;
50 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
52 public class AugmentToGenTypeTest {
54 @SuppressWarnings("unchecked")
55 @Test(expected = UnsupportedOperationException.class)
56 public void constructorTest() throws Throwable {
57 final Constructor<AugmentToGenType> constructor =
58 (Constructor<AugmentToGenType>) AugmentToGenType.class.getDeclaredConstructors()[0];
59 constructor.setAccessible(true);
60 final Object[] objs = {};
62 constructor.newInstance(objs);
63 } catch (final Exception e) {
68 @SuppressWarnings("rawtypes")
70 public void generateNullModuleTest() throws Exception {
71 final Class[] parameterTypes =
72 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
73 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
74 assertNotNull(generate);
75 generate.setAccessible(true);
77 final SchemaContext context = null;
78 final TypeProvider typeProvider = null;
79 final Map<Module, ModuleContext> genCtx = new HashMap<>();
80 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
81 final Module m = null;
83 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
85 generate.invoke(AugmentToGenType.class, args);
87 } catch (final Exception e) {
89 assertTrue(e instanceof InvocationTargetException);
90 final Throwable cause = e.getCause();
92 assertTrue(cause instanceof IllegalArgumentException);
93 assertEquals("Module reference cannot be NULL.", cause.getMessage());
97 @SuppressWarnings("rawtypes")
99 public void generateNullModuleNameTest() throws Exception {
100 final Class[] parameterTypes =
101 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
102 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
103 assertNotNull(generate);
104 generate.setAccessible(true);
106 final SchemaContext context = null;
107 final TypeProvider typeProvider = null;
108 final Map<Module, ModuleContext> genCtx = new HashMap<>();
109 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
110 final Module m = mock(Module.class);
111 when(m.getName()).thenReturn(null);
113 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
115 generate.invoke(AugmentToGenType.class, args);
117 } catch (final Exception e) {
119 assertTrue(e instanceof InvocationTargetException);
120 final Throwable cause = e.getCause();
121 assertNotNull(cause);
122 assertTrue(cause instanceof IllegalArgumentException);
123 assertEquals("Module name cannot be NULL.", cause.getMessage());
127 @SuppressWarnings("rawtypes")
129 public void generateNullModuleAugmentationsTest() throws Exception {
130 final Class[] parameterTypes =
131 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
132 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
133 assertNotNull(generate);
134 generate.setAccessible(true);
136 final SchemaContext context = null;
137 final TypeProvider typeProvider = null;
138 final Map<Module, ModuleContext> genCtx = new HashMap<>();
139 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
140 final Module m = mock(Module.class);
141 when(m.getName()).thenReturn("name");
142 when(m.getAugmentations()).thenReturn(null);
144 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
146 generate.invoke(AugmentToGenType.class, args);
148 } catch (final Exception e) {
150 assertTrue(e instanceof InvocationTargetException);
151 final Throwable cause = e.getCause();
152 assertNotNull(cause);
153 assertTrue(cause instanceof IllegalStateException);
154 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
158 @SuppressWarnings("rawtypes")
160 public void generateWithoutAugmentationsTest() throws Exception {
161 final Class[] parameterTypes =
162 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
163 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
164 assertNotNull(generate);
165 generate.setAccessible(true);
167 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test.yang");
168 final TypeProvider typeProvider = new TypeProviderImpl(context);
169 final Map<Module, ModuleContext> genCtx = new HashMap<>();
170 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
172 final Object[] args =
173 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
174 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
175 assertNotNull(invoke);
178 @SuppressWarnings({ "rawtypes", "unchecked" })
180 public void generateWithAugmentationsTest() throws Exception {
181 final Class[] parameterTypes =
182 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
183 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
184 assertNotNull(generate);
185 generate.setAccessible(true);
187 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test-augment.yang");
188 final TypeProvider typeProvider = new TypeProviderImpl(context);
189 final Map<Module, ModuleContext> genCtx = mock(Map.class);
190 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
191 final ModuleContext moduleContext = new ModuleContext();
192 moduleContexts.add(moduleContext);
193 final QName create = QName.create("urn:test:simple:test", "2017-02-06", "my-cont");
194 final SchemaNode schemaNode = mock(SchemaNode.class);
195 when(schemaNode.getPath()).thenReturn(SchemaPath.create(true, create));
196 moduleContext.addChildNodeType(schemaNode, new GeneratedTypeBuilderImpl("test", "Test"));
197 when(genCtx.values()).thenReturn(moduleContexts);
198 when(genCtx.get(context.getModules().iterator().next())).thenReturn(moduleContext);
199 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
201 final Object[] args =
202 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
203 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
204 assertNotNull(invoke);
207 @SuppressWarnings("rawtypes")
209 public void resolveAugmentationsNullModuleTest() throws Exception {
210 final Class[] parameterTypes = { Module.class };
211 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
212 assertNotNull(generate);
213 generate.setAccessible(true);
215 final Module m = null;
217 final Object[] args = { m };
219 generate.invoke(AugmentToGenType.class, args);
221 } catch (final Exception e) {
223 assertTrue(e instanceof InvocationTargetException);
224 final Throwable cause = e.getCause();
225 assertNotNull(cause);
226 assertTrue(cause instanceof IllegalArgumentException);
227 assertEquals("Module reference cannot be NULL.", cause.getMessage());
231 @SuppressWarnings("rawtypes")
233 public void resolveAugmentationsNullAugmentationsTest() throws Exception {
234 final Class[] parameterTypes = { Module.class };
235 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
236 assertNotNull(generate);
237 generate.setAccessible(true);
239 final Module m = mock(Module.class);
240 when(m.getAugmentations()).thenReturn(null);
242 final Object[] args = { m };
244 generate.invoke(AugmentToGenType.class, args);
246 } catch (final Exception e) {
248 assertTrue(e instanceof InvocationTargetException);
249 final Throwable cause = e.getCause();
250 assertNotNull(cause);
251 assertTrue(cause instanceof IllegalStateException);
252 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
256 @SuppressWarnings({ "rawtypes", "unchecked" })
258 public void resolveAugmentationsTest() throws Exception {
259 final Class[] parameterTypes = { Module.class };
260 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
261 assertNotNull(generate);
262 generate.setAccessible(true);
264 final Module m = mock(Module.class);
266 final Set<AugmentationSchema> augmentations = new HashSet<>();
268 final QName q1 = QName.create("q1", "2017-04-04", "q1");
269 final QName q2 = QName.create("q2", "2017-04-04", "q2");
270 final QName q3 = QName.create("q3", "2017-04-04", "q3");
271 final QName q4 = QName.create("q4", "2017-04-04", "q4");
272 final QName q5 = QName.create("q5", "2017-04-04", "q5");
274 final AugmentationSchema augmentationSchema1 = mock(AugmentationSchema.class);
275 when(augmentationSchema1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
276 final AugmentationSchema augmentationSchema2 = mock(AugmentationSchema.class);
277 when(augmentationSchema2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
278 augmentations.add(augmentationSchema1);
279 augmentations.add(augmentationSchema2);
281 when(m.getAugmentations()).thenReturn(augmentations);
283 final Object[] args = { m };
285 final List<AugmentationSchema> result =
286 (List<AugmentationSchema>) generate.invoke(AugmentToGenType.class, args);
287 assertNotNull(result);
288 assertTrue(!result.isEmpty());
289 assertEquals(result.get(0), augmentationSchema1);
290 assertEquals(result.get(1), augmentationSchema2);
293 @SuppressWarnings({ "rawtypes" })
295 public void augmentationToGenTypesNullPckgNameTest() throws Exception {
296 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
297 boolean.class, Map.class, Map.class, TypeProvider.class };
298 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
299 assertNotNull(generate);
300 generate.setAccessible(true);
302 final String augmPackName = null;
303 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
304 final SchemaContext context = null;
305 final TypeProvider typeProvider = null;
306 final Map<Module, ModuleContext> genCtx = new HashMap<>();
307 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
308 final Module m = null;
310 final Object[] args = { augmPackName, schemaPathAugmentListEntry, 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());
324 @SuppressWarnings({ "rawtypes" })
326 public void augmentationToGenTypesNullAugSchemaTest() throws Exception {
327 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
328 boolean.class, Map.class, Map.class, TypeProvider.class };
329 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
330 assertNotNull(generate);
331 generate.setAccessible(true);
333 final String augmPackName = "pckg.name";
334 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
335 final SchemaContext context = null;
336 final TypeProvider typeProvider = null;
337 final Map<Module, ModuleContext> genCtx = new HashMap<>();
338 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
339 final Module m = null;
341 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
343 generate.invoke(AugmentToGenType.class, args);
345 } catch (final Exception e) {
347 assertTrue(e instanceof InvocationTargetException);
348 final Throwable cause = e.getCause();
349 assertNotNull(cause);
350 assertTrue(cause instanceof IllegalArgumentException);
351 assertEquals("Augmentation List Entry cannot be NULL.", cause.getMessage());
355 @SuppressWarnings({ "rawtypes" })
357 public void augmentationToGenTypesNullAugSchemaTargetPathTest() throws Exception {
358 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
359 boolean.class, Map.class, Map.class, TypeProvider.class };
360 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
361 assertNotNull(generate);
362 generate.setAccessible(true);
364 final String augmPackName = "pckg.name";
365 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
366 when(augmSchema.getTargetPath()).thenReturn(null);
368 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
369 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
370 when(schemaPathAugmentListEntry.getKey()).thenReturn(null);
371 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
373 final SchemaContext context = null;
374 final TypeProvider typeProvider = null;
375 final Map<Module, ModuleContext> genCtx = new HashMap<>();
376 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
377 final Module m = null;
379 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
381 generate.invoke(AugmentToGenType.class, args);
383 } catch (final Exception e) {
385 assertTrue(e instanceof InvocationTargetException);
386 final Throwable cause = e.getCause();
387 assertNotNull(cause);
388 assertTrue(cause instanceof IllegalStateException);
389 assertEquals("Augmentation List Entry does not contain Target Path (Target Path is NULL).", cause.getMessage());
393 @SuppressWarnings({ "rawtypes" })
395 public void augmentationToGenTypesNullAugSchemaListTest() throws Exception {
396 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
397 boolean.class, Map.class, Map.class, TypeProvider.class };
398 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
399 assertNotNull(generate);
400 generate.setAccessible(true);
402 final String augmPackName = "pckg.name";
403 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
404 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
405 final SchemaPath path = SchemaPath.create(true, qnamePath);
406 when(augmSchema.getTargetPath()).thenReturn(path);
408 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
409 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
410 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
411 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
413 final SchemaContext context = null;
414 final TypeProvider typeProvider = null;
415 final Map<Module, ModuleContext> genCtx = new HashMap<>();
416 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
417 final Module m = null;
419 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
421 generate.invoke(AugmentToGenType.class, args);
423 } catch (final Exception e) {
425 assertTrue(e instanceof InvocationTargetException);
426 final Throwable cause = e.getCause();
427 assertNotNull(cause);
428 assertTrue(cause instanceof IllegalStateException);
429 assertEquals("Augmentation List cannot be empty.", cause.getMessage());
433 @SuppressWarnings({ "rawtypes" })
435 public void augmentationToGenTypesNullAugSchemaTargetNodeTest() throws Exception {
436 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
437 boolean.class, Map.class, Map.class, TypeProvider.class };
438 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
439 assertNotNull(generate);
440 generate.setAccessible(true);
442 final String augmPackName = "pckg.name";
444 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
445 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
446 final SchemaPath path = SchemaPath.create(true, qnamePath);
447 when(augmSchema.getTargetPath()).thenReturn(path);
448 final Set<UsesNode> uses = new HashSet<>();
449 when(augmSchema.getUses()).thenReturn(uses);
451 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
452 augmentationSchemaList.add(augmSchema);
453 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
454 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
455 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
457 final SchemaContext context = mock(SchemaContext.class);
458 final Module moduleAug = mock(Module.class);
459 final DataSchemaNode schNode = null;
460 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
461 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
462 .thenReturn(moduleAug);
464 final TypeProvider typeProvider = null;
465 final Map<Module, ModuleContext> genCtx = new HashMap<>();
466 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
468 final Module m = mock(Module.class);
469 when(m.getName()).thenReturn("augm-module");
470 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
471 when(m.getRevision()).thenReturn(qnamePath.getRevision());
473 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
475 generate.invoke(AugmentToGenType.class, args);
477 } catch (final Exception e) {
479 assertTrue(e instanceof InvocationTargetException);
480 final Throwable cause = e.getCause();
481 assertNotNull(cause);
482 assertTrue(cause instanceof IllegalArgumentException);
483 assertEquals("augment target not found: " + path, cause.getMessage());
487 @SuppressWarnings({ "rawtypes" })
489 public void augmentationToGenTypesNullAugTargetGTBTest() throws Exception {
490 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
491 boolean.class, Map.class, Map.class, TypeProvider.class };
492 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
493 assertNotNull(generate);
494 generate.setAccessible(true);
496 final String augmPackName = "pckg.name";
498 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
499 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
500 final SchemaPath path = SchemaPath.create(true, qnamePath);
501 when(augmSchema.getTargetPath()).thenReturn(path);
502 final Set<UsesNode> uses = new HashSet<>();
503 when(augmSchema.getUses()).thenReturn(uses);
505 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
506 augmentationSchemaList.add(augmSchema);
507 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
508 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
509 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
511 final SchemaContext context = mock(SchemaContext.class);
512 final Module moduleAug = mock(Module.class);
513 final DataSchemaNode schNode = mock(DataSchemaNode.class);
514 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
515 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
516 .thenReturn(moduleAug);
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, schemaPathAugmentListEntry, 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 NullPointerException);
537 assertEquals("Target type not yet generated: " + schNode, cause.getMessage());
541 @SuppressWarnings({ "rawtypes", "unchecked" })
543 public void augmentationToGenTypesAugUsesNullOrigTargetTest() throws Exception {
544 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
545 boolean.class, Map.class, Map.class, TypeProvider.class };
546 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
547 assertNotNull(generate);
548 generate.setAccessible(true);
550 final String augmPackName = "pckg.name";
552 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
553 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
554 final SchemaPath path = SchemaPath.create(true, qnamePath);
555 when(augmSchema.getTargetPath()).thenReturn(path);
556 final Set<UsesNode> uses = new HashSet<>();
557 when(augmSchema.getUses()).thenReturn(uses);
559 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
560 augmentationSchemaList.add(augmSchema);
561 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
562 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
563 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
565 final SchemaContext context = mock(SchemaContext.class);
566 final Module moduleAug = mock(Module.class);
567 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
568 when(targetSchNode.getPath()).thenReturn(path);
569 when(targetSchNode.isAddedByUses()).thenReturn(true);
570 final Optional optionalSchemaNode = Optional.absent();
571 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
572 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
573 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
574 .thenReturn(moduleAug);
576 final TypeProvider typeProvider = null;
577 final Map<Module, ModuleContext> genCtx = new HashMap<>();
578 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
580 final Module m = mock(Module.class);
581 when(m.getName()).thenReturn("augm-module");
582 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
583 when(m.getRevision()).thenReturn(qnamePath.getRevision());
585 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
587 generate.invoke(AugmentToGenType.class, args);
589 } catch (final Exception e) {
591 assertTrue(e instanceof InvocationTargetException);
592 final Throwable cause = e.getCause();
593 assertNotNull(cause);
594 assertTrue(cause instanceof IllegalStateException);
595 assertEquals("Failed to find target node from grouping in augmentation " + augmSchema + " in module "
596 + m.getName(), cause.getMessage());
600 @SuppressWarnings({ "rawtypes" })
602 public void augmentationToGenTypesTargetChoicSchemaNodeTest() throws Exception {
603 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
604 boolean.class, Map.class, Map.class, TypeProvider.class };
605 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
606 assertNotNull(generate);
607 generate.setAccessible(true);
609 final String augmPackName = "pckg.name";
611 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
612 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
613 final SchemaPath path = SchemaPath.create(true, qnamePath);
614 when(augmSchema.getTargetPath()).thenReturn(path);
615 final Set<UsesNode> uses = new HashSet<>();
616 when(augmSchema.getUses()).thenReturn(uses);
618 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
619 augmentationSchemaList.add(augmSchema);
620 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
621 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
622 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
624 final SchemaContext context = mock(SchemaContext.class);
625 final Module moduleAug = mock(Module.class);
626 final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
627 when(targetSchNode.getPath()).thenReturn(path);
628 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
629 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
630 .thenReturn(moduleAug);
632 final TypeProvider typeProvider = null;
634 final Map genCtx = mock(Map.class);
635 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
636 final ModuleContext mc = new ModuleContext();
637 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
638 mc.addChildNodeType(targetSchNode, gtb);
639 moduleContexts.add(mc);
640 when(genCtx.values()).thenReturn(moduleContexts);
642 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
644 final Module m = mock(Module.class);
645 when(m.getName()).thenReturn("augm-module");
646 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
647 when(m.getRevision()).thenReturn(qnamePath.getRevision());
649 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
650 final Map result = (Map) generate.invoke(AugmentToGenType.class, args);
651 assertNotNull(result);
654 @SuppressWarnings({ "rawtypes", "unchecked" })
656 public void augmentationToGenTypesTest() throws Exception {
657 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
658 boolean.class, Map.class, Map.class, TypeProvider.class };
659 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
660 assertNotNull(generate);
661 generate.setAccessible(true);
663 final String augmPackName = "pckg.name";
665 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
666 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
667 final SchemaPath path = SchemaPath.create(true, qnamePath);
668 when(augmSchema.getTargetPath()).thenReturn(path);
669 final Set<UsesNode> uses = new HashSet<>();
670 when(augmSchema.getUses()).thenReturn(uses);
671 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
672 when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
674 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
675 augmentationSchemaList.add(augmSchema);
676 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
677 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
678 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
680 final SchemaContext context = mock(SchemaContext.class);
681 final Module moduleAug = mock(Module.class);
682 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
683 when(targetSchNode.getPath()).thenReturn(path);
684 when(targetSchNode.isAddedByUses()).thenReturn(true);
685 final DataSchemaNode origSchNode = mock(DataSchemaNode.class);
686 when(origSchNode.getPath()).thenReturn(path);
687 when(origSchNode.isAddedByUses()).thenReturn(true);
688 final Optional optionalSchemaNode = Optional.of(origSchNode);
689 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
690 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
691 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
692 .thenReturn(moduleAug);
694 final TypeProvider typeProvider = null;
696 final Map<Module, ModuleContext> genCtx = new HashMap<>();
698 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
699 final ModuleContext mc = new ModuleContext();
700 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
701 mc.addChildNodeType(targetSchNode, gtb);
702 moduleContexts.add(mc);
703 genCtx.put(moduleAug, mc);
705 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
707 when(moduleAug.getName()).thenReturn("augm-module");
708 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
709 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
711 final Object[] args =
712 { augmPackName, schemaPathAugmentListEntry, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
713 final Map<Module, ModuleContext> result =
714 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
715 assertNotNull(result);
716 final ModuleContext moduleContext = result.get(moduleAug);
717 assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
718 assertEquals("pckg.name.data.aug", moduleContext.getAugmentations().get(0).getPackageName());
719 assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
720 assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
724 public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
726 AugmentToGenType.usesAugmentationToGenTypes(null, null, null, null, null, null, null, null, false, null);
727 } catch (final Exception e) {
729 assertTrue(e instanceof IllegalArgumentException);
730 assertEquals(e.getMessage(), "Package Name cannot be NULL.");
735 public void usesAugmentationToGenTypesNullAugSchemaListEntryTest() throws Exception {
737 AugmentToGenType.usesAugmentationToGenTypes(null, "", null, null, null, null, null, null, false, null);
738 } catch (final Exception e) {
740 assertTrue(e instanceof IllegalArgumentException);
741 assertEquals(e.getMessage(), "Augmentation Schema List Entry cannot be NULL.");
746 public void usesAugmentationToGenTypesEmptyAugSchemaListTest() throws Exception {
747 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
749 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
751 } catch (final Exception e) {
753 assertTrue(e instanceof IllegalStateException);
754 assertEquals(e.getMessage(), "Augmentation Schema List cannot be empty");
759 public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
760 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
761 when(augmentationSchema.getTargetPath()).thenReturn(null);
762 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
763 augmentationSchemaList.add(augmentationSchema);
765 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
767 } catch (final Exception e) {
769 assertTrue(e instanceof IllegalStateException);
770 assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
775 public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
776 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
777 final SchemaPath path = SchemaPath.create(true, qnamePath);
778 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
779 when(augmentationSchema.getTargetPath()).thenReturn(path);
780 final Set<UsesNode> uses = new HashSet<>();
781 when(augmentationSchema.getUses()).thenReturn(uses);
783 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
784 augmentationSchemaList.add(augmentationSchema);
786 final SchemaContext context = mock(SchemaContext.class);
787 final Module moduleAug = mock(Module.class);
788 when(moduleAug.getName()).thenReturn("augm-module");
789 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
790 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
791 final Set<GroupingDefinition> groupings = new HashSet<>();
792 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
793 when(groupingDefinition.getQName()).thenReturn(qnamePath);
794 groupings.add(groupingDefinition);
795 when(moduleAug.getGroupings()).thenReturn(groupings);
797 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
798 .thenReturn(moduleAug);
800 final Map<Module, ModuleContext> genCtx = new HashMap<>();
801 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
804 final UsesNode usesNode = mock(UsesNode.class);
805 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
807 when(usesNode.getGroupingPath()).thenReturn(path);
810 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
811 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
812 } catch (final Exception e) {
814 assertTrue(e instanceof IllegalArgumentException);
815 assertEquals(e.getMessage(), "augment target not found: " + path);
820 public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
821 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
822 final SchemaPath path = SchemaPath.create(true, qnamePath);
823 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
824 when(augmentationSchema.getTargetPath()).thenReturn(path);
825 final Set<UsesNode> uses = new HashSet<>();
826 when(augmentationSchema.getUses()).thenReturn(uses);
828 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
829 augmentationSchemaList.add(augmentationSchema);
831 final SchemaContext context = mock(SchemaContext.class);
832 final Module moduleAug = mock(Module.class);
833 when(moduleAug.getName()).thenReturn("augm-module");
834 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
835 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
836 final Set<GroupingDefinition> groupings = new HashSet<>();
837 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
838 when(groupingDefinition.getQName()).thenReturn(qnamePath);
839 final DataSchemaNode schNode = mock(DataSchemaNode.class);
840 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
841 groupings.add(groupingDefinition);
842 when(moduleAug.getGroupings()).thenReturn(groupings);
844 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
845 .thenReturn(moduleAug);
847 final Map<Module, ModuleContext> genCtx = new HashMap<>();
848 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
850 final UsesNode usesNode = mock(UsesNode.class);
851 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
853 when(usesNode.getGroupingPath()).thenReturn(path);
856 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
857 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
858 } catch (final Exception e) {
860 assertTrue(e instanceof NullPointerException);
861 assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
866 public void usesAugmentationToGenTypesTest() throws Exception {
867 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
868 final SchemaPath path = SchemaPath.create(true, qnamePath);
869 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
870 when(augmentationSchema.getTargetPath()).thenReturn(path);
871 final Set<UsesNode> uses = new HashSet<>();
872 when(augmentationSchema.getUses()).thenReturn(uses);
874 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
875 augmentationSchemaList.add(augmentationSchema);
877 final SchemaContext context = mock(SchemaContext.class);
878 final Module moduleAug = mock(Module.class);
879 when(moduleAug.getName()).thenReturn("augm-module");
880 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
881 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
882 final Set<GroupingDefinition> groupings = new HashSet<>();
883 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
884 when(groupingDefinition.getQName()).thenReturn(qnamePath);
885 final DataSchemaNode schNode = mock(DataSchemaNode.class);
886 when(schNode.getPath()).thenReturn(path);
887 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
888 groupings.add(groupingDefinition);
889 when(moduleAug.getGroupings()).thenReturn(groupings);
891 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
892 .thenReturn(moduleAug);
894 final Map<Module, ModuleContext> genCtx = new HashMap<>();
895 final ModuleContext mc = new ModuleContext();
896 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
897 mc.addChildNodeType(schNode, gtb);
898 genCtx.put(moduleAug, mc);
900 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
902 final UsesNode usesNode = mock(UsesNode.class);
903 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
905 when(usesNode.getGroupingPath()).thenReturn(path);
907 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
908 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
909 assertNotNull(result);
913 public void usesAugmentationToGenTypesChoiceTest() throws Exception {
914 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
915 final SchemaPath path = SchemaPath.create(true, qnamePath);
916 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
917 when(augmentationSchema.getTargetPath()).thenReturn(path);
918 final Set<UsesNode> uses = new HashSet<>();
919 when(augmentationSchema.getUses()).thenReturn(uses);
921 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
922 augmentationSchemaList.add(augmentationSchema);
924 final SchemaContext context = mock(SchemaContext.class);
925 final Module moduleAug = mock(Module.class);
926 when(moduleAug.getName()).thenReturn("augm-module");
927 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
928 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
929 final Set<GroupingDefinition> groupings = new HashSet<>();
930 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
931 when(groupingDefinition.getQName()).thenReturn(qnamePath);
932 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
933 when(schNode.getPath()).thenReturn(path);
934 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
935 groupings.add(groupingDefinition);
936 when(moduleAug.getGroupings()).thenReturn(groupings);
938 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
939 .thenReturn(moduleAug);
941 final Map<Module, ModuleContext> genCtx = new HashMap<>();
942 final ModuleContext mc = new ModuleContext();
943 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
944 mc.addChildNodeType(schNode, gtb);
945 genCtx.put(moduleAug, mc);
947 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
949 final UsesNode usesNode = mock(UsesNode.class);
950 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
952 when(usesNode.getGroupingPath()).thenReturn(path);
954 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
955 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
956 assertNotNull(result);
959 @SuppressWarnings({ "rawtypes" })
961 public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
962 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
963 final Method generate =
964 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
965 assertNotNull(generate);
966 generate.setAccessible(true);
968 final Module module = mock(Module.class);
969 final QName qnamePath = QName.create("test", "2017-04-04", "test");
970 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
971 final DataSchemaNode schNode = mock(DataSchemaNode.class);
972 when(schNode.getPath()).thenReturn(schemaPath);
973 when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
975 final SchemaContext context = mock(SchemaContext.class);
976 when(context.findModuleByNamespaceAndRevision(qnamePath .getNamespace(), qnamePath.getRevision()))
978 final UsesNode usesNode = mock(UsesNode.class);
979 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
981 final Object[] args = { context, schemaPath, usesNode };
983 generate.invoke(AugmentToGenType.class, args);
985 } catch (final Exception e) {
987 assertTrue(e instanceof InvocationTargetException);
988 final Throwable cause = e.getCause();
989 assertNotNull(cause);
990 assertTrue(cause instanceof IllegalArgumentException);
991 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
995 @SuppressWarnings({ "rawtypes" })
997 public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
998 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
999 final Method generate =
1000 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1001 assertNotNull(generate);
1002 generate.setAccessible(true);
1004 final Module module = mock(Module.class);
1005 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1006 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1007 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1008 when(schNode.getPath()).thenReturn(schemaPath);
1009 when(schNode.isAddedByUses()).thenReturn(true);
1010 final Set<GroupingDefinition> groupings = new HashSet<>();
1011 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1012 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1013 groupings.add(groupingDefinition);
1014 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1015 when(module.getGroupings()).thenReturn(groupings);
1017 final SchemaContext context = mock(SchemaContext.class);
1018 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1019 .thenReturn(module);
1020 final UsesNode usesNode = mock(UsesNode.class);
1021 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1023 final Object[] args = { context, schemaPath, usesNode };
1025 generate.invoke(AugmentToGenType.class, args);
1027 } catch (final Exception e) {
1029 assertTrue(e instanceof InvocationTargetException);
1030 final Throwable cause = e.getCause();
1031 assertNotNull(cause);
1032 assertTrue(cause instanceof IllegalStateException);
1033 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1037 @SuppressWarnings({ "rawtypes" })
1039 public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
1040 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1041 final Method generate =
1042 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1043 assertNotNull(generate);
1044 generate.setAccessible(true);
1046 final Module module = mock(Module.class);
1047 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1048 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1049 final DataSchemaNode schNode = null;
1050 final Set<GroupingDefinition> groupings = new HashSet<>();
1051 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1052 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1053 groupings.add(groupingDefinition);
1054 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1055 when(module.getGroupings()).thenReturn(groupings);
1057 final SchemaContext context = mock(SchemaContext.class);
1058 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1059 .thenReturn(module);
1060 final UsesNode usesNode = mock(UsesNode.class);
1061 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1063 final Object[] args = { context, schemaPath, usesNode };
1064 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1065 assertEquals(null, result);
1068 @SuppressWarnings({ "rawtypes", "unchecked" })
1070 public void findOriginalTargetFromGroupingTest() throws Exception {
1071 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1072 final Method generate =
1073 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1074 assertNotNull(generate);
1075 generate.setAccessible(true);
1077 final Module module = mock(Module.class);
1078 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1079 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1080 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1081 when(schNode.getPath()).thenReturn(schemaPath);
1082 final Set<GroupingDefinition> groupings = new HashSet<>();
1083 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1084 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1085 groupings.add(groupingDefinition);
1086 final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
1087 when(derivSchNode.isAddedByUses()).thenReturn(true);
1088 final Optional optional = Optional.of(schNode);
1089 when(derivSchNode.getOriginal()).thenReturn(optional);
1090 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
1091 when(module.getGroupings()).thenReturn(groupings);
1093 final SchemaContext context = mock(SchemaContext.class);
1094 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1095 .thenReturn(module);
1096 final UsesNode usesNode = mock(UsesNode.class);
1097 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1099 final Object[] args = { context, schemaPath, usesNode };
1100 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1101 assertEquals(schNode, result);
1104 @SuppressWarnings({ "rawtypes" })
1106 public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1107 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1108 final Method generate =
1109 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1110 assertNotNull(generate);
1111 generate.setAccessible(true);
1113 final Module module = mock(Module.class);
1114 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1115 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1116 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1117 when(schNode.getPath()).thenReturn(schemaPath);
1118 final Set<GroupingDefinition> groupings = new HashSet<>();
1119 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1120 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1121 groupings.add(groupingDefinition);
1122 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1123 when(module.getGroupings()).thenReturn(groupings);
1125 final SchemaContext context = mock(SchemaContext.class);
1126 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1127 .thenReturn(module);
1128 final UsesNode usesNode = mock(UsesNode.class);
1129 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1131 final Object[] args = { context, schemaPath, usesNode };
1132 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1133 assertEquals(schNode, result);
1136 @SuppressWarnings({ "rawtypes" })
1138 public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1139 final Class[] parameterTypes =
1140 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1141 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1142 final Method generate =
1143 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1144 assertNotNull(generate);
1145 generate.setAccessible(true);
1147 final SchemaContext schemaContext = null;
1148 final Module module = null;
1149 final String pckgName = null;
1150 final Type targetType = null;
1151 final ChoiceSchemaNode targetNode = null;
1152 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1153 final DataNodeContainer usesNodeParent = null;
1154 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1155 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1157 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1158 usesNodeParent, genCtx, false, genTypeBuilder, null };
1160 generate.invoke(AugmentToGenType.class, args);
1162 } catch (final Exception e) {
1164 assertTrue(e instanceof InvocationTargetException);
1165 final Throwable cause = e.getCause();
1166 assertNotNull(cause);
1167 assertTrue(cause instanceof IllegalArgumentException);
1168 assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1172 @SuppressWarnings({ "rawtypes" })
1174 public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1175 final Class[] parameterTypes =
1176 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1177 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1178 final Method generate =
1179 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1180 assertNotNull(generate);
1181 generate.setAccessible(true);
1183 final SchemaContext schemaContext = null;
1184 final Module module = null;
1185 final String pckgName = "";
1186 final Type targetType = null;
1187 final ChoiceSchemaNode targetNode = null;
1188 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1189 final DataNodeContainer usesNodeParent = null;
1190 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1191 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1193 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1194 usesNodeParent, genCtx, false, genTypeBuilder, null };
1196 generate.invoke(AugmentToGenType.class, args);
1198 } catch (final Exception e) {
1200 assertTrue(e instanceof InvocationTargetException);
1201 final Throwable cause = e.getCause();
1202 assertNotNull(cause);
1203 assertTrue(cause instanceof IllegalArgumentException);
1204 assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1208 @SuppressWarnings({ "rawtypes" })
1210 public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1211 final Class[] parameterTypes =
1212 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1213 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1214 final Method generate =
1215 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1216 assertNotNull(generate);
1217 generate.setAccessible(true);
1219 final SchemaContext schemaContext = null;
1220 final Module module = null;
1221 final String pckgName = "";
1222 final Type targetType = mock(Type.class);
1223 final ChoiceSchemaNode targetNode = null;
1224 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1225 final DataNodeContainer usesNodeParent = null;
1226 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1227 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1229 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1230 genCtx, false, genTypeBuilder, null };
1232 generate.invoke(AugmentToGenType.class, args);
1234 } catch (final Exception e) {
1236 assertTrue(e instanceof InvocationTargetException);
1237 final Throwable cause = e.getCause();
1238 assertNotNull(cause);
1239 assertTrue(cause instanceof IllegalArgumentException);
1240 assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1244 @SuppressWarnings({ "rawtypes", "unchecked" })
1246 public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1247 final Class[] parameterTypes =
1248 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1249 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1250 final Method generate =
1251 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1252 assertNotNull(generate);
1253 generate.setAccessible(true);
1255 final SchemaContext schemaContext = null;
1256 final Module module = null;
1257 final String pckgName = "";
1258 final Type targetType = mock(Type.class);
1259 final ChoiceSchemaNode targetNode = null;
1260 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1261 final DataSchemaNode caseNode = null;
1262 augmentNodes.add(caseNode);
1264 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1265 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1266 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1267 schemaPathAugmentListEntry.add(augmentationSchema);
1269 final DataNodeContainer usesNodeParent = null;
1270 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1271 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1273 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1274 genCtx, false, genTypeBuilder, null };
1275 final Map<Module, ModuleContext> result =
1276 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1277 assertEquals(genCtx, result);
1280 @SuppressWarnings({ "rawtypes" })
1282 public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1283 final Class[] parameterTypes =
1284 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1285 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1286 final Method generate =
1287 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1288 assertNotNull(generate);
1289 generate.setAccessible(true);
1291 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1292 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1293 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1295 final SchemaContext schemaContext = mock(SchemaContext.class);
1296 final Module module = mock(Module.class);
1297 when(module.getName()).thenReturn("test-module-case");
1298 final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1299 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1300 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1301 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1302 final String pckgName = "test.augment.choice.cases";
1303 final Type targetType = mock(Type.class);
1304 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1305 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1306 when(caseNode.getPath()).thenReturn(path);
1307 when(caseNode.getQName()).thenReturn(qnamePath);
1308 augmentNodes.add(caseNode);
1310 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1311 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1312 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1313 schemaPathAugmentListEntry.add(augmentationSchema);
1315 final DataNodeContainer usesNodeParent = null;
1316 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1317 when(targetNode.getPath()).thenReturn(path);
1318 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1319 final ModuleContext moduleContext = new ModuleContext();
1320 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1321 moduleContext.addCaseType(path, gtb);
1322 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1324 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1325 .thenReturn(module);
1327 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1328 genCtx, false, genTypeBuilder, null };
1330 generate.invoke(AugmentToGenType.class, args);
1332 } catch (final Exception e) {
1334 assertTrue(e instanceof InvocationTargetException);
1335 final Throwable cause = e.getCause();
1336 assertNotNull(cause);
1337 assertTrue(cause instanceof IllegalArgumentException);
1338 assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1342 @SuppressWarnings({ "rawtypes", "unchecked" })
1344 public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1345 final Class[] parameterTypes =
1346 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1347 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1348 final Method generate =
1349 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1350 assertNotNull(generate);
1351 generate.setAccessible(true);
1353 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1354 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1355 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1357 final SchemaContext schemaContext = mock(SchemaContext.class);
1358 final Module module = mock(Module.class);
1359 when(module.getName()).thenReturn("test-module-case");
1360 final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1361 when(schemaNode.getPath()).thenReturn(path);
1362 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1363 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1364 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1365 final String pckgName = "test.augment.choice.cases";
1366 final Type targetType = mock(Type.class);
1367 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1368 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1369 when(caseNode.getPath()).thenReturn(path);
1370 when(caseNode.getQName()).thenReturn(qnamePath);
1371 augmentNodes.add(caseNode);
1373 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1374 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1375 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1376 schemaPathAugmentListEntry.add(augmentationSchema);
1378 final DataNodeContainer usesNodeParent = null;
1379 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1380 when(targetNode.getPath()).thenReturn(path);
1381 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1382 final ModuleContext moduleContext = new ModuleContext();
1383 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1384 moduleContext.addCaseType(path, gtb);
1385 genCtx.put(module, moduleContext);
1386 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1388 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1389 .thenReturn(module);
1391 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1392 genCtx, false, genTypeBuilder, null };
1393 final Map<Module, ModuleContext> result =
1394 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1395 assertNotNull(result);
1396 assertEquals(result.get(module), moduleContext);