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.runtime.BindingNamespaceType;
36 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
37 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
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 = YangParserTestUtils.parseYangSource("/generator/test.yang");
170 final TypeProvider typeProvider = new TypeProviderImpl(context);
171 final Map<Module, ModuleContext> genCtx = new HashMap<>();
172 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
174 final Object[] args =
175 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
176 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
177 assertNotNull(invoke);
180 @SuppressWarnings({ "rawtypes", "unchecked" })
182 public void generateWithAugmentationsTest() throws Exception {
183 final Class[] parameterTypes =
184 { Module.class, SchemaContext.class, TypeProvider.class, Map.class, Map.class, boolean.class };
185 final Method generate = AugmentToGenType.class.getDeclaredMethod("generate", parameterTypes);
186 assertNotNull(generate);
187 generate.setAccessible(true);
189 final SchemaContext context = YangParserTestUtils.parseYangSource("/generator/test-augment.yang");
190 final TypeProvider typeProvider = new TypeProviderImpl(context);
191 final Map<Module, ModuleContext> genCtx = mock(Map.class);
192 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
193 final ModuleContext moduleContext = new ModuleContext();
194 moduleContexts.add(moduleContext);
195 final QName create = QName.create("urn:test:simple:test", "2017-02-06", "my-cont");
196 final SchemaNode schemaNode = mock(SchemaNode.class);
197 when(schemaNode.getPath()).thenReturn(SchemaPath.create(true, create));
198 moduleContext.addChildNodeType(schemaNode, new GeneratedTypeBuilderImpl("test", "Test"));
199 when(genCtx.values()).thenReturn(moduleContexts);
200 when(genCtx.get(context.getModules().iterator().next())).thenReturn(moduleContext);
201 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
203 final Object[] args =
204 { context.getModules().iterator().next(), context, typeProvider, genCtx, genTypeBuilders, false };
205 final Map invoke = (Map) generate.invoke(AugmentToGenType.class, args);
206 assertNotNull(invoke);
209 @SuppressWarnings("rawtypes")
211 public void resolveAugmentationsNullModuleTest() throws Exception {
212 final Class[] parameterTypes = { Module.class, SchemaContext.class };
213 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
214 assertNotNull(generate);
215 generate.setAccessible(true);
217 final Module m = null;
218 final SchemaContext schemaContext = null;
220 final Object[] args = { m, schemaContext };
222 generate.invoke(AugmentToGenType.class, args);
224 } catch (final Exception e) {
226 assertTrue(e instanceof InvocationTargetException);
227 final Throwable cause = e.getCause();
228 assertNotNull(cause);
229 assertTrue(cause instanceof IllegalArgumentException);
230 assertEquals("Module reference cannot be NULL.", cause.getMessage());
234 @SuppressWarnings("rawtypes")
236 public void resolveAugmentationsNullAugmentationsTest() throws Exception {
237 final Class[] parameterTypes = { Module.class, SchemaContext.class };
238 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
239 assertNotNull(generate);
240 generate.setAccessible(true);
242 final Module m = mock(Module.class);
243 when(m.getAugmentations()).thenReturn(null);
244 final SchemaContext schemaContext = mock(SchemaContext.class);
246 final Object[] args = { m, schemaContext };
248 generate.invoke(AugmentToGenType.class, args);
250 } catch (final Exception e) {
252 assertTrue(e instanceof InvocationTargetException);
253 final Throwable cause = e.getCause();
254 assertNotNull(cause);
255 assertTrue(cause instanceof IllegalStateException);
256 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
260 @SuppressWarnings({ "rawtypes", "unchecked" })
262 public void resolveAugmentationsTest() throws Exception {
263 final Class[] parameterTypes = { Module.class, SchemaContext.class };
264 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
265 assertNotNull(generate);
266 generate.setAccessible(true);
268 final Module m = mock(Module.class);
269 final Module m2 = mock(Module.class);
270 final SchemaContext schemaContext = mock(SchemaContext.class);
272 final Set<AugmentationSchema> augmentations = new HashSet<>();
274 final QName q1 = QName.create("q1", "2017-04-04", "q1");
275 final QName q2 = QName.create("q2", "2017-04-04", "q2");
276 final QName q3 = QName.create("q3", "2017-04-04", "q3");
277 final QName q4 = QName.create("q4", "2017-04-04", "q4");
278 final QName q5 = QName.create("q5", "2017-04-04", "q5");
280 final AugmentationSchema augmentationSchema1 = mock(AugmentationSchema.class);
281 when(augmentationSchema1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
282 final AugmentationSchema augmentationSchema2 = mock(AugmentationSchema.class);
283 when(augmentationSchema2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
284 augmentations.add(augmentationSchema1);
285 augmentations.add(augmentationSchema2);
287 when(m.getAugmentations()).thenReturn(augmentations);
288 when(schemaContext.findModuleByNamespaceAndRevision(q1.getNamespace(), q1.getRevision())).thenReturn(m2);
289 when(schemaContext.findModuleByNamespaceAndRevision(q3.getNamespace(), q3.getRevision())).thenReturn(m2);
291 final Object[] args = { m, schemaContext };
293 final List<AugmentationSchema> result =
294 (List<AugmentationSchema>) generate.invoke(AugmentToGenType.class, args);
295 assertNotNull(result);
296 assertTrue(!result.isEmpty());
297 assertEquals(result.get(0), augmentationSchema1);
298 assertEquals(result.get(1), augmentationSchema2);
301 @SuppressWarnings({ "rawtypes" })
303 public void augmentationToGenTypesNullPckgNameTest() throws Exception {
304 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
305 boolean.class, Map.class, Map.class, TypeProvider.class };
306 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
307 assertNotNull(generate);
308 generate.setAccessible(true);
310 final String augmPackName = null;
311 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
312 final SchemaContext context = null;
313 final TypeProvider typeProvider = null;
314 final Map<Module, ModuleContext> genCtx = new HashMap<>();
315 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
316 final Module m = null;
318 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
320 generate.invoke(AugmentToGenType.class, args);
322 } catch (final Exception e) {
324 assertTrue(e instanceof InvocationTargetException);
325 final Throwable cause = e.getCause();
326 assertNotNull(cause);
327 assertTrue(cause instanceof IllegalArgumentException);
328 assertEquals("Package Name cannot be NULL.", cause.getMessage());
332 @SuppressWarnings({ "rawtypes" })
334 public void augmentationToGenTypesNullAugSchemaTest() throws Exception {
335 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
336 boolean.class, Map.class, Map.class, TypeProvider.class };
337 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
338 assertNotNull(generate);
339 generate.setAccessible(true);
341 final String augmPackName = "pckg.name";
342 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
343 final SchemaContext context = null;
344 final TypeProvider typeProvider = null;
345 final Map<Module, ModuleContext> genCtx = new HashMap<>();
346 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
347 final Module m = null;
349 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
351 generate.invoke(AugmentToGenType.class, args);
353 } catch (final Exception e) {
355 assertTrue(e instanceof InvocationTargetException);
356 final Throwable cause = e.getCause();
357 assertNotNull(cause);
358 assertTrue(cause instanceof IllegalArgumentException);
359 assertEquals("Augmentation List Entry cannot be NULL.", cause.getMessage());
363 @SuppressWarnings({ "rawtypes" })
365 public void augmentationToGenTypesNullAugSchemaTargetPathTest() throws Exception {
366 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
367 boolean.class, Map.class, Map.class, TypeProvider.class };
368 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
369 assertNotNull(generate);
370 generate.setAccessible(true);
372 final String augmPackName = "pckg.name";
373 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
374 when(augmSchema.getTargetPath()).thenReturn(null);
376 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
377 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
378 when(schemaPathAugmentListEntry.getKey()).thenReturn(null);
379 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
381 final SchemaContext context = null;
382 final TypeProvider typeProvider = null;
383 final Map<Module, ModuleContext> genCtx = new HashMap<>();
384 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
385 final Module m = null;
387 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
389 generate.invoke(AugmentToGenType.class, args);
391 } catch (final Exception e) {
393 assertTrue(e instanceof InvocationTargetException);
394 final Throwable cause = e.getCause();
395 assertNotNull(cause);
396 assertTrue(cause instanceof IllegalStateException);
397 assertEquals("Augmentation List Entry does not contain Target Path (Target Path is NULL).", cause.getMessage());
401 @SuppressWarnings({ "rawtypes" })
403 public void augmentationToGenTypesNullAugSchemaListTest() throws Exception {
404 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
405 boolean.class, Map.class, Map.class, TypeProvider.class };
406 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
407 assertNotNull(generate);
408 generate.setAccessible(true);
410 final String augmPackName = "pckg.name";
411 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
412 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
413 final SchemaPath path = SchemaPath.create(true, qnamePath);
414 when(augmSchema.getTargetPath()).thenReturn(path);
416 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
417 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
418 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
419 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
421 final SchemaContext context = null;
422 final TypeProvider typeProvider = null;
423 final Map<Module, ModuleContext> genCtx = new HashMap<>();
424 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
425 final Module m = null;
427 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
429 generate.invoke(AugmentToGenType.class, args);
431 } catch (final Exception e) {
433 assertTrue(e instanceof InvocationTargetException);
434 final Throwable cause = e.getCause();
435 assertNotNull(cause);
436 assertTrue(cause instanceof IllegalStateException);
437 assertEquals("Augmentation List cannot be empty.", cause.getMessage());
441 @SuppressWarnings({ "rawtypes" })
443 public void augmentationToGenTypesNullAugSchemaTargetNodeTest() throws Exception {
444 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
445 boolean.class, Map.class, Map.class, TypeProvider.class };
446 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
447 assertNotNull(generate);
448 generate.setAccessible(true);
450 final String augmPackName = "pckg.name";
452 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
453 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
454 final SchemaPath path = SchemaPath.create(true, qnamePath);
455 when(augmSchema.getTargetPath()).thenReturn(path);
456 final Set<UsesNode> uses = new HashSet<>();
457 when(augmSchema.getUses()).thenReturn(uses);
459 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
460 augmentationSchemaList.add(augmSchema);
461 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
462 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
463 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
465 final SchemaContext context = mock(SchemaContext.class);
466 final Module moduleAug = mock(Module.class);
467 final DataSchemaNode schNode = null;
468 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
469 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
470 .thenReturn(moduleAug);
472 final TypeProvider typeProvider = null;
473 final Map<Module, ModuleContext> genCtx = new HashMap<>();
474 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
476 final Module m = mock(Module.class);
477 when(m.getName()).thenReturn("augm-module");
478 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
479 when(m.getRevision()).thenReturn(qnamePath.getRevision());
481 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
483 generate.invoke(AugmentToGenType.class, args);
485 } catch (final Exception e) {
487 assertTrue(e instanceof InvocationTargetException);
488 final Throwable cause = e.getCause();
489 assertNotNull(cause);
490 assertTrue(cause instanceof IllegalArgumentException);
491 assertEquals("augment target not found: " + path, cause.getMessage());
495 @SuppressWarnings({ "rawtypes" })
497 public void augmentationToGenTypesNullAugTargetGTBTest() throws Exception {
498 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
499 boolean.class, Map.class, Map.class, TypeProvider.class };
500 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
501 assertNotNull(generate);
502 generate.setAccessible(true);
504 final String augmPackName = "pckg.name";
506 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
507 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
508 final SchemaPath path = SchemaPath.create(true, qnamePath);
509 when(augmSchema.getTargetPath()).thenReturn(path);
510 final Set<UsesNode> uses = new HashSet<>();
511 when(augmSchema.getUses()).thenReturn(uses);
513 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
514 augmentationSchemaList.add(augmSchema);
515 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
516 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
517 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
519 final SchemaContext context = mock(SchemaContext.class);
520 final Module moduleAug = mock(Module.class);
521 final DataSchemaNode schNode = mock(DataSchemaNode.class);
522 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
523 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
524 .thenReturn(moduleAug);
526 final TypeProvider typeProvider = null;
527 final Map<Module, ModuleContext> genCtx = new HashMap<>();
528 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
530 final Module m = mock(Module.class);
531 when(m.getName()).thenReturn("augm-module");
532 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
533 when(m.getRevision()).thenReturn(qnamePath.getRevision());
535 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
537 generate.invoke(AugmentToGenType.class, args);
539 } catch (final Exception e) {
541 assertTrue(e instanceof InvocationTargetException);
542 final Throwable cause = e.getCause();
543 assertNotNull(cause);
544 assertTrue(cause instanceof NullPointerException);
545 assertEquals("Target type not yet generated: " + schNode, cause.getMessage());
549 @SuppressWarnings({ "rawtypes", "unchecked" })
551 public void augmentationToGenTypesAugUsesNullOrigTargetTest() throws Exception {
552 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
553 boolean.class, Map.class, Map.class, TypeProvider.class };
554 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
555 assertNotNull(generate);
556 generate.setAccessible(true);
558 final String augmPackName = "pckg.name";
560 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
561 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
562 final SchemaPath path = SchemaPath.create(true, qnamePath);
563 when(augmSchema.getTargetPath()).thenReturn(path);
564 final Set<UsesNode> uses = new HashSet<>();
565 when(augmSchema.getUses()).thenReturn(uses);
567 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
568 augmentationSchemaList.add(augmSchema);
569 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
570 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
571 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
573 final SchemaContext context = mock(SchemaContext.class);
574 final Module moduleAug = mock(Module.class);
575 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
576 when(targetSchNode.getPath()).thenReturn(path);
577 when(targetSchNode.isAddedByUses()).thenReturn(true);
578 final Optional optionalSchemaNode = Optional.absent();
579 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
580 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
581 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
582 .thenReturn(moduleAug);
584 final TypeProvider typeProvider = null;
585 final Map<Module, ModuleContext> genCtx = new HashMap<>();
586 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
588 final Module m = mock(Module.class);
589 when(m.getName()).thenReturn("augm-module");
590 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
591 when(m.getRevision()).thenReturn(qnamePath.getRevision());
593 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
595 generate.invoke(AugmentToGenType.class, args);
597 } catch (final Exception e) {
599 assertTrue(e instanceof InvocationTargetException);
600 final Throwable cause = e.getCause();
601 assertNotNull(cause);
602 assertTrue(cause instanceof IllegalStateException);
603 assertEquals("Failed to find target node from grouping in augmentation " + augmSchema + " in module "
604 + m.getName(), cause.getMessage());
608 @SuppressWarnings({ "rawtypes" })
610 public void augmentationToGenTypesTargetChoicSchemaNodeTest() throws Exception {
611 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
612 boolean.class, Map.class, Map.class, TypeProvider.class };
613 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
614 assertNotNull(generate);
615 generate.setAccessible(true);
617 final String augmPackName = "pckg.name";
619 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
620 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
621 final SchemaPath path = SchemaPath.create(true, qnamePath);
622 when(augmSchema.getTargetPath()).thenReturn(path);
623 final Set<UsesNode> uses = new HashSet<>();
624 when(augmSchema.getUses()).thenReturn(uses);
626 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
627 augmentationSchemaList.add(augmSchema);
628 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
629 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
630 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
632 final SchemaContext context = mock(SchemaContext.class);
633 final Module moduleAug = mock(Module.class);
634 final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
635 when(targetSchNode.getPath()).thenReturn(path);
636 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
637 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
638 .thenReturn(moduleAug);
640 final TypeProvider typeProvider = null;
642 final Map genCtx = mock(Map.class);
643 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
644 final ModuleContext mc = new ModuleContext();
645 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
646 mc.addChildNodeType(targetSchNode, gtb);
647 moduleContexts.add(mc);
648 when(genCtx.values()).thenReturn(moduleContexts);
650 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
652 final Module m = mock(Module.class);
653 when(m.getName()).thenReturn("augm-module");
654 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
655 when(m.getRevision()).thenReturn(qnamePath.getRevision());
657 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
658 final Map result = (Map) generate.invoke(AugmentToGenType.class, args);
659 assertNotNull(result);
662 @SuppressWarnings({ "rawtypes", "unchecked" })
664 public void augmentationToGenTypesTest() throws Exception {
665 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
666 boolean.class, Map.class, Map.class, TypeProvider.class };
667 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
668 assertNotNull(generate);
669 generate.setAccessible(true);
671 final String augmPackName = "pckg.name";
673 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
674 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
675 final SchemaPath path = SchemaPath.create(true, qnamePath);
676 when(augmSchema.getTargetPath()).thenReturn(path);
677 final Set<UsesNode> uses = new HashSet<>();
678 when(augmSchema.getUses()).thenReturn(uses);
679 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
680 when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
682 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
683 augmentationSchemaList.add(augmSchema);
684 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
685 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
686 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
688 final SchemaContext context = mock(SchemaContext.class);
689 final Module moduleAug = mock(Module.class);
690 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
691 when(targetSchNode.getPath()).thenReturn(path);
692 when(targetSchNode.isAddedByUses()).thenReturn(true);
693 final DataSchemaNode origSchNode = mock(DataSchemaNode.class);
694 when(origSchNode.getPath()).thenReturn(path);
695 when(origSchNode.isAddedByUses()).thenReturn(true);
696 when(origSchNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
697 final Optional optionalSchemaNode = Optional.of(origSchNode);
698 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
699 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
700 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
701 .thenReturn(moduleAug);
703 final TypeProvider typeProvider = null;
705 final Map<Module, ModuleContext> genCtx = new HashMap<>();
707 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
708 final ModuleContext mc = new ModuleContext();
709 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
710 mc.addChildNodeType(targetSchNode, gtb);
711 moduleContexts.add(mc);
712 genCtx.put(moduleAug, mc);
714 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
716 when(moduleAug.getName()).thenReturn("augm-module");
717 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
718 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
720 final Object[] args =
721 { augmPackName, schemaPathAugmentListEntry, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
722 final Map<Module, ModuleContext> result =
723 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
724 assertNotNull(result);
725 final ModuleContext moduleContext = result.get(moduleAug);
726 assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
727 assertEquals("pckg.name.data", moduleContext.getAugmentations().get(0).getPackageName());
728 assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
729 assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
734 public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
736 AugmentToGenType.usesAugmentationToGenTypes(null, null, null, null, null, null, null, null, false, null, null);
737 } catch (final Exception e) {
739 assertTrue(e instanceof IllegalArgumentException);
740 assertEquals(e.getMessage(), "Package Name cannot be NULL.");
746 public void usesAugmentationToGenTypesNullAugSchemaListEntryTest() throws Exception {
748 AugmentToGenType.usesAugmentationToGenTypes(null, "", null, null, null, null, null, null, false, null, null);
749 } catch (final Exception e) {
751 assertTrue(e instanceof IllegalArgumentException);
752 assertEquals(e.getMessage(), "Augmentation Schema List Entry cannot be NULL.");
758 public void usesAugmentationToGenTypesEmptyAugSchemaListTest() throws Exception {
759 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
761 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
763 } catch (final Exception e) {
765 assertTrue(e instanceof IllegalStateException);
766 assertEquals(e.getMessage(), "Augmentation Schema List cannot be empty");
772 public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
773 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
774 when(augmentationSchema.getTargetPath()).thenReturn(null);
775 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
776 augmentationSchemaList.add(augmentationSchema);
778 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
780 } catch (final Exception e) {
782 assertTrue(e instanceof IllegalStateException);
783 assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
789 public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
790 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
791 final SchemaPath path = SchemaPath.create(true, qnamePath);
792 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
793 when(augmentationSchema.getTargetPath()).thenReturn(path);
794 final Set<UsesNode> uses = new HashSet<>();
795 when(augmentationSchema.getUses()).thenReturn(uses);
797 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
798 augmentationSchemaList.add(augmentationSchema);
800 final SchemaContext context = mock(SchemaContext.class);
801 final Module moduleAug = mock(Module.class);
802 when(moduleAug.getName()).thenReturn("augm-module");
803 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
804 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
805 final Set<GroupingDefinition> groupings = new HashSet<>();
806 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
807 when(groupingDefinition.getQName()).thenReturn(qnamePath);
808 groupings.add(groupingDefinition);
809 when(moduleAug.getGroupings()).thenReturn(groupings);
811 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
812 .thenReturn(moduleAug);
814 final Map<Module, ModuleContext> genCtx = new HashMap<>();
815 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
818 final UsesNode usesNode = mock(UsesNode.class);
819 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
821 when(usesNode.getGroupingPath()).thenReturn(path);
824 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
825 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
826 } catch (final Exception e) {
828 assertTrue(e instanceof IllegalArgumentException);
829 assertEquals(e.getMessage(), "augment target not found: " + path);
835 public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
836 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
837 final SchemaPath path = SchemaPath.create(true, qnamePath);
838 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
839 when(augmentationSchema.getTargetPath()).thenReturn(path);
840 final Set<UsesNode> uses = new HashSet<>();
841 when(augmentationSchema.getUses()).thenReturn(uses);
843 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
844 augmentationSchemaList.add(augmentationSchema);
846 final SchemaContext context = mock(SchemaContext.class);
847 final Module moduleAug = mock(Module.class);
848 when(moduleAug.getName()).thenReturn("augm-module");
849 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
850 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
851 final Set<GroupingDefinition> groupings = new HashSet<>();
852 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
853 when(groupingDefinition.getQName()).thenReturn(qnamePath);
854 final DataSchemaNode schNode = mock(DataSchemaNode.class);
855 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
856 groupings.add(groupingDefinition);
857 when(moduleAug.getGroupings()).thenReturn(groupings);
859 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
860 .thenReturn(moduleAug);
862 final Map<Module, ModuleContext> genCtx = new HashMap<>();
863 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
865 final UsesNode usesNode = mock(UsesNode.class);
866 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
868 when(usesNode.getGroupingPath()).thenReturn(path);
871 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
872 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
873 } catch (final Exception e) {
875 assertTrue(e instanceof NullPointerException);
876 assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
882 public void usesAugmentationToGenTypesTest() throws Exception {
883 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
884 final SchemaPath path = SchemaPath.create(true, qnamePath);
885 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
886 when(augmentationSchema.getTargetPath()).thenReturn(path);
887 final Set<UsesNode> uses = new HashSet<>();
888 when(augmentationSchema.getUses()).thenReturn(uses);
890 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
891 augmentationSchemaList.add(augmentationSchema);
893 final SchemaContext context = mock(SchemaContext.class);
894 final Module moduleAug = mock(Module.class);
895 when(moduleAug.getName()).thenReturn("augm-module");
896 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
897 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
898 final Set<GroupingDefinition> groupings = new HashSet<>();
899 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
900 when(groupingDefinition.getQName()).thenReturn(qnamePath);
901 final DataSchemaNode schNode = mock(DataSchemaNode.class);
902 when(schNode.getPath()).thenReturn(path);
903 when(schNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
904 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
905 groupings.add(groupingDefinition);
906 when(moduleAug.getGroupings()).thenReturn(groupings);
908 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
909 .thenReturn(moduleAug);
911 final Map<Module, ModuleContext> genCtx = new HashMap<>();
912 final ModuleContext mc = new ModuleContext();
913 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
914 mc.addChildNodeType(schNode, gtb);
915 genCtx.put(moduleAug, mc);
917 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
919 final UsesNode usesNode = mock(UsesNode.class);
920 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
922 when(usesNode.getGroupingPath()).thenReturn(path);
924 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
925 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
926 BindingNamespaceType.Data);
927 assertNotNull(result);
932 public void usesAugmentationToGenTypesChoiceTest() throws Exception {
933 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
934 final SchemaPath path = SchemaPath.create(true, qnamePath);
935 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
936 when(augmentationSchema.getTargetPath()).thenReturn(path);
937 final Set<UsesNode> uses = new HashSet<>();
938 when(augmentationSchema.getUses()).thenReturn(uses);
940 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
941 augmentationSchemaList.add(augmentationSchema);
943 final SchemaContext context = mock(SchemaContext.class);
944 final Module moduleAug = mock(Module.class);
945 when(moduleAug.getName()).thenReturn("augm-module");
946 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
947 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
948 final Set<GroupingDefinition> groupings = new HashSet<>();
949 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
950 when(groupingDefinition.getQName()).thenReturn(qnamePath);
951 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
952 when(schNode.getPath()).thenReturn(path);
953 when(schNode.getQName()).thenReturn(QName.create("test", "2017-04-04", "aug-node"));
954 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
955 groupings.add(groupingDefinition);
956 when(moduleAug.getGroupings()).thenReturn(groupings);
958 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
959 .thenReturn(moduleAug);
961 final Map<Module, ModuleContext> genCtx = new HashMap<>();
962 final ModuleContext mc = new ModuleContext();
963 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
964 mc.addChildNodeType(schNode, gtb);
965 genCtx.put(moduleAug, mc);
967 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
969 final UsesNode usesNode = mock(UsesNode.class);
970 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
972 when(usesNode.getGroupingPath()).thenReturn(path);
974 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
975 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
976 BindingNamespaceType.Data);
977 assertNotNull(result);
980 @SuppressWarnings({ "rawtypes" })
982 public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
983 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
984 final Method generate =
985 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
986 assertNotNull(generate);
987 generate.setAccessible(true);
989 final Module module = mock(Module.class);
990 final QName qnamePath = QName.create("test", "2017-04-04", "test");
991 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
992 final DataSchemaNode schNode = mock(DataSchemaNode.class);
993 when(schNode.getPath()).thenReturn(schemaPath);
994 when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
996 final SchemaContext context = mock(SchemaContext.class);
997 when(context.findModuleByNamespaceAndRevision(qnamePath .getNamespace(), qnamePath.getRevision()))
999 final UsesNode usesNode = mock(UsesNode.class);
1000 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1002 final Object[] args = { context, schemaPath, usesNode };
1004 generate.invoke(AugmentToGenType.class, args);
1006 } catch (final Exception e) {
1008 assertTrue(e instanceof InvocationTargetException);
1009 final Throwable cause = e.getCause();
1010 assertNotNull(cause);
1011 assertTrue(cause instanceof IllegalArgumentException);
1012 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1016 @SuppressWarnings({ "rawtypes" })
1018 public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
1019 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1020 final Method generate =
1021 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1022 assertNotNull(generate);
1023 generate.setAccessible(true);
1025 final Module module = mock(Module.class);
1026 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1027 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1028 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1029 when(schNode.getPath()).thenReturn(schemaPath);
1030 when(schNode.isAddedByUses()).thenReturn(true);
1031 final Set<GroupingDefinition> groupings = new HashSet<>();
1032 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1033 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1034 groupings.add(groupingDefinition);
1035 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1036 when(module.getGroupings()).thenReturn(groupings);
1038 final SchemaContext context = mock(SchemaContext.class);
1039 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1040 .thenReturn(module);
1041 final UsesNode usesNode = mock(UsesNode.class);
1042 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1044 final Object[] args = { context, schemaPath, usesNode };
1046 generate.invoke(AugmentToGenType.class, args);
1048 } catch (final Exception e) {
1050 assertTrue(e instanceof InvocationTargetException);
1051 final Throwable cause = e.getCause();
1052 assertNotNull(cause);
1053 assertTrue(cause instanceof IllegalStateException);
1054 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1058 @SuppressWarnings({ "rawtypes" })
1060 public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
1061 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1062 final Method generate =
1063 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1064 assertNotNull(generate);
1065 generate.setAccessible(true);
1067 final Module module = mock(Module.class);
1068 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1069 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1070 final DataSchemaNode schNode = null;
1071 final Set<GroupingDefinition> groupings = new HashSet<>();
1072 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1073 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1074 groupings.add(groupingDefinition);
1075 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1076 when(module.getGroupings()).thenReturn(groupings);
1078 final SchemaContext context = mock(SchemaContext.class);
1079 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1080 .thenReturn(module);
1081 final UsesNode usesNode = mock(UsesNode.class);
1082 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1084 final Object[] args = { context, schemaPath, usesNode };
1085 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1086 assertEquals(null, result);
1089 @SuppressWarnings({ "rawtypes", "unchecked" })
1091 public void findOriginalTargetFromGroupingTest() throws Exception {
1092 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1093 final Method generate =
1094 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1095 assertNotNull(generate);
1096 generate.setAccessible(true);
1098 final Module module = mock(Module.class);
1099 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1100 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1101 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1102 when(schNode.getPath()).thenReturn(schemaPath);
1103 final Set<GroupingDefinition> groupings = new HashSet<>();
1104 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1105 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1106 groupings.add(groupingDefinition);
1107 final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
1108 when(derivSchNode.isAddedByUses()).thenReturn(true);
1109 final Optional optional = Optional.of(schNode);
1110 when(derivSchNode.getOriginal()).thenReturn(optional);
1111 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
1112 when(module.getGroupings()).thenReturn(groupings);
1114 final SchemaContext context = mock(SchemaContext.class);
1115 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1116 .thenReturn(module);
1117 final UsesNode usesNode = mock(UsesNode.class);
1118 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1120 final Object[] args = { context, schemaPath, usesNode };
1121 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1122 assertEquals(schNode, result);
1125 @SuppressWarnings({ "rawtypes" })
1127 public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1128 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1129 final Method generate =
1130 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1131 assertNotNull(generate);
1132 generate.setAccessible(true);
1134 final Module module = mock(Module.class);
1135 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1136 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1137 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1138 when(schNode.getPath()).thenReturn(schemaPath);
1139 final Set<GroupingDefinition> groupings = new HashSet<>();
1140 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1141 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1142 groupings.add(groupingDefinition);
1143 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1144 when(module.getGroupings()).thenReturn(groupings);
1146 final SchemaContext context = mock(SchemaContext.class);
1147 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1148 .thenReturn(module);
1149 final UsesNode usesNode = mock(UsesNode.class);
1150 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1152 final Object[] args = { context, schemaPath, usesNode };
1153 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1154 assertEquals(schNode, result);
1157 @SuppressWarnings({ "rawtypes" })
1159 public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1160 final Class[] parameterTypes =
1161 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1162 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1163 BindingNamespaceType.class};
1164 final Method generate =
1165 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1166 assertNotNull(generate);
1167 generate.setAccessible(true);
1169 final SchemaContext schemaContext = null;
1170 final Module module = null;
1171 final String pckgName = null;
1172 final Type targetType = null;
1173 final ChoiceSchemaNode targetNode = null;
1174 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1175 final DataNodeContainer usesNodeParent = null;
1176 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1177 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1179 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1180 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1182 generate.invoke(AugmentToGenType.class, args);
1184 } catch (final Exception e) {
1186 assertTrue(e instanceof InvocationTargetException);
1187 final Throwable cause = e.getCause();
1188 assertNotNull(cause);
1189 assertTrue(cause instanceof IllegalArgumentException);
1190 assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1194 @SuppressWarnings({ "rawtypes" })
1196 public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1197 final Class[] parameterTypes =
1198 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1199 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1200 BindingNamespaceType.class };
1201 final Method generate =
1202 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1203 assertNotNull(generate);
1204 generate.setAccessible(true);
1206 final SchemaContext schemaContext = null;
1207 final Module module = null;
1208 final String pckgName = "";
1209 final Type targetType = null;
1210 final ChoiceSchemaNode targetNode = null;
1211 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1212 final DataNodeContainer usesNodeParent = null;
1213 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1214 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1216 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1217 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1219 generate.invoke(AugmentToGenType.class, args);
1221 } catch (final Exception e) {
1223 assertTrue(e instanceof InvocationTargetException);
1224 final Throwable cause = e.getCause();
1225 assertNotNull(cause);
1226 assertTrue(cause instanceof IllegalArgumentException);
1227 assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1231 @SuppressWarnings({ "rawtypes" })
1233 public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1234 final Class[] parameterTypes =
1235 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1236 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1237 BindingNamespaceType.class };
1238 final Method generate =
1239 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1240 assertNotNull(generate);
1241 generate.setAccessible(true);
1243 final SchemaContext schemaContext = null;
1244 final Module module = null;
1245 final String pckgName = "";
1246 final Type targetType = mock(Type.class);
1247 final ChoiceSchemaNode targetNode = null;
1248 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1249 final DataNodeContainer usesNodeParent = null;
1250 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1251 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1253 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1254 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1256 generate.invoke(AugmentToGenType.class, args);
1258 } catch (final Exception e) {
1260 assertTrue(e instanceof InvocationTargetException);
1261 final Throwable cause = e.getCause();
1262 assertNotNull(cause);
1263 assertTrue(cause instanceof IllegalArgumentException);
1264 assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1268 @SuppressWarnings({ "rawtypes", "unchecked" })
1270 public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1271 final Class[] parameterTypes =
1272 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1273 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1274 BindingNamespaceType.class };
1275 final Method generate =
1276 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1277 assertNotNull(generate);
1278 generate.setAccessible(true);
1280 final SchemaContext schemaContext = null;
1281 final Module module = null;
1282 final String pckgName = "";
1283 final Type targetType = mock(Type.class);
1284 final ChoiceSchemaNode targetNode = null;
1285 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1286 final DataSchemaNode caseNode = null;
1287 augmentNodes.add(caseNode);
1289 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1290 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1291 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1292 schemaPathAugmentListEntry.add(augmentationSchema);
1294 final DataNodeContainer usesNodeParent = null;
1295 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1296 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1298 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1299 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1300 final Map<Module, ModuleContext> result =
1301 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1302 assertEquals(genCtx, result);
1305 @SuppressWarnings({ "rawtypes" })
1307 public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1308 final Class[] parameterTypes =
1309 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1310 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1311 BindingNamespaceType.class };
1312 final Method generate =
1313 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1314 assertNotNull(generate);
1315 generate.setAccessible(true);
1317 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1318 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1319 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1321 final SchemaContext schemaContext = mock(SchemaContext.class);
1322 final Module module = mock(Module.class);
1323 when(module.getName()).thenReturn("test-module-case");
1324 final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1325 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1326 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1327 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1328 final String pckgName = "test.augment.choice.cases";
1329 final Type targetType = mock(Type.class);
1330 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1331 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1332 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1333 when(caseNode.getPath()).thenReturn(path);
1334 when(caseNode.getQName()).thenReturn(qnamePath);
1335 augmentNodes.add(caseNode);
1337 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1338 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1339 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1340 schemaPathAugmentListEntry.add(augmentationSchema);
1342 final DataNodeContainer usesNodeParent = null;
1343 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1344 when(targetNode.getPath()).thenReturn(path);
1345 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1346 final ModuleContext moduleContext = new ModuleContext();
1347 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1348 moduleContext.addCaseType(path, gtb);
1349 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1351 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1352 .thenReturn(module);
1354 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1355 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1357 generate.invoke(AugmentToGenType.class, args);
1359 } catch (final Exception e) {
1361 assertTrue(e instanceof InvocationTargetException);
1362 final Throwable cause = e.getCause();
1363 assertNotNull(cause);
1364 assertTrue(cause instanceof IllegalArgumentException);
1365 assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1369 @SuppressWarnings({ "rawtypes", "unchecked" })
1371 public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1372 final Class[] parameterTypes =
1373 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1374 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1375 BindingNamespaceType.class };
1376 final Method generate =
1377 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1378 assertNotNull(generate);
1379 generate.setAccessible(true);
1381 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1382 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1383 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1385 final SchemaContext schemaContext = mock(SchemaContext.class);
1386 final Module module = mock(Module.class);
1387 when(module.getName()).thenReturn("test-module-case");
1388 final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1389 when(schemaNode.getPath()).thenReturn(path);
1390 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1391 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1392 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1393 final String pckgName = "test.augment.choice.cases";
1394 final Type targetType = mock(Type.class);
1395 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1396 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1397 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1398 when(caseNode.getPath()).thenReturn(path);
1399 when(caseNode.getQName()).thenReturn(qnamePath);
1400 augmentNodes.add(caseNode);
1402 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1403 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1404 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1405 schemaPathAugmentListEntry.add(augmentationSchema);
1407 final DataNodeContainer usesNodeParent = null;
1408 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1409 when(targetNode.getPath()).thenReturn(path);
1410 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1411 final ModuleContext moduleContext = new ModuleContext();
1412 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1413 moduleContext.addCaseType(path, gtb);
1414 genCtx.put(module, moduleContext);
1415 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1417 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1418 .thenReturn(module);
1420 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1421 genCtx, false, genTypeBuilder, null ,BindingNamespaceType.Data };
1422 final Map<Module, ModuleContext> result =
1423 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1424 assertNotNull(result);
1425 assertEquals(result.get(module), moduleContext);