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;
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
29 import org.junit.Test;
30 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
31 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
32 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
33 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
34 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
35 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
36 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
37 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
40 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
41 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
43 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.UsesNode;
52 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
54 public class AugmentToGenTypeTest {
56 @SuppressWarnings("unchecked")
57 @Test(expected = UnsupportedOperationException.class)
58 public void constructorTest() throws Throwable {
59 final Constructor<AugmentToGenType> constructor =
60 (Constructor<AugmentToGenType>) AugmentToGenType.class.getDeclaredConstructors()[0];
61 constructor.setAccessible(true);
62 final Object[] objs = {};
64 constructor.newInstance(objs);
65 } catch (final Exception e) {
70 @SuppressWarnings("rawtypes")
72 public void generateNullModuleTest() throws Exception {
73 final Class[] parameterTypes =
74 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
75 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
76 assertNotNull(generate);
77 generate.setAccessible(true);
79 final SchemaContext context = null;
80 final TypeProvider typeProvider = null;
81 final Map<Module, ModuleContext> genCtx = new HashMap<>();
82 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
83 final Module m = null;
85 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
87 generate.invoke(AugmentToGenType.class, args);
89 } catch (final Exception e) {
91 assertTrue(e instanceof InvocationTargetException);
92 final Throwable cause = e.getCause();
94 assertTrue(cause instanceof IllegalArgumentException);
95 assertEquals("Module reference cannot be NULL.", cause.getMessage());
99 @SuppressWarnings("rawtypes")
101 public void generateNullModuleNameTest() throws Exception {
102 final Class[] parameterTypes =
103 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
104 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
105 assertNotNull(generate);
106 generate.setAccessible(true);
108 final SchemaContext context = null;
109 final TypeProvider typeProvider = null;
110 final Map<Module, ModuleContext> genCtx = new HashMap<>();
111 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
112 final Module m = mock(Module.class);
113 when(m.getName()).thenReturn(null);
115 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
117 generate.invoke(AugmentToGenType.class, args);
119 } catch (final Exception e) {
121 assertTrue(e instanceof InvocationTargetException);
122 final Throwable cause = e.getCause();
123 assertNotNull(cause);
124 assertTrue(cause instanceof IllegalArgumentException);
125 assertEquals("Module name cannot be NULL.", cause.getMessage());
129 @SuppressWarnings("rawtypes")
131 public void generateNullModuleAugmentationsTest() throws Exception {
132 final Class[] parameterTypes =
133 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
134 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
135 assertNotNull(generate);
136 generate.setAccessible(true);
138 final SchemaContext context = null;
139 final TypeProvider typeProvider = null;
140 final Map<Module, ModuleContext> genCtx = new HashMap<>();
141 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
142 final Module m = mock(Module.class);
143 when(m.getName()).thenReturn("name");
144 when(m.getAugmentations()).thenReturn(null);
146 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
148 generate.invoke(AugmentToGenType.class, args);
150 } catch (final Exception e) {
152 assertTrue(e instanceof InvocationTargetException);
153 final Throwable cause = e.getCause();
154 assertNotNull(cause);
155 assertTrue(cause instanceof IllegalStateException);
156 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
160 @SuppressWarnings("rawtypes")
162 public void generateWithoutAugmentationsTest() throws Exception {
163 final Class[] parameterTypes =
164 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
165 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
166 assertNotNull(generate);
167 generate.setAccessible(true);
169 final SchemaContext context =
170 YangParserTestUtils.parseYangFiles(new File(getClass().getResource("/generator/test.yang").toURI()));
171 final TypeProvider typeProvider = new TypeProviderImpl(context);
172 final Map<Module, ModuleContext> genCtx = new HashMap<>();
173 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
175 final Object[] args =
176 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
177 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
178 assertNotNull(invoke);
181 @SuppressWarnings({ "rawtypes", "unchecked" })
183 public void generateWithAugmentationsTest() throws Exception {
184 final Class[] parameterTypes =
185 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
186 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
187 assertNotNull(generate);
188 generate.setAccessible(true);
190 final SchemaContext context = YangParserTestUtils
191 .parseYangFiles(new File(getClass().getResource("/generator/test-augment.yang").toURI()));
192 final TypeProvider typeProvider = new TypeProviderImpl(context);
193 final Map<Module, ModuleContext> genCtx = mock(Map.class);
194 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
195 final ModuleContext moduleContext = new ModuleContext();
196 moduleContexts.add(moduleContext);
197 final QName create = QName.create("urn:test:simple:test", "2017-02-06", "my-cont");
198 final SchemaNode schemaNode = mock(SchemaNode.class);
199 when(schemaNode.getPath()).thenReturn(SchemaPath.create(true, create));
200 moduleContext.addChildNodeType(schemaNode, new GeneratedTypeBuilderImpl("test", "Test", moduleContext));
201 when(genCtx.values()).thenReturn(moduleContexts);
202 when(genCtx.get(context.getModules().iterator().next())).thenReturn(moduleContext);
203 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
205 final Object[] args =
206 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
207 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
208 assertNotNull(invoke);
211 @SuppressWarnings("rawtypes")
213 public void resolveAugmentationsNullModuleTest() throws Exception {
214 final Class[] parameterTypes = { Module.class, SchemaContext.class };
215 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
216 assertNotNull(generate);
217 generate.setAccessible(true);
219 final Module m = null;
220 final SchemaContext schemaContext = null;
222 final Object[] args = { m, schemaContext };
224 generate.invoke(AugmentToGenType.class, args);
226 } catch (final Exception e) {
228 assertTrue(e instanceof InvocationTargetException);
229 final Throwable cause = e.getCause();
230 assertNotNull(cause);
231 assertTrue(cause instanceof IllegalArgumentException);
232 assertEquals("Module reference cannot be NULL.", cause.getMessage());
236 @SuppressWarnings("rawtypes")
238 public void resolveAugmentationsNullAugmentationsTest() throws Exception {
239 final Class[] parameterTypes = { Module.class, SchemaContext.class };
240 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
241 assertNotNull(generate);
242 generate.setAccessible(true);
244 final Module m = mock(Module.class);
245 when(m.getAugmentations()).thenReturn(null);
246 final SchemaContext schemaContext = mock(SchemaContext.class);
248 final Object[] args = { m, schemaContext };
250 generate.invoke(AugmentToGenType.class, args);
252 } catch (final Exception e) {
254 assertTrue(e instanceof InvocationTargetException);
255 final Throwable cause = e.getCause();
256 assertNotNull(cause);
257 assertTrue(cause instanceof IllegalStateException);
258 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
262 @SuppressWarnings({ "rawtypes", "unchecked" })
264 public void resolveAugmentationsTest() throws Exception {
265 final Class[] parameterTypes = { Module.class, SchemaContext.class };
266 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
267 assertNotNull(generate);
268 generate.setAccessible(true);
270 final Module m = mock(Module.class);
271 final Module m2 = mock(Module.class);
272 final SchemaContext schemaContext = mock(SchemaContext.class);
274 final Set<AugmentationSchema> augmentations = new HashSet<>();
276 final QName q1 = QName.create("q1", "2017-04-04", "q1");
277 final QName q2 = QName.create("q2", "2017-04-04", "q2");
278 final QName q3 = QName.create("q3", "2017-04-04", "q3");
279 final QName q4 = QName.create("q4", "2017-04-04", "q4");
280 final QName q5 = QName.create("q5", "2017-04-04", "q5");
282 final AugmentationSchema augmentationSchema1 = mock(AugmentationSchema.class);
283 when(augmentationSchema1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
284 final AugmentationSchema augmentationSchema2 = mock(AugmentationSchema.class);
285 when(augmentationSchema2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
286 augmentations.add(augmentationSchema1);
287 augmentations.add(augmentationSchema2);
289 when(m.getAugmentations()).thenReturn(augmentations);
290 when(schemaContext.findModuleByNamespaceAndRevision(q1.getNamespace(), q1.getRevision())).thenReturn(m2);
291 when(schemaContext.findModuleByNamespaceAndRevision(q3.getNamespace(), q3.getRevision())).thenReturn(m2);
293 final Object[] args = { m, schemaContext };
295 final List<AugmentationSchema> result =
296 (List<AugmentationSchema>) generate.invoke(AugmentToGenType.class, args);
297 assertNotNull(result);
298 assertTrue(!result.isEmpty());
299 assertEquals(result.get(0), augmentationSchema1);
300 assertEquals(result.get(1), augmentationSchema2);
303 @SuppressWarnings({ "rawtypes" })
305 public void augmentationToGenTypesNullPckgNameTest() throws Exception {
306 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
307 boolean.class, Map.class, Map.class, TypeProvider.class };
308 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
309 assertNotNull(generate);
310 generate.setAccessible(true);
312 final String augmPackName = null;
313 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
314 final SchemaContext context = null;
315 final TypeProvider typeProvider = null;
316 final Map<Module, ModuleContext> genCtx = new HashMap<>();
317 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
318 final Module m = null;
320 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
322 generate.invoke(AugmentToGenType.class, args);
324 } catch (final Exception e) {
326 assertTrue(e instanceof InvocationTargetException);
327 final Throwable cause = e.getCause();
328 assertNotNull(cause);
329 assertTrue(cause instanceof IllegalArgumentException);
330 assertEquals("Package Name cannot be NULL.", cause.getMessage());
334 @SuppressWarnings({ "rawtypes" })
336 public void augmentationToGenTypesNullAugSchemaTest() throws Exception {
337 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
338 boolean.class, Map.class, Map.class, TypeProvider.class };
339 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
340 assertNotNull(generate);
341 generate.setAccessible(true);
343 final String augmPackName = "pckg.name";
344 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
345 final SchemaContext context = null;
346 final TypeProvider typeProvider = null;
347 final Map<Module, ModuleContext> genCtx = new HashMap<>();
348 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
349 final Module m = null;
351 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
353 generate.invoke(AugmentToGenType.class, args);
355 } catch (final Exception e) {
357 assertTrue(e instanceof InvocationTargetException);
358 final Throwable cause = e.getCause();
359 assertNotNull(cause);
360 assertTrue(cause instanceof IllegalArgumentException);
361 assertEquals("Augmentation List Entry cannot be NULL.", cause.getMessage());
365 @SuppressWarnings({ "rawtypes" })
367 public void augmentationToGenTypesNullAugSchemaTargetPathTest() throws Exception {
368 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
369 boolean.class, Map.class, Map.class, TypeProvider.class };
370 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
371 assertNotNull(generate);
372 generate.setAccessible(true);
374 final String augmPackName = "pckg.name";
375 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
376 when(augmSchema.getTargetPath()).thenReturn(null);
378 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
379 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
380 when(schemaPathAugmentListEntry.getKey()).thenReturn(null);
381 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
383 final SchemaContext context = null;
384 final TypeProvider typeProvider = null;
385 final Map<Module, ModuleContext> genCtx = new HashMap<>();
386 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
387 final Module m = null;
389 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
391 generate.invoke(AugmentToGenType.class, args);
393 } catch (final Exception e) {
395 assertTrue(e instanceof InvocationTargetException);
396 final Throwable cause = e.getCause();
397 assertNotNull(cause);
398 assertTrue(cause instanceof IllegalStateException);
399 assertEquals("Augmentation List Entry does not contain Target Path (Target Path is NULL).", cause.getMessage());
403 @SuppressWarnings({ "rawtypes" })
405 public void augmentationToGenTypesNullAugSchemaListTest() throws Exception {
406 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
407 boolean.class, Map.class, Map.class, TypeProvider.class };
408 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
409 assertNotNull(generate);
410 generate.setAccessible(true);
412 final String augmPackName = "pckg.name";
413 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
414 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
415 final SchemaPath path = SchemaPath.create(true, qnamePath);
416 when(augmSchema.getTargetPath()).thenReturn(path);
418 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
419 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
420 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
421 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
423 final SchemaContext context = null;
424 final TypeProvider typeProvider = null;
425 final Map<Module, ModuleContext> genCtx = new HashMap<>();
426 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
427 final Module m = null;
429 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
431 generate.invoke(AugmentToGenType.class, args);
433 } catch (final Exception e) {
435 assertTrue(e instanceof InvocationTargetException);
436 final Throwable cause = e.getCause();
437 assertNotNull(cause);
438 assertTrue(cause instanceof IllegalStateException);
439 assertEquals("Augmentation List cannot be empty.", cause.getMessage());
443 @SuppressWarnings({ "rawtypes" })
445 public void augmentationToGenTypesNullAugSchemaTargetNodeTest() throws Exception {
446 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
447 boolean.class, Map.class, Map.class, TypeProvider.class };
448 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
449 assertNotNull(generate);
450 generate.setAccessible(true);
452 final String augmPackName = "pckg.name";
454 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
455 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
456 final SchemaPath path = SchemaPath.create(true, qnamePath);
457 when(augmSchema.getTargetPath()).thenReturn(path);
458 final Set<UsesNode> uses = new HashSet<>();
459 when(augmSchema.getUses()).thenReturn(uses);
461 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
462 augmentationSchemaList.add(augmSchema);
463 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
464 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
465 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
467 final SchemaContext context = mock(SchemaContext.class);
468 final Module moduleAug = mock(Module.class);
469 final DataSchemaNode schNode = null;
470 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
471 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
472 .thenReturn(moduleAug);
474 final TypeProvider typeProvider = null;
475 final Map<Module, ModuleContext> genCtx = new HashMap<>();
476 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
478 final Module m = mock(Module.class);
479 when(m.getName()).thenReturn("augm-module");
480 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
481 when(m.getRevision()).thenReturn(qnamePath.getRevision());
483 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
485 generate.invoke(AugmentToGenType.class, args);
487 } catch (final Exception e) {
489 assertTrue(e instanceof InvocationTargetException);
490 final Throwable cause = e.getCause();
491 assertNotNull(cause);
492 assertTrue(cause instanceof IllegalArgumentException);
493 assertEquals("augment target not found: " + path, cause.getMessage());
497 @SuppressWarnings({ "rawtypes" })
499 public void augmentationToGenTypesNullAugTargetGTBTest() throws Exception {
500 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
501 boolean.class, Map.class, Map.class, TypeProvider.class };
502 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
503 assertNotNull(generate);
504 generate.setAccessible(true);
506 final String augmPackName = "pckg.name";
508 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
509 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
510 final SchemaPath path = SchemaPath.create(true, qnamePath);
511 when(augmSchema.getTargetPath()).thenReturn(path);
512 final Set<UsesNode> uses = new HashSet<>();
513 when(augmSchema.getUses()).thenReturn(uses);
515 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
516 augmentationSchemaList.add(augmSchema);
517 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
518 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
519 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
521 final SchemaContext context = mock(SchemaContext.class);
522 final Module moduleAug = mock(Module.class);
523 final DataSchemaNode schNode = mock(DataSchemaNode.class);
524 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
525 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
526 .thenReturn(moduleAug);
528 final TypeProvider typeProvider = null;
529 final Map<Module, ModuleContext> genCtx = new HashMap<>();
530 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
532 final Module m = mock(Module.class);
533 when(m.getName()).thenReturn("augm-module");
534 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
535 when(m.getRevision()).thenReturn(qnamePath.getRevision());
537 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
539 generate.invoke(AugmentToGenType.class, args);
541 } catch (final Exception e) {
543 assertTrue(e instanceof InvocationTargetException);
544 final Throwable cause = e.getCause();
545 assertNotNull(cause);
546 assertTrue(cause instanceof NullPointerException);
547 assertEquals("Target type not yet generated: " + schNode, cause.getMessage());
551 @SuppressWarnings({ "rawtypes", "unchecked" })
553 public void augmentationToGenTypesAugUsesNullOrigTargetTest() throws Exception {
554 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
555 boolean.class, Map.class, Map.class, TypeProvider.class };
556 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
557 assertNotNull(generate);
558 generate.setAccessible(true);
560 final String augmPackName = "pckg.name";
562 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
563 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
564 final SchemaPath path = SchemaPath.create(true, qnamePath);
565 when(augmSchema.getTargetPath()).thenReturn(path);
566 final Set<UsesNode> uses = new HashSet<>();
567 when(augmSchema.getUses()).thenReturn(uses);
569 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
570 augmentationSchemaList.add(augmSchema);
571 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
572 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
573 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
575 final SchemaContext context = mock(SchemaContext.class);
576 final Module moduleAug = mock(Module.class);
577 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
578 when(targetSchNode.getPath()).thenReturn(path);
579 when(targetSchNode.isAddedByUses()).thenReturn(true);
580 final Optional optionalSchemaNode = Optional.absent();
581 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
582 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
583 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
584 .thenReturn(moduleAug);
586 final TypeProvider typeProvider = null;
587 final Map<Module, ModuleContext> genCtx = new HashMap<>();
588 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
590 final Module m = mock(Module.class);
591 when(m.getName()).thenReturn("augm-module");
592 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
593 when(m.getRevision()).thenReturn(qnamePath.getRevision());
595 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
597 generate.invoke(AugmentToGenType.class, args);
599 } catch (final Exception e) {
601 assertTrue(e instanceof InvocationTargetException);
602 final Throwable cause = e.getCause();
603 assertNotNull(cause);
604 assertTrue(cause instanceof IllegalStateException);
605 assertEquals("Failed to find target node from grouping in augmentation " + augmSchema + " in module "
606 + m.getName(), cause.getMessage());
610 @SuppressWarnings({ "rawtypes" })
612 public void augmentationToGenTypesTargetChoicSchemaNodeTest() throws Exception {
613 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
614 boolean.class, Map.class, Map.class, TypeProvider.class };
615 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
616 assertNotNull(generate);
617 generate.setAccessible(true);
619 final String augmPackName = "pckg.name";
621 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
622 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
623 final SchemaPath path = SchemaPath.create(true, qnamePath);
624 when(augmSchema.getTargetPath()).thenReturn(path);
625 final Set<UsesNode> uses = new HashSet<>();
626 when(augmSchema.getUses()).thenReturn(uses);
628 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
629 augmentationSchemaList.add(augmSchema);
630 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
631 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
632 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
634 final SchemaContext context = mock(SchemaContext.class);
635 final Module moduleAug = mock(Module.class);
636 final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
637 when(targetSchNode.getPath()).thenReturn(path);
638 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
639 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
640 .thenReturn(moduleAug);
642 final TypeProvider typeProvider = null;
644 final Map genCtx = mock(Map.class);
645 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
646 final ModuleContext mc = new ModuleContext();
647 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm", mc);
648 mc.addChildNodeType(targetSchNode, gtb);
649 moduleContexts.add(mc);
650 when(genCtx.values()).thenReturn(moduleContexts);
652 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
654 final Module m = mock(Module.class);
655 when(m.getName()).thenReturn("augm-module");
656 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
657 when(m.getRevision()).thenReturn(qnamePath.getRevision());
659 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
660 final Map result = (Map) generate.invoke(AugmentToGenType.class, args);
661 assertNotNull(result);
664 @SuppressWarnings({ "rawtypes", "unchecked" })
666 public void augmentationToGenTypesTest() throws Exception {
667 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
668 boolean.class, Map.class, Map.class, TypeProvider.class };
669 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
670 assertNotNull(generate);
671 generate.setAccessible(true);
673 final String augmPackName = "pckg.name";
675 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
676 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
677 final SchemaPath path = SchemaPath.create(true, qnamePath);
678 when(augmSchema.getTargetPath()).thenReturn(path);
679 final Set<UsesNode> uses = new HashSet<>();
680 when(augmSchema.getUses()).thenReturn(uses);
681 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
682 when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
684 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
685 augmentationSchemaList.add(augmSchema);
686 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
687 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
688 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
690 final SchemaContext context = mock(SchemaContext.class);
691 final Module moduleAug = mock(Module.class);
692 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
693 when(targetSchNode.getPath()).thenReturn(path);
694 when(targetSchNode.isAddedByUses()).thenReturn(true);
695 final DataSchemaNode origSchNode = mock(DataSchemaNode.class);
696 when(origSchNode.getPath()).thenReturn(path);
697 when(origSchNode.isAddedByUses()).thenReturn(true);
698 when(origSchNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
699 final Optional optionalSchemaNode = Optional.of(origSchNode);
700 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
701 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
702 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
703 .thenReturn(moduleAug);
705 final TypeProvider typeProvider = null;
707 final Map<Module, ModuleContext> genCtx = new HashMap<>();
709 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
710 final ModuleContext mc = new ModuleContext();
711 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm", mc);
712 mc.addChildNodeType(targetSchNode, gtb);
713 moduleContexts.add(mc);
714 genCtx.put(moduleAug, mc);
716 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
718 when(moduleAug.getName()).thenReturn("augm-module");
719 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
720 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
722 final Object[] args =
723 { augmPackName, schemaPathAugmentListEntry, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
724 final Map<Module, ModuleContext> result =
725 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
726 assertNotNull(result);
727 final ModuleContext moduleContext = result.get(moduleAug);
728 assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
729 assertEquals("pckg.name.data", moduleContext.getAugmentations().get(0).getPackageName());
730 assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
731 assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
736 public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
738 AugmentToGenType.usesAugmentationToGenTypes(null, null, null, null, null, null, null, null, false, null, null);
739 } catch (final Exception e) {
741 assertTrue(e instanceof IllegalArgumentException);
742 assertEquals(e.getMessage(), "Package Name cannot be NULL.");
748 public void usesAugmentationToGenTypesNullAugSchemaListEntryTest() throws Exception {
750 AugmentToGenType.usesAugmentationToGenTypes(null, "", null, null, null, null, null, null, false, null, null);
751 } catch (final Exception e) {
753 assertTrue(e instanceof IllegalArgumentException);
754 assertEquals(e.getMessage(), "Augmentation Schema List Entry cannot be NULL.");
760 public void usesAugmentationToGenTypesEmptyAugSchemaListTest() throws Exception {
761 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
763 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
765 } catch (final Exception e) {
767 assertTrue(e instanceof IllegalStateException);
768 assertEquals(e.getMessage(), "Augmentation Schema List cannot be empty");
774 public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
775 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
776 when(augmentationSchema.getTargetPath()).thenReturn(null);
777 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
778 augmentationSchemaList.add(augmentationSchema);
780 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
782 } catch (final Exception e) {
784 assertTrue(e instanceof IllegalStateException);
785 assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
791 public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
792 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
793 final SchemaPath path = SchemaPath.create(true, qnamePath);
794 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
795 when(augmentationSchema.getTargetPath()).thenReturn(path);
796 final Set<UsesNode> uses = new HashSet<>();
797 when(augmentationSchema.getUses()).thenReturn(uses);
799 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
800 augmentationSchemaList.add(augmentationSchema);
802 final SchemaContext context = mock(SchemaContext.class);
803 final Module moduleAug = mock(Module.class);
804 when(moduleAug.getName()).thenReturn("augm-module");
805 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
806 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
807 final Set<GroupingDefinition> groupings = new HashSet<>();
808 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
809 when(groupingDefinition.getQName()).thenReturn(qnamePath);
810 groupings.add(groupingDefinition);
811 when(moduleAug.getGroupings()).thenReturn(groupings);
813 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
814 .thenReturn(moduleAug);
816 final Map<Module, ModuleContext> genCtx = new HashMap<>();
817 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
820 final UsesNode usesNode = mock(UsesNode.class);
821 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
823 when(usesNode.getGroupingPath()).thenReturn(path);
826 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
827 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
828 } catch (final Exception e) {
830 assertTrue(e instanceof IllegalArgumentException);
831 assertEquals(e.getMessage(), "augment target not found: " + path);
837 public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
838 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
839 final SchemaPath path = SchemaPath.create(true, qnamePath);
840 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
841 when(augmentationSchema.getTargetPath()).thenReturn(path);
842 final Set<UsesNode> uses = new HashSet<>();
843 when(augmentationSchema.getUses()).thenReturn(uses);
845 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
846 augmentationSchemaList.add(augmentationSchema);
848 final SchemaContext context = mock(SchemaContext.class);
849 final Module moduleAug = mock(Module.class);
850 when(moduleAug.getName()).thenReturn("augm-module");
851 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
852 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
853 final Set<GroupingDefinition> groupings = new HashSet<>();
854 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
855 when(groupingDefinition.getQName()).thenReturn(qnamePath);
856 final DataSchemaNode schNode = mock(DataSchemaNode.class);
857 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
858 groupings.add(groupingDefinition);
859 when(moduleAug.getGroupings()).thenReturn(groupings);
861 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
862 .thenReturn(moduleAug);
864 final Map<Module, ModuleContext> genCtx = new HashMap<>();
865 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
867 final UsesNode usesNode = mock(UsesNode.class);
868 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
870 when(usesNode.getGroupingPath()).thenReturn(path);
873 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
874 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
875 } catch (final Exception e) {
877 assertTrue(e instanceof NullPointerException);
878 assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
884 public void usesAugmentationToGenTypesTest() throws Exception {
885 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
886 final SchemaPath path = SchemaPath.create(true, qnamePath);
887 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
888 when(augmentationSchema.getTargetPath()).thenReturn(path);
889 final Set<UsesNode> uses = new HashSet<>();
890 when(augmentationSchema.getUses()).thenReturn(uses);
892 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
893 augmentationSchemaList.add(augmentationSchema);
895 final SchemaContext context = mock(SchemaContext.class);
896 final Module moduleAug = mock(Module.class);
897 when(moduleAug.getName()).thenReturn("augm-module");
898 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
899 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
900 final Set<GroupingDefinition> groupings = new HashSet<>();
901 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
902 when(groupingDefinition.getQName()).thenReturn(qnamePath);
903 final DataSchemaNode schNode = mock(DataSchemaNode.class);
904 when(schNode.getPath()).thenReturn(path);
905 when(schNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
906 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
907 groupings.add(groupingDefinition);
908 when(moduleAug.getGroupings()).thenReturn(groupings);
910 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
911 .thenReturn(moduleAug);
913 final Map<Module, ModuleContext> genCtx = new HashMap<>();
914 final ModuleContext mc = new ModuleContext();
915 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm", mc);
916 mc.addChildNodeType(schNode, gtb);
917 genCtx.put(moduleAug, mc);
919 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
921 final UsesNode usesNode = mock(UsesNode.class);
922 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
924 when(usesNode.getGroupingPath()).thenReturn(path);
926 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
927 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
928 BindingNamespaceType.Data);
929 assertNotNull(result);
934 public void usesAugmentationToGenTypesChoiceTest() throws Exception {
935 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
936 final SchemaPath path = SchemaPath.create(true, qnamePath);
937 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
938 when(augmentationSchema.getTargetPath()).thenReturn(path);
939 final Set<UsesNode> uses = new HashSet<>();
940 when(augmentationSchema.getUses()).thenReturn(uses);
942 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
943 augmentationSchemaList.add(augmentationSchema);
945 final SchemaContext context = mock(SchemaContext.class);
946 final Module moduleAug = mock(Module.class);
947 when(moduleAug.getName()).thenReturn("augm-module");
948 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
949 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
950 final Set<GroupingDefinition> groupings = new HashSet<>();
951 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
952 when(groupingDefinition.getQName()).thenReturn(qnamePath);
953 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
954 when(schNode.getPath()).thenReturn(path);
955 when(schNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
956 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
957 groupings.add(groupingDefinition);
958 when(moduleAug.getGroupings()).thenReturn(groupings);
960 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
961 .thenReturn(moduleAug);
963 final Map<Module, ModuleContext> genCtx = new HashMap<>();
964 final ModuleContext mc = new ModuleContext();
965 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm", mc);
966 mc.addChildNodeType(schNode, gtb);
967 genCtx.put(moduleAug, mc);
969 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
971 final UsesNode usesNode = mock(UsesNode.class);
972 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
974 when(usesNode.getGroupingPath()).thenReturn(path);
976 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
977 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
978 BindingNamespaceType.Data);
979 assertNotNull(result);
982 @SuppressWarnings({ "rawtypes" })
984 public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
985 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
986 final Method generate =
987 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
988 assertNotNull(generate);
989 generate.setAccessible(true);
991 final Module module = mock(Module.class);
992 final QName qnamePath = QName.create("test", "2017-04-04", "test");
993 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
994 final DataSchemaNode schNode = mock(DataSchemaNode.class);
995 when(schNode.getPath()).thenReturn(schemaPath);
996 when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
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 };
1006 generate.invoke(AugmentToGenType.class, args);
1008 } catch (final Exception e) {
1010 assertTrue(e instanceof InvocationTargetException);
1011 final Throwable cause = e.getCause();
1012 assertNotNull(cause);
1013 assertTrue(cause instanceof IllegalArgumentException);
1014 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1018 @SuppressWarnings({ "rawtypes" })
1020 public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
1021 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1022 final Method generate =
1023 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1024 assertNotNull(generate);
1025 generate.setAccessible(true);
1027 final Module module = mock(Module.class);
1028 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1029 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1030 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1031 when(schNode.getPath()).thenReturn(schemaPath);
1032 when(schNode.isAddedByUses()).thenReturn(true);
1033 final Set<GroupingDefinition> groupings = new HashSet<>();
1034 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1035 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1036 groupings.add(groupingDefinition);
1037 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1038 when(module.getGroupings()).thenReturn(groupings);
1040 final SchemaContext context = mock(SchemaContext.class);
1041 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1042 .thenReturn(module);
1043 final UsesNode usesNode = mock(UsesNode.class);
1044 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1046 final Object[] args = { context, schemaPath, usesNode };
1048 generate.invoke(AugmentToGenType.class, args);
1050 } catch (final Exception e) {
1052 assertTrue(e instanceof InvocationTargetException);
1053 final Throwable cause = e.getCause();
1054 assertNotNull(cause);
1055 assertTrue(cause instanceof IllegalStateException);
1056 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1060 @SuppressWarnings({ "rawtypes" })
1062 public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
1063 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1064 final Method generate =
1065 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1066 assertNotNull(generate);
1067 generate.setAccessible(true);
1069 final Module module = mock(Module.class);
1070 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1071 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1072 final DataSchemaNode schNode = null;
1073 final Set<GroupingDefinition> groupings = new HashSet<>();
1074 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1075 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1076 groupings.add(groupingDefinition);
1077 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1078 when(module.getGroupings()).thenReturn(groupings);
1080 final SchemaContext context = mock(SchemaContext.class);
1081 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1082 .thenReturn(module);
1083 final UsesNode usesNode = mock(UsesNode.class);
1084 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1086 final Object[] args = { context, schemaPath, usesNode };
1087 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1088 assertEquals(null, result);
1091 @SuppressWarnings({ "rawtypes", "unchecked" })
1093 public void findOriginalTargetFromGroupingTest() throws Exception {
1094 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1095 final Method generate =
1096 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1097 assertNotNull(generate);
1098 generate.setAccessible(true);
1100 final Module module = mock(Module.class);
1101 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1102 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1103 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1104 when(schNode.getPath()).thenReturn(schemaPath);
1105 final Set<GroupingDefinition> groupings = new HashSet<>();
1106 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1107 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1108 groupings.add(groupingDefinition);
1109 final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
1110 when(derivSchNode.isAddedByUses()).thenReturn(true);
1111 final Optional optional = Optional.of(schNode);
1112 when(derivSchNode.getOriginal()).thenReturn(optional);
1113 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
1114 when(module.getGroupings()).thenReturn(groupings);
1116 final SchemaContext context = mock(SchemaContext.class);
1117 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1118 .thenReturn(module);
1119 final UsesNode usesNode = mock(UsesNode.class);
1120 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1122 final Object[] args = { context, schemaPath, usesNode };
1123 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1124 assertEquals(schNode, result);
1127 @SuppressWarnings({ "rawtypes" })
1129 public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1130 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1131 final Method generate =
1132 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1133 assertNotNull(generate);
1134 generate.setAccessible(true);
1136 final Module module = mock(Module.class);
1137 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1138 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1139 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1140 when(schNode.getPath()).thenReturn(schemaPath);
1141 final Set<GroupingDefinition> groupings = new HashSet<>();
1142 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1143 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1144 groupings.add(groupingDefinition);
1145 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1146 when(module.getGroupings()).thenReturn(groupings);
1148 final SchemaContext context = mock(SchemaContext.class);
1149 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1150 .thenReturn(module);
1151 final UsesNode usesNode = mock(UsesNode.class);
1152 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1154 final Object[] args = { context, schemaPath, usesNode };
1155 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1156 assertEquals(schNode, result);
1159 @SuppressWarnings({ "rawtypes" })
1161 public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1162 final Class[] parameterTypes =
1163 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1164 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1165 BindingNamespaceType.class};
1166 final Method generate =
1167 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1168 assertNotNull(generate);
1169 generate.setAccessible(true);
1171 final SchemaContext schemaContext = null;
1172 final Module module = null;
1173 final String pckgName = null;
1174 final Type targetType = null;
1175 final ChoiceSchemaNode targetNode = null;
1176 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1177 final DataNodeContainer usesNodeParent = null;
1178 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1179 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1181 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1182 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1184 generate.invoke(AugmentToGenType.class, args);
1186 } catch (final Exception e) {
1188 assertTrue(e instanceof InvocationTargetException);
1189 final Throwable cause = e.getCause();
1190 assertNotNull(cause);
1191 assertTrue(cause instanceof IllegalArgumentException);
1192 assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1196 @SuppressWarnings({ "rawtypes" })
1198 public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1199 final Class[] parameterTypes =
1200 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1201 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1202 BindingNamespaceType.class };
1203 final Method generate =
1204 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1205 assertNotNull(generate);
1206 generate.setAccessible(true);
1208 final SchemaContext schemaContext = null;
1209 final Module module = null;
1210 final String pckgName = "";
1211 final Type targetType = null;
1212 final ChoiceSchemaNode targetNode = null;
1213 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1214 final DataNodeContainer usesNodeParent = null;
1215 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1216 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1218 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1219 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1221 generate.invoke(AugmentToGenType.class, args);
1223 } catch (final Exception e) {
1225 assertTrue(e instanceof InvocationTargetException);
1226 final Throwable cause = e.getCause();
1227 assertNotNull(cause);
1228 assertTrue(cause instanceof IllegalArgumentException);
1229 assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1233 @SuppressWarnings({ "rawtypes" })
1235 public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1236 final Class[] parameterTypes =
1237 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1238 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1239 BindingNamespaceType.class };
1240 final Method generate =
1241 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1242 assertNotNull(generate);
1243 generate.setAccessible(true);
1245 final SchemaContext schemaContext = null;
1246 final Module module = null;
1247 final String pckgName = "";
1248 final Type targetType = mock(Type.class);
1249 final ChoiceSchemaNode targetNode = null;
1250 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1251 final DataNodeContainer usesNodeParent = null;
1252 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1253 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1255 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1256 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1258 generate.invoke(AugmentToGenType.class, args);
1260 } catch (final Exception e) {
1262 assertTrue(e instanceof InvocationTargetException);
1263 final Throwable cause = e.getCause();
1264 assertNotNull(cause);
1265 assertTrue(cause instanceof IllegalArgumentException);
1266 assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1270 @SuppressWarnings({ "rawtypes", "unchecked" })
1272 public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1273 final Class[] parameterTypes =
1274 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1275 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1276 BindingNamespaceType.class };
1277 final Method generate =
1278 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1279 assertNotNull(generate);
1280 generate.setAccessible(true);
1282 final SchemaContext schemaContext = null;
1283 final Module module = null;
1284 final String pckgName = "";
1285 final Type targetType = mock(Type.class);
1286 final ChoiceSchemaNode targetNode = null;
1287 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1288 final DataSchemaNode caseNode = null;
1289 augmentNodes.add(caseNode);
1291 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1292 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1293 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1294 schemaPathAugmentListEntry.add(augmentationSchema);
1296 final DataNodeContainer usesNodeParent = null;
1297 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1298 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1300 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1301 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1302 final Map<Module, ModuleContext> result =
1303 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1304 assertEquals(genCtx, result);
1307 @SuppressWarnings({ "rawtypes" })
1309 public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1310 final Class[] parameterTypes =
1311 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1312 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1313 BindingNamespaceType.class };
1314 final Method generate =
1315 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1316 assertNotNull(generate);
1317 generate.setAccessible(true);
1319 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1320 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1321 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1323 final SchemaContext schemaContext = mock(SchemaContext.class);
1324 final Module module = mock(Module.class);
1325 when(module.getName()).thenReturn("test-module-case");
1326 final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1327 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1328 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1329 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1330 final String pckgName = "test.augment.choice.cases";
1331 final Type targetType = mock(Type.class);
1332 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1333 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1334 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1335 when(caseNode.getPath()).thenReturn(path);
1336 when(caseNode.getQName()).thenReturn(qnamePath);
1337 augmentNodes.add(caseNode);
1339 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1340 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1341 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1342 schemaPathAugmentListEntry.add(augmentationSchema);
1344 final DataNodeContainer usesNodeParent = null;
1345 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1346 when(targetNode.getPath()).thenReturn(path);
1347 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1348 genCtx.put(module, new ModuleContext());
1349 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment", genCtx.get(module));
1350 genCtx.get(module).addCaseType(path, gtb);
1351 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1353 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1354 .thenReturn(module);
1356 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1357 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1359 generate.invoke(AugmentToGenType.class, args);
1361 } catch (final Exception e) {
1363 assertTrue(e instanceof InvocationTargetException);
1364 final Throwable cause = e.getCause();
1365 assertNotNull(cause);
1366 assertTrue(cause instanceof IllegalArgumentException);
1367 assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1371 @SuppressWarnings({ "rawtypes", "unchecked" })
1373 public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1374 final Class[] parameterTypes =
1375 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1376 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1377 BindingNamespaceType.class };
1378 final Method generate =
1379 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1380 assertNotNull(generate);
1381 generate.setAccessible(true);
1383 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1384 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1385 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1387 final SchemaContext schemaContext = mock(SchemaContext.class);
1388 final Module module = mock(Module.class);
1389 when(module.getName()).thenReturn("test-module-case");
1390 final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1391 when(schemaNode.getPath()).thenReturn(path);
1392 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1393 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1394 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1395 final String pckgName = "test.augment.choice.cases";
1396 final Type targetType = mock(Type.class);
1397 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1398 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1399 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1400 when(caseNode.getPath()).thenReturn(path);
1401 when(caseNode.getQName()).thenReturn(qnamePath);
1402 augmentNodes.add(caseNode);
1404 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1405 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1406 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1407 schemaPathAugmentListEntry.add(augmentationSchema);
1409 final DataNodeContainer usesNodeParent = null;
1410 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1411 when(targetNode.getPath()).thenReturn(path);
1412 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1413 final ModuleContext moduleContext = new ModuleContext();
1414 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment", moduleContext);
1415 moduleContext.addCaseType(path, gtb);
1416 genCtx.put(module, moduleContext);
1417 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1419 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1420 .thenReturn(module);
1422 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1423 genCtx, false, genTypeBuilder, null ,BindingNamespaceType.Data };
1424 final Map<Module, ModuleContext> result =
1425 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1426 assertNotNull(result);
1427 assertEquals(result.get(module), moduleContext);