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.Augmentable;
36 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
39 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
40 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
45 import org.opendaylight.yangtools.yang.model.api.Module;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.UsesNode;
51 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
53 public class AugmentToGenTypeTest {
55 @SuppressWarnings("unchecked")
56 @Test(expected = UnsupportedOperationException.class)
57 public void constructorTest() throws Throwable {
58 final Constructor<AugmentToGenType> constructor =
59 (Constructor<AugmentToGenType>) AugmentToGenType.class.getDeclaredConstructors()[0];
60 constructor.setAccessible(true);
61 final Object[] objs = {};
63 constructor.newInstance(objs);
64 } catch (final Exception e) {
69 @SuppressWarnings("rawtypes")
71 public void generateNullModuleTest() throws Exception {
72 final Class[] parameterTypes =
73 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
74 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
75 assertNotNull(generate);
76 generate.setAccessible(true);
78 final SchemaContext context = null;
79 final TypeProvider typeProvider = null;
80 final Map<Module, ModuleContext> genCtx = new HashMap<>();
81 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
82 final Module m = null;
84 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
86 generate.invoke(AugmentToGenType.class, args);
88 } catch (final Exception e) {
90 assertTrue(e instanceof InvocationTargetException);
91 final Throwable cause = e.getCause();
93 assertTrue(cause instanceof IllegalArgumentException);
94 assertEquals("Module reference cannot be NULL.", cause.getMessage());
98 @SuppressWarnings("rawtypes")
100 public void generateNullModuleNameTest() throws Exception {
101 final Class[] parameterTypes =
102 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
103 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
104 assertNotNull(generate);
105 generate.setAccessible(true);
107 final SchemaContext context = null;
108 final TypeProvider typeProvider = null;
109 final Map<Module, ModuleContext> genCtx = new HashMap<>();
110 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
111 final Module m = mock(Module.class);
112 when(m.getName()).thenReturn(null);
114 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
116 generate.invoke(AugmentToGenType.class, args);
118 } catch (final Exception e) {
120 assertTrue(e instanceof InvocationTargetException);
121 final Throwable cause = e.getCause();
122 assertNotNull(cause);
123 assertTrue(cause instanceof IllegalArgumentException);
124 assertEquals("Module name cannot be NULL.", cause.getMessage());
128 @SuppressWarnings("rawtypes")
130 public void generateNullModuleAugmentationsTest() throws Exception {
131 final Class[] parameterTypes =
132 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
133 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
134 assertNotNull(generate);
135 generate.setAccessible(true);
137 final SchemaContext context = null;
138 final TypeProvider typeProvider = null;
139 final Map<Module, ModuleContext> genCtx = new HashMap<>();
140 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
141 final Module m = mock(Module.class);
142 when(m.getName()).thenReturn("name");
143 when(m.getAugmentations()).thenReturn(null);
145 final Object[] args = { m, context, typeProvider, genCtx, genTypeBuilders, false };
147 generate.invoke(AugmentToGenType.class, args);
149 } catch (final Exception e) {
151 assertTrue(e instanceof InvocationTargetException);
152 final Throwable cause = e.getCause();
153 assertNotNull(cause);
154 assertTrue(cause instanceof IllegalStateException);
155 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
159 @SuppressWarnings("rawtypes")
161 public void generateWithoutAugmentationsTest() throws Exception {
162 final Class[] parameterTypes =
163 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
164 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
165 assertNotNull(generate);
166 generate.setAccessible(true);
168 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test.yang");
169 final TypeProvider typeProvider = new TypeProviderImpl(context);
170 final Map<Module, ModuleContext> genCtx = new HashMap<>();
171 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
173 final Object[] args =
174 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
175 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
176 assertNotNull(invoke);
179 @SuppressWarnings({ "rawtypes", "unchecked" })
181 public void generateWithAugmentationsTest() throws Exception {
182 final Class[] parameterTypes =
183 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
184 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
185 assertNotNull(generate);
186 generate.setAccessible(true);
188 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test-augment.yang");
189 final TypeProvider typeProvider = new TypeProviderImpl(context);
190 final Map<Module, ModuleContext> genCtx = mock(Map.class);
191 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
192 final ModuleContext moduleContext = new ModuleContext();
193 moduleContexts.add(moduleContext);
194 final QName create = QName.create("urn:test:simple:test", "2017-02-06", "my-cont");
195 final SchemaNode schemaNode = mock(SchemaNode.class);
196 when(schemaNode.getPath()).thenReturn(SchemaPath.create(true, create));
197 moduleContext.addChildNodeType(schemaNode, new GeneratedTypeBuilderImpl("test", "Test"));
198 when(genCtx.values()).thenReturn(moduleContexts);
199 when(genCtx.get(context.getModules().iterator().next())).thenReturn(moduleContext);
200 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
202 final Object[] args =
203 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
204 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
205 assertNotNull(invoke);
208 @SuppressWarnings("rawtypes")
210 public void resolveAugmentationsNullModuleTest() throws Exception {
211 final Class[] parameterTypes = { Module.class };
212 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
213 assertNotNull(generate);
214 generate.setAccessible(true);
216 final Module m = null;
218 final Object[] args = { m };
220 generate.invoke(AugmentToGenType.class, args);
222 } catch (final Exception e) {
224 assertTrue(e instanceof InvocationTargetException);
225 final Throwable cause = e.getCause();
226 assertNotNull(cause);
227 assertTrue(cause instanceof IllegalArgumentException);
228 assertEquals("Module reference cannot be NULL.", cause.getMessage());
232 @SuppressWarnings("rawtypes")
234 public void resolveAugmentationsNullAugmentationsTest() throws Exception {
235 final Class[] parameterTypes = { Module.class };
236 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
237 assertNotNull(generate);
238 generate.setAccessible(true);
240 final Module m = mock(Module.class);
241 when(m.getAugmentations()).thenReturn(null);
243 final Object[] args = { m };
245 generate.invoke(AugmentToGenType.class, args);
247 } catch (final Exception e) {
249 assertTrue(e instanceof InvocationTargetException);
250 final Throwable cause = e.getCause();
251 assertNotNull(cause);
252 assertTrue(cause instanceof IllegalStateException);
253 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
257 @SuppressWarnings({ "rawtypes", "unchecked" })
259 public void resolveAugmentationsTest() throws Exception {
260 final Class[] parameterTypes = { Module.class };
261 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
262 assertNotNull(generate);
263 generate.setAccessible(true);
265 final Module m = mock(Module.class);
267 final Set<AugmentationSchema> augmentations = new HashSet<>();
269 final QName q1 = QName.create("q1", "2017-04-04", "q1");
270 final QName q2 = QName.create("q2", "2017-04-04", "q2");
271 final QName q3 = QName.create("q3", "2017-04-04", "q3");
272 final QName q4 = QName.create("q4", "2017-04-04", "q4");
273 final QName q5 = QName.create("q5", "2017-04-04", "q5");
275 final AugmentationSchema augmentationSchema1 = mock(AugmentationSchema.class);
276 when(augmentationSchema1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
277 final AugmentationSchema augmentationSchema2 = mock(AugmentationSchema.class);
278 when(augmentationSchema2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
279 augmentations.add(augmentationSchema1);
280 augmentations.add(augmentationSchema2);
282 when(m.getAugmentations()).thenReturn(augmentations);
284 final Object[] args = { m };
286 final List<AugmentationSchema> result =
287 (List<AugmentationSchema>) generate.invoke(AugmentToGenType.class, args);
288 assertNotNull(result);
289 assertTrue(!result.isEmpty());
290 assertEquals(result.get(0), augmentationSchema1);
291 assertEquals(result.get(1), augmentationSchema2);
294 @SuppressWarnings({ "rawtypes" })
296 public void augmentationToGenTypesNullPckgNameTest() throws Exception {
297 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
298 boolean.class, Map.class, Map.class, TypeProvider.class };
299 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
300 assertNotNull(generate);
301 generate.setAccessible(true);
303 final String augmPackName = null;
304 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
305 final SchemaContext context = null;
306 final TypeProvider typeProvider = null;
307 final Map<Module, ModuleContext> genCtx = new HashMap<>();
308 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
309 final Module m = null;
311 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
313 generate.invoke(AugmentToGenType.class, args);
315 } catch (final Exception e) {
317 assertTrue(e instanceof InvocationTargetException);
318 final Throwable cause = e.getCause();
319 assertNotNull(cause);
320 assertTrue(cause instanceof IllegalArgumentException);
321 assertEquals("Package Name cannot be NULL.", cause.getMessage());
325 @SuppressWarnings({ "rawtypes" })
327 public void augmentationToGenTypesNullAugSchemaTest() throws Exception {
328 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
329 boolean.class, Map.class, Map.class, TypeProvider.class };
330 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
331 assertNotNull(generate);
332 generate.setAccessible(true);
334 final String augmPackName = "pckg.name";
335 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
336 final SchemaContext context = null;
337 final TypeProvider typeProvider = null;
338 final Map<Module, ModuleContext> genCtx = new HashMap<>();
339 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
340 final Module m = null;
342 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
344 generate.invoke(AugmentToGenType.class, args);
346 } catch (final Exception e) {
348 assertTrue(e instanceof InvocationTargetException);
349 final Throwable cause = e.getCause();
350 assertNotNull(cause);
351 assertTrue(cause instanceof IllegalArgumentException);
352 assertEquals("Augmentation List Entry cannot be NULL.", cause.getMessage());
356 @SuppressWarnings({ "rawtypes" })
358 public void augmentationToGenTypesNullAugSchemaTargetPathTest() throws Exception {
359 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
360 boolean.class, Map.class, Map.class, TypeProvider.class };
361 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
362 assertNotNull(generate);
363 generate.setAccessible(true);
365 final String augmPackName = "pckg.name";
366 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
367 when(augmSchema.getTargetPath()).thenReturn(null);
369 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
370 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
371 when(schemaPathAugmentListEntry.getKey()).thenReturn(null);
372 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
374 final SchemaContext context = null;
375 final TypeProvider typeProvider = null;
376 final Map<Module, ModuleContext> genCtx = new HashMap<>();
377 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
378 final Module m = null;
380 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
382 generate.invoke(AugmentToGenType.class, args);
384 } catch (final Exception e) {
386 assertTrue(e instanceof InvocationTargetException);
387 final Throwable cause = e.getCause();
388 assertNotNull(cause);
389 assertTrue(cause instanceof IllegalStateException);
390 assertEquals("Augmentation List Entry does not contain Target Path (Target Path is NULL).", cause.getMessage());
394 @SuppressWarnings({ "rawtypes" })
396 public void augmentationToGenTypesNullAugSchemaListTest() throws Exception {
397 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
398 boolean.class, Map.class, Map.class, TypeProvider.class };
399 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
400 assertNotNull(generate);
401 generate.setAccessible(true);
403 final String augmPackName = "pckg.name";
404 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
405 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
406 final SchemaPath path = SchemaPath.create(true, qnamePath);
407 when(augmSchema.getTargetPath()).thenReturn(path);
409 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
410 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
411 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
412 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
414 final SchemaContext context = null;
415 final TypeProvider typeProvider = null;
416 final Map<Module, ModuleContext> genCtx = new HashMap<>();
417 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
418 final Module m = null;
420 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
422 generate.invoke(AugmentToGenType.class, args);
424 } catch (final Exception e) {
426 assertTrue(e instanceof InvocationTargetException);
427 final Throwable cause = e.getCause();
428 assertNotNull(cause);
429 assertTrue(cause instanceof IllegalStateException);
430 assertEquals("Augmentation List cannot be empty.", cause.getMessage());
434 @SuppressWarnings({ "rawtypes" })
436 public void augmentationToGenTypesNullAugSchemaTargetNodeTest() throws Exception {
437 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
438 boolean.class, Map.class, Map.class, TypeProvider.class };
439 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
440 assertNotNull(generate);
441 generate.setAccessible(true);
443 final String augmPackName = "pckg.name";
445 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
446 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
447 final SchemaPath path = SchemaPath.create(true, qnamePath);
448 when(augmSchema.getTargetPath()).thenReturn(path);
449 final Set<UsesNode> uses = new HashSet<>();
450 when(augmSchema.getUses()).thenReturn(uses);
452 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
453 augmentationSchemaList.add(augmSchema);
454 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
455 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
456 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
458 final SchemaContext context = mock(SchemaContext.class);
459 final Module moduleAug = mock(Module.class);
460 final DataSchemaNode schNode = null;
461 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
462 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
463 .thenReturn(moduleAug);
465 final TypeProvider typeProvider = null;
466 final Map<Module, ModuleContext> genCtx = new HashMap<>();
467 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
469 final Module m = mock(Module.class);
470 when(m.getName()).thenReturn("augm-module");
471 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
472 when(m.getRevision()).thenReturn(qnamePath.getRevision());
474 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
476 generate.invoke(AugmentToGenType.class, args);
478 } catch (final Exception e) {
480 assertTrue(e instanceof InvocationTargetException);
481 final Throwable cause = e.getCause();
482 assertNotNull(cause);
483 assertTrue(cause instanceof IllegalArgumentException);
484 assertEquals("augment target not found: " + path, cause.getMessage());
488 @SuppressWarnings({ "rawtypes" })
490 public void augmentationToGenTypesNullAugTargetGTBTest() throws Exception {
491 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
492 boolean.class, Map.class, Map.class, TypeProvider.class };
493 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
494 assertNotNull(generate);
495 generate.setAccessible(true);
497 final String augmPackName = "pckg.name";
499 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
500 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
501 final SchemaPath path = SchemaPath.create(true, qnamePath);
502 when(augmSchema.getTargetPath()).thenReturn(path);
503 final Set<UsesNode> uses = new HashSet<>();
504 when(augmSchema.getUses()).thenReturn(uses);
506 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
507 augmentationSchemaList.add(augmSchema);
508 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
509 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
510 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
512 final SchemaContext context = mock(SchemaContext.class);
513 final Module moduleAug = mock(Module.class);
514 final DataSchemaNode schNode = mock(DataSchemaNode.class);
515 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
516 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
517 .thenReturn(moduleAug);
519 final TypeProvider typeProvider = null;
520 final Map<Module, ModuleContext> genCtx = new HashMap<>();
521 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
523 final Module m = mock(Module.class);
524 when(m.getName()).thenReturn("augm-module");
525 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
526 when(m.getRevision()).thenReturn(qnamePath.getRevision());
528 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
530 generate.invoke(AugmentToGenType.class, args);
532 } catch (final Exception e) {
534 assertTrue(e instanceof InvocationTargetException);
535 final Throwable cause = e.getCause();
536 assertNotNull(cause);
537 assertTrue(cause instanceof NullPointerException);
538 assertEquals("Target type not yet generated: " + schNode, cause.getMessage());
542 @SuppressWarnings({ "rawtypes", "unchecked" })
544 public void augmentationToGenTypesAugUsesNullOrigTargetTest() throws Exception {
545 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
546 boolean.class, Map.class, Map.class, TypeProvider.class };
547 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
548 assertNotNull(generate);
549 generate.setAccessible(true);
551 final String augmPackName = "pckg.name";
553 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
554 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
555 final SchemaPath path = SchemaPath.create(true, qnamePath);
556 when(augmSchema.getTargetPath()).thenReturn(path);
557 final Set<UsesNode> uses = new HashSet<>();
558 when(augmSchema.getUses()).thenReturn(uses);
560 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
561 augmentationSchemaList.add(augmSchema);
562 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
563 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
564 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
566 final SchemaContext context = mock(SchemaContext.class);
567 final Module moduleAug = mock(Module.class);
568 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
569 when(targetSchNode.getPath()).thenReturn(path);
570 when(targetSchNode.isAddedByUses()).thenReturn(true);
571 final Optional optionalSchemaNode = Optional.absent();
572 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
573 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
574 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
575 .thenReturn(moduleAug);
577 final TypeProvider typeProvider = null;
578 final Map<Module, ModuleContext> genCtx = new HashMap<>();
579 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
581 final Module m = mock(Module.class);
582 when(m.getName()).thenReturn("augm-module");
583 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
584 when(m.getRevision()).thenReturn(qnamePath.getRevision());
586 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
588 generate.invoke(AugmentToGenType.class, args);
590 } catch (final Exception e) {
592 assertTrue(e instanceof InvocationTargetException);
593 final Throwable cause = e.getCause();
594 assertNotNull(cause);
595 assertTrue(cause instanceof IllegalStateException);
596 assertEquals("Failed to find target node from grouping in augmentation " + augmSchema + " in module "
597 + m.getName(), cause.getMessage());
601 @SuppressWarnings({ "rawtypes" })
603 public void augmentationToGenTypesTargetChoicSchemaNodeTest() throws Exception {
604 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
605 boolean.class, Map.class, Map.class, TypeProvider.class };
606 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
607 assertNotNull(generate);
608 generate.setAccessible(true);
610 final String augmPackName = "pckg.name";
612 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
613 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
614 final SchemaPath path = SchemaPath.create(true, qnamePath);
615 when(augmSchema.getTargetPath()).thenReturn(path);
616 final Set<UsesNode> uses = new HashSet<>();
617 when(augmSchema.getUses()).thenReturn(uses);
619 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
620 augmentationSchemaList.add(augmSchema);
621 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
622 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
623 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
625 final SchemaContext context = mock(SchemaContext.class);
626 final Module moduleAug = mock(Module.class);
627 final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
628 when(targetSchNode.getPath()).thenReturn(path);
629 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
630 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
631 .thenReturn(moduleAug);
633 final TypeProvider typeProvider = null;
635 final Map genCtx = mock(Map.class);
636 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
637 final ModuleContext mc = new ModuleContext();
638 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
639 mc.addChildNodeType(targetSchNode, gtb);
640 moduleContexts.add(mc);
641 when(genCtx.values()).thenReturn(moduleContexts);
643 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
645 final Module m = mock(Module.class);
646 when(m.getName()).thenReturn("augm-module");
647 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
648 when(m.getRevision()).thenReturn(qnamePath.getRevision());
650 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
651 final Map result = (Map) generate.invoke(AugmentToGenType.class, args);
652 assertNotNull(result);
655 @SuppressWarnings({ "rawtypes", "unchecked" })
657 public void augmentationToGenTypesTest() throws Exception {
658 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
659 boolean.class, Map.class, Map.class, TypeProvider.class };
660 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
661 assertNotNull(generate);
662 generate.setAccessible(true);
664 final String augmPackName = "pckg.name";
666 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
667 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
668 final SchemaPath path = SchemaPath.create(true, qnamePath);
669 when(augmSchema.getTargetPath()).thenReturn(path);
670 final Set<UsesNode> uses = new HashSet<>();
671 when(augmSchema.getUses()).thenReturn(uses);
672 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
673 when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
675 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
676 augmentationSchemaList.add(augmSchema);
677 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
678 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
679 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
681 final SchemaContext context = mock(SchemaContext.class);
682 final Module moduleAug = mock(Module.class);
683 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
684 when(targetSchNode.getPath()).thenReturn(path);
685 when(targetSchNode.isAddedByUses()).thenReturn(true);
686 final DataSchemaNode origSchNode = mock(DataSchemaNode.class);
687 when(origSchNode.getPath()).thenReturn(path);
688 when(origSchNode.isAddedByUses()).thenReturn(true);
689 final Optional optionalSchemaNode = Optional.of(origSchNode);
690 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
691 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
692 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
693 .thenReturn(moduleAug);
695 final TypeProvider typeProvider = null;
697 final Map<Module, ModuleContext> genCtx = new HashMap<>();
699 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
700 final ModuleContext mc = new ModuleContext();
701 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
702 mc.addChildNodeType(targetSchNode, gtb);
703 moduleContexts.add(mc);
704 genCtx.put(moduleAug, mc);
706 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
708 when(moduleAug.getName()).thenReturn("augm-module");
709 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
710 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
712 final Object[] args =
713 { augmPackName, schemaPathAugmentListEntry, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
714 final Map<Module, ModuleContext> result =
715 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
716 assertNotNull(result);
717 final ModuleContext moduleContext = result.get(moduleAug);
718 assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
719 assertEquals("pckg.name.data.aug", moduleContext.getAugmentations().get(0).getPackageName());
720 assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
721 assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
725 public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
727 AugmentToGenType.usesAugmentationToGenTypes(null, null, null, null, null, null, null, null, false, null);
728 } catch (final Exception e) {
730 assertTrue(e instanceof IllegalArgumentException);
731 assertEquals(e.getMessage(), "Package Name cannot be NULL.");
736 public void usesAugmentationToGenTypesNullAugSchemaListEntryTest() throws Exception {
738 AugmentToGenType.usesAugmentationToGenTypes(null, "", null, null, null, null, null, null, false, null);
739 } catch (final Exception e) {
741 assertTrue(e instanceof IllegalArgumentException);
742 assertEquals(e.getMessage(), "Augmentation Schema List Entry cannot be NULL.");
747 public void usesAugmentationToGenTypesEmptyAugSchemaListTest() throws Exception {
748 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
750 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
752 } catch (final Exception e) {
754 assertTrue(e instanceof IllegalStateException);
755 assertEquals(e.getMessage(), "Augmentation Schema List cannot be empty");
760 public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
761 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
762 when(augmentationSchema.getTargetPath()).thenReturn(null);
763 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
764 augmentationSchemaList.add(augmentationSchema);
766 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
768 } catch (final Exception e) {
770 assertTrue(e instanceof IllegalStateException);
771 assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
776 public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
777 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
778 final SchemaPath path = SchemaPath.create(true, qnamePath);
779 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
780 when(augmentationSchema.getTargetPath()).thenReturn(path);
781 final Set<UsesNode> uses = new HashSet<>();
782 when(augmentationSchema.getUses()).thenReturn(uses);
784 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
785 augmentationSchemaList.add(augmentationSchema);
787 final SchemaContext context = mock(SchemaContext.class);
788 final Module moduleAug = mock(Module.class);
789 when(moduleAug.getName()).thenReturn("augm-module");
790 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
791 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
792 final Set<GroupingDefinition> groupings = new HashSet<>();
793 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
794 when(groupingDefinition.getQName()).thenReturn(qnamePath);
795 groupings.add(groupingDefinition);
796 when(moduleAug.getGroupings()).thenReturn(groupings);
798 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
799 .thenReturn(moduleAug);
801 final Map<Module, ModuleContext> genCtx = new HashMap<>();
802 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
805 final UsesNode usesNode = mock(UsesNode.class);
806 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
808 when(usesNode.getGroupingPath()).thenReturn(path);
811 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
812 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
813 } catch (final Exception e) {
815 assertTrue(e instanceof IllegalArgumentException);
816 assertEquals(e.getMessage(), "augment target not found: " + path);
821 public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
822 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
823 final SchemaPath path = SchemaPath.create(true, qnamePath);
824 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
825 when(augmentationSchema.getTargetPath()).thenReturn(path);
826 final Set<UsesNode> uses = new HashSet<>();
827 when(augmentationSchema.getUses()).thenReturn(uses);
829 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
830 augmentationSchemaList.add(augmentationSchema);
832 final SchemaContext context = mock(SchemaContext.class);
833 final Module moduleAug = mock(Module.class);
834 when(moduleAug.getName()).thenReturn("augm-module");
835 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
836 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
837 final Set<GroupingDefinition> groupings = new HashSet<>();
838 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
839 when(groupingDefinition.getQName()).thenReturn(qnamePath);
840 final DataSchemaNode schNode = mock(DataSchemaNode.class);
841 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
842 groupings.add(groupingDefinition);
843 when(moduleAug.getGroupings()).thenReturn(groupings);
845 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
846 .thenReturn(moduleAug);
848 final Map<Module, ModuleContext> genCtx = new HashMap<>();
849 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
851 final UsesNode usesNode = mock(UsesNode.class);
852 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
854 when(usesNode.getGroupingPath()).thenReturn(path);
857 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
858 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
859 } catch (final Exception e) {
861 assertTrue(e instanceof NullPointerException);
862 assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
867 public void usesAugmentationToGenTypesTest() throws Exception {
868 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
869 final SchemaPath path = SchemaPath.create(true, qnamePath);
870 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
871 when(augmentationSchema.getTargetPath()).thenReturn(path);
872 final Set<UsesNode> uses = new HashSet<>();
873 when(augmentationSchema.getUses()).thenReturn(uses);
875 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
876 augmentationSchemaList.add(augmentationSchema);
878 final SchemaContext context = mock(SchemaContext.class);
879 final Module moduleAug = mock(Module.class);
880 when(moduleAug.getName()).thenReturn("augm-module");
881 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
882 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
883 final Set<GroupingDefinition> groupings = new HashSet<>();
884 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
885 when(groupingDefinition.getQName()).thenReturn(qnamePath);
886 final DataSchemaNode schNode = mock(DataSchemaNode.class);
887 when(schNode.getPath()).thenReturn(path);
888 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
889 groupings.add(groupingDefinition);
890 when(moduleAug.getGroupings()).thenReturn(groupings);
892 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
893 .thenReturn(moduleAug);
895 final Map<Module, ModuleContext> genCtx = new HashMap<>();
896 final ModuleContext mc = new ModuleContext();
897 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
898 mc.addChildNodeType(schNode, gtb);
899 genCtx.put(moduleAug, mc);
901 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
903 final UsesNode usesNode = mock(UsesNode.class);
904 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
906 when(usesNode.getGroupingPath()).thenReturn(path);
908 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
909 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
910 assertNotNull(result);
914 public void usesAugmentationToGenTypesChoiceTest() throws Exception {
915 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
916 final SchemaPath path = SchemaPath.create(true, qnamePath);
917 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
918 when(augmentationSchema.getTargetPath()).thenReturn(path);
919 final Set<UsesNode> uses = new HashSet<>();
920 when(augmentationSchema.getUses()).thenReturn(uses);
922 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
923 augmentationSchemaList.add(augmentationSchema);
925 final SchemaContext context = mock(SchemaContext.class);
926 final Module moduleAug = mock(Module.class);
927 when(moduleAug.getName()).thenReturn("augm-module");
928 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
929 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
930 final Set<GroupingDefinition> groupings = new HashSet<>();
931 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
932 when(groupingDefinition.getQName()).thenReturn(qnamePath);
933 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
934 when(schNode.getPath()).thenReturn(path);
935 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
936 groupings.add(groupingDefinition);
937 when(moduleAug.getGroupings()).thenReturn(groupings);
939 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
940 .thenReturn(moduleAug);
942 final Map<Module, ModuleContext> genCtx = new HashMap<>();
943 final ModuleContext mc = new ModuleContext();
944 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
945 mc.addChildNodeType(schNode, gtb);
946 genCtx.put(moduleAug, mc);
948 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
950 final UsesNode usesNode = mock(UsesNode.class);
951 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
953 when(usesNode.getGroupingPath()).thenReturn(path);
955 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
956 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null);
957 assertNotNull(result);
960 @SuppressWarnings({ "rawtypes" })
962 public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
963 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
964 final Method generate =
965 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
966 assertNotNull(generate);
967 generate.setAccessible(true);
969 final Module module = mock(Module.class);
970 final QName qnamePath = QName.create("test", "2017-04-04", "test");
971 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
972 final DataSchemaNode schNode = mock(DataSchemaNode.class);
973 when(schNode.getPath()).thenReturn(schemaPath);
974 when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
976 final SchemaContext context = mock(SchemaContext.class);
977 when(context.findModuleByNamespaceAndRevision(qnamePath .getNamespace(), qnamePath.getRevision()))
979 final UsesNode usesNode = mock(UsesNode.class);
980 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
982 final Object[] args = { context, schemaPath, usesNode };
984 generate.invoke(AugmentToGenType.class, args);
986 } catch (final Exception e) {
988 assertTrue(e instanceof InvocationTargetException);
989 final Throwable cause = e.getCause();
990 assertNotNull(cause);
991 assertTrue(cause instanceof IllegalArgumentException);
992 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
996 @SuppressWarnings({ "rawtypes" })
998 public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
999 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1000 final Method generate =
1001 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1002 assertNotNull(generate);
1003 generate.setAccessible(true);
1005 final Module module = mock(Module.class);
1006 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1007 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1008 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1009 when(schNode.getPath()).thenReturn(schemaPath);
1010 when(schNode.isAddedByUses()).thenReturn(true);
1011 final Set<GroupingDefinition> groupings = new HashSet<>();
1012 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1013 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1014 groupings.add(groupingDefinition);
1015 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1016 when(module.getGroupings()).thenReturn(groupings);
1018 final SchemaContext context = mock(SchemaContext.class);
1019 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1020 .thenReturn(module);
1021 final UsesNode usesNode = mock(UsesNode.class);
1022 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1024 final Object[] args = { context, schemaPath, usesNode };
1026 generate.invoke(AugmentToGenType.class, args);
1028 } catch (final Exception e) {
1030 assertTrue(e instanceof InvocationTargetException);
1031 final Throwable cause = e.getCause();
1032 assertNotNull(cause);
1033 assertTrue(cause instanceof IllegalStateException);
1034 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1038 @SuppressWarnings({ "rawtypes" })
1040 public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
1041 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1042 final Method generate =
1043 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1044 assertNotNull(generate);
1045 generate.setAccessible(true);
1047 final Module module = mock(Module.class);
1048 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1049 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1050 final DataSchemaNode schNode = null;
1051 final Set<GroupingDefinition> groupings = new HashSet<>();
1052 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1053 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1054 groupings.add(groupingDefinition);
1055 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1056 when(module.getGroupings()).thenReturn(groupings);
1058 final SchemaContext context = mock(SchemaContext.class);
1059 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1060 .thenReturn(module);
1061 final UsesNode usesNode = mock(UsesNode.class);
1062 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1064 final Object[] args = { context, schemaPath, usesNode };
1065 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1066 assertEquals(null, result);
1069 @SuppressWarnings({ "rawtypes", "unchecked" })
1071 public void findOriginalTargetFromGroupingTest() throws Exception {
1072 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1073 final Method generate =
1074 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1075 assertNotNull(generate);
1076 generate.setAccessible(true);
1078 final Module module = mock(Module.class);
1079 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1080 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1081 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1082 when(schNode.getPath()).thenReturn(schemaPath);
1083 final Set<GroupingDefinition> groupings = new HashSet<>();
1084 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1085 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1086 groupings.add(groupingDefinition);
1087 final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
1088 when(derivSchNode.isAddedByUses()).thenReturn(true);
1089 final Optional optional = Optional.of(schNode);
1090 when(derivSchNode.getOriginal()).thenReturn(optional);
1091 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
1092 when(module.getGroupings()).thenReturn(groupings);
1094 final SchemaContext context = mock(SchemaContext.class);
1095 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1096 .thenReturn(module);
1097 final UsesNode usesNode = mock(UsesNode.class);
1098 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1100 final Object[] args = { context, schemaPath, usesNode };
1101 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1102 assertEquals(schNode, result);
1105 @SuppressWarnings({ "rawtypes" })
1107 public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1108 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1109 final Method generate =
1110 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1111 assertNotNull(generate);
1112 generate.setAccessible(true);
1114 final Module module = mock(Module.class);
1115 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1116 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1117 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1118 when(schNode.getPath()).thenReturn(schemaPath);
1119 final Set<GroupingDefinition> groupings = new HashSet<>();
1120 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1121 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1122 groupings.add(groupingDefinition);
1123 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1124 when(module.getGroupings()).thenReturn(groupings);
1126 final SchemaContext context = mock(SchemaContext.class);
1127 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1128 .thenReturn(module);
1129 final UsesNode usesNode = mock(UsesNode.class);
1130 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1132 final Object[] args = { context, schemaPath, usesNode };
1133 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1134 assertEquals(schNode, result);
1137 @SuppressWarnings({ "rawtypes" })
1139 public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1140 final Class[] parameterTypes =
1141 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1142 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1143 final Method generate =
1144 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1145 assertNotNull(generate);
1146 generate.setAccessible(true);
1148 final SchemaContext schemaContext = null;
1149 final Module module = null;
1150 final String pckgName = null;
1151 final Type targetType = null;
1152 final ChoiceSchemaNode targetNode = null;
1153 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1154 final DataNodeContainer usesNodeParent = null;
1155 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1156 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1158 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1159 usesNodeParent, genCtx, false, genTypeBuilder, null };
1161 generate.invoke(AugmentToGenType.class, args);
1163 } catch (final Exception e) {
1165 assertTrue(e instanceof InvocationTargetException);
1166 final Throwable cause = e.getCause();
1167 assertNotNull(cause);
1168 assertTrue(cause instanceof IllegalArgumentException);
1169 assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1173 @SuppressWarnings({ "rawtypes" })
1175 public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1176 final Class[] parameterTypes =
1177 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1178 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1179 final Method generate =
1180 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1181 assertNotNull(generate);
1182 generate.setAccessible(true);
1184 final SchemaContext schemaContext = null;
1185 final Module module = null;
1186 final String pckgName = "";
1187 final Type targetType = null;
1188 final ChoiceSchemaNode targetNode = null;
1189 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1190 final DataNodeContainer usesNodeParent = null;
1191 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1192 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1194 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1195 usesNodeParent, genCtx, false, genTypeBuilder, null };
1197 generate.invoke(AugmentToGenType.class, args);
1199 } catch (final Exception e) {
1201 assertTrue(e instanceof InvocationTargetException);
1202 final Throwable cause = e.getCause();
1203 assertNotNull(cause);
1204 assertTrue(cause instanceof IllegalArgumentException);
1205 assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1209 @SuppressWarnings({ "rawtypes" })
1211 public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1212 final Class[] parameterTypes =
1213 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1214 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1215 final Method generate =
1216 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1217 assertNotNull(generate);
1218 generate.setAccessible(true);
1220 final SchemaContext schemaContext = null;
1221 final Module module = null;
1222 final String pckgName = "";
1223 final Type targetType = mock(Type.class);
1224 final ChoiceSchemaNode targetNode = null;
1225 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1226 final DataNodeContainer usesNodeParent = null;
1227 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1228 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1230 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1231 genCtx, false, genTypeBuilder, null };
1233 generate.invoke(AugmentToGenType.class, args);
1235 } catch (final Exception e) {
1237 assertTrue(e instanceof InvocationTargetException);
1238 final Throwable cause = e.getCause();
1239 assertNotNull(cause);
1240 assertTrue(cause instanceof IllegalArgumentException);
1241 assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1245 @SuppressWarnings({ "rawtypes", "unchecked" })
1247 public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1248 final Class[] parameterTypes =
1249 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1250 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1251 final Method generate =
1252 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1253 assertNotNull(generate);
1254 generate.setAccessible(true);
1256 final SchemaContext schemaContext = null;
1257 final Module module = null;
1258 final String pckgName = "";
1259 final Type targetType = mock(Type.class);
1260 final ChoiceSchemaNode targetNode = null;
1261 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1262 final DataSchemaNode caseNode = null;
1263 augmentNodes.add(caseNode);
1265 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1266 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1267 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1268 schemaPathAugmentListEntry.add(augmentationSchema);
1270 final DataNodeContainer usesNodeParent = null;
1271 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1272 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1274 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1275 genCtx, false, genTypeBuilder, null };
1276 final Map<Module, ModuleContext> result =
1277 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1278 assertEquals(genCtx, result);
1281 @SuppressWarnings({ "rawtypes" })
1283 public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1284 final Class[] parameterTypes =
1285 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1286 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1287 final Method generate =
1288 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1289 assertNotNull(generate);
1290 generate.setAccessible(true);
1292 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1293 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1294 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1296 final SchemaContext schemaContext = mock(SchemaContext.class);
1297 final Module module = mock(Module.class);
1298 when(module.getName()).thenReturn("test-module-case");
1299 final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1300 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1301 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1302 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1303 final String pckgName = "test.augment.choice.cases";
1304 final Type targetType = mock(Type.class);
1305 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1306 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1307 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1308 when(caseNode.getPath()).thenReturn(path);
1309 when(caseNode.getQName()).thenReturn(qnamePath);
1310 augmentNodes.add(caseNode);
1312 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1313 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1314 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1315 schemaPathAugmentListEntry.add(augmentationSchema);
1317 final DataNodeContainer usesNodeParent = null;
1318 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1319 when(targetNode.getPath()).thenReturn(path);
1320 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1321 final ModuleContext moduleContext = new ModuleContext();
1322 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1323 moduleContext.addCaseType(path, gtb);
1324 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1326 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1327 .thenReturn(module);
1329 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1330 genCtx, false, genTypeBuilder, null };
1332 generate.invoke(AugmentToGenType.class, args);
1334 } catch (final Exception e) {
1336 assertTrue(e instanceof InvocationTargetException);
1337 final Throwable cause = e.getCause();
1338 assertNotNull(cause);
1339 assertTrue(cause instanceof IllegalArgumentException);
1340 assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1344 @SuppressWarnings({ "rawtypes", "unchecked" })
1346 public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1347 final Class[] parameterTypes =
1348 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1349 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class };
1350 final Method generate =
1351 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1352 assertNotNull(generate);
1353 generate.setAccessible(true);
1355 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1356 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1357 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1359 final SchemaContext schemaContext = mock(SchemaContext.class);
1360 final Module module = mock(Module.class);
1361 when(module.getName()).thenReturn("test-module-case");
1362 final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1363 when(schemaNode.getPath()).thenReturn(path);
1364 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1365 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1366 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1367 final String pckgName = "test.augment.choice.cases";
1368 final Type targetType = mock(Type.class);
1369 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1370 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1371 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1372 when(caseNode.getPath()).thenReturn(path);
1373 when(caseNode.getQName()).thenReturn(qnamePath);
1374 augmentNodes.add(caseNode);
1376 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1377 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1378 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1379 schemaPathAugmentListEntry.add(augmentationSchema);
1381 final DataNodeContainer usesNodeParent = null;
1382 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1383 when(targetNode.getPath()).thenReturn(path);
1384 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1385 final ModuleContext moduleContext = new ModuleContext();
1386 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1387 moduleContext.addCaseType(path, gtb);
1388 genCtx.put(module, moduleContext);
1389 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1391 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1392 .thenReturn(module);
1394 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1395 genCtx, false, genTypeBuilder, null };
1396 final Map<Module, ModuleContext> result =
1397 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1398 assertNotNull(result);
1399 assertEquals(result.get(module), moduleContext);