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(q2.getNamespace(), q2.getRevision())).thenReturn(m2);
289 when(schemaContext.findModuleByNamespaceAndRevision(q5.getNamespace(), q5.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 final Optional optionalSchemaNode = Optional.of(origSchNode);
697 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
698 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
699 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
700 .thenReturn(moduleAug);
702 final TypeProvider typeProvider = null;
704 final Map<Module, ModuleContext> genCtx = new HashMap<>();
706 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
707 final ModuleContext mc = new ModuleContext();
708 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
709 mc.addChildNodeType(targetSchNode, gtb);
710 moduleContexts.add(mc);
711 genCtx.put(moduleAug, mc);
713 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
715 when(moduleAug.getName()).thenReturn("augm-module");
716 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
717 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
719 final Object[] args =
720 { augmPackName, schemaPathAugmentListEntry, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
721 final Map<Module, ModuleContext> result =
722 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
723 assertNotNull(result);
724 final ModuleContext moduleContext = result.get(moduleAug);
725 assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
726 assertEquals("pckg.name.data.aug", moduleContext.getAugmentations().get(0).getPackageName());
727 assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
728 assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
733 public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
735 AugmentToGenType.usesAugmentationToGenTypes(null, null, null, null, null, null, null, null, false, null, null);
736 } catch (final Exception e) {
738 assertTrue(e instanceof IllegalArgumentException);
739 assertEquals(e.getMessage(), "Package Name cannot be NULL.");
745 public void usesAugmentationToGenTypesNullAugSchemaListEntryTest() throws Exception {
747 AugmentToGenType.usesAugmentationToGenTypes(null, "", null, null, null, null, null, null, false, null, null);
748 } catch (final Exception e) {
750 assertTrue(e instanceof IllegalArgumentException);
751 assertEquals(e.getMessage(), "Augmentation Schema List Entry cannot be NULL.");
757 public void usesAugmentationToGenTypesEmptyAugSchemaListTest() throws Exception {
758 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
760 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
762 } catch (final Exception e) {
764 assertTrue(e instanceof IllegalStateException);
765 assertEquals(e.getMessage(), "Augmentation Schema List cannot be empty");
771 public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
772 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
773 when(augmentationSchema.getTargetPath()).thenReturn(null);
774 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
775 augmentationSchemaList.add(augmentationSchema);
777 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
779 } catch (final Exception e) {
781 assertTrue(e instanceof IllegalStateException);
782 assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
788 public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
789 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
790 final SchemaPath path = SchemaPath.create(true, qnamePath);
791 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
792 when(augmentationSchema.getTargetPath()).thenReturn(path);
793 final Set<UsesNode> uses = new HashSet<>();
794 when(augmentationSchema.getUses()).thenReturn(uses);
796 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
797 augmentationSchemaList.add(augmentationSchema);
799 final SchemaContext context = mock(SchemaContext.class);
800 final Module moduleAug = mock(Module.class);
801 when(moduleAug.getName()).thenReturn("augm-module");
802 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
803 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
804 final Set<GroupingDefinition> groupings = new HashSet<>();
805 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
806 when(groupingDefinition.getQName()).thenReturn(qnamePath);
807 groupings.add(groupingDefinition);
808 when(moduleAug.getGroupings()).thenReturn(groupings);
810 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
811 .thenReturn(moduleAug);
813 final Map<Module, ModuleContext> genCtx = new HashMap<>();
814 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
817 final UsesNode usesNode = mock(UsesNode.class);
818 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
820 when(usesNode.getGroupingPath()).thenReturn(path);
823 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
824 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
825 } catch (final Exception e) {
827 assertTrue(e instanceof IllegalArgumentException);
828 assertEquals(e.getMessage(), "augment target not found: " + path);
834 public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
835 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
836 final SchemaPath path = SchemaPath.create(true, qnamePath);
837 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
838 when(augmentationSchema.getTargetPath()).thenReturn(path);
839 final Set<UsesNode> uses = new HashSet<>();
840 when(augmentationSchema.getUses()).thenReturn(uses);
842 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
843 augmentationSchemaList.add(augmentationSchema);
845 final SchemaContext context = mock(SchemaContext.class);
846 final Module moduleAug = mock(Module.class);
847 when(moduleAug.getName()).thenReturn("augm-module");
848 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
849 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
850 final Set<GroupingDefinition> groupings = new HashSet<>();
851 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
852 when(groupingDefinition.getQName()).thenReturn(qnamePath);
853 final DataSchemaNode schNode = mock(DataSchemaNode.class);
854 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
855 groupings.add(groupingDefinition);
856 when(moduleAug.getGroupings()).thenReturn(groupings);
858 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
859 .thenReturn(moduleAug);
861 final Map<Module, ModuleContext> genCtx = new HashMap<>();
862 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
864 final UsesNode usesNode = mock(UsesNode.class);
865 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
867 when(usesNode.getGroupingPath()).thenReturn(path);
870 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
871 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
872 } catch (final Exception e) {
874 assertTrue(e instanceof NullPointerException);
875 assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
881 public void usesAugmentationToGenTypesTest() throws Exception {
882 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
883 final SchemaPath path = SchemaPath.create(true, qnamePath);
884 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
885 when(augmentationSchema.getTargetPath()).thenReturn(path);
886 final Set<UsesNode> uses = new HashSet<>();
887 when(augmentationSchema.getUses()).thenReturn(uses);
889 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
890 augmentationSchemaList.add(augmentationSchema);
892 final SchemaContext context = mock(SchemaContext.class);
893 final Module moduleAug = mock(Module.class);
894 when(moduleAug.getName()).thenReturn("augm-module");
895 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
896 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
897 final Set<GroupingDefinition> groupings = new HashSet<>();
898 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
899 when(groupingDefinition.getQName()).thenReturn(qnamePath);
900 final DataSchemaNode schNode = mock(DataSchemaNode.class);
901 when(schNode.getPath()).thenReturn(path);
902 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
903 groupings.add(groupingDefinition);
904 when(moduleAug.getGroupings()).thenReturn(groupings);
906 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
907 .thenReturn(moduleAug);
909 final Map<Module, ModuleContext> genCtx = new HashMap<>();
910 final ModuleContext mc = new ModuleContext();
911 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
912 mc.addChildNodeType(schNode, gtb);
913 genCtx.put(moduleAug, mc);
915 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
917 final UsesNode usesNode = mock(UsesNode.class);
918 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
920 when(usesNode.getGroupingPath()).thenReturn(path);
922 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
923 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
924 BindingNamespaceType.Data);
925 assertNotNull(result);
930 public void usesAugmentationToGenTypesChoiceTest() throws Exception {
931 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
932 final SchemaPath path = SchemaPath.create(true, qnamePath);
933 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
934 when(augmentationSchema.getTargetPath()).thenReturn(path);
935 final Set<UsesNode> uses = new HashSet<>();
936 when(augmentationSchema.getUses()).thenReturn(uses);
938 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
939 augmentationSchemaList.add(augmentationSchema);
941 final SchemaContext context = mock(SchemaContext.class);
942 final Module moduleAug = mock(Module.class);
943 when(moduleAug.getName()).thenReturn("augm-module");
944 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
945 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
946 final Set<GroupingDefinition> groupings = new HashSet<>();
947 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
948 when(groupingDefinition.getQName()).thenReturn(qnamePath);
949 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
950 when(schNode.getPath()).thenReturn(path);
951 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
952 groupings.add(groupingDefinition);
953 when(moduleAug.getGroupings()).thenReturn(groupings);
955 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
956 .thenReturn(moduleAug);
958 final Map<Module, ModuleContext> genCtx = new HashMap<>();
959 final ModuleContext mc = new ModuleContext();
960 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
961 mc.addChildNodeType(schNode, gtb);
962 genCtx.put(moduleAug, mc);
964 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
966 final UsesNode usesNode = mock(UsesNode.class);
967 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
969 when(usesNode.getGroupingPath()).thenReturn(path);
971 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
972 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
973 BindingNamespaceType.Data);
974 assertNotNull(result);
977 @SuppressWarnings({ "rawtypes" })
979 public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
980 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
981 final Method generate =
982 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
983 assertNotNull(generate);
984 generate.setAccessible(true);
986 final Module module = mock(Module.class);
987 final QName qnamePath = QName.create("test", "2017-04-04", "test");
988 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
989 final DataSchemaNode schNode = mock(DataSchemaNode.class);
990 when(schNode.getPath()).thenReturn(schemaPath);
991 when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
993 final SchemaContext context = mock(SchemaContext.class);
994 when(context.findModuleByNamespaceAndRevision(qnamePath .getNamespace(), qnamePath.getRevision()))
996 final UsesNode usesNode = mock(UsesNode.class);
997 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
999 final Object[] args = { context, schemaPath, usesNode };
1001 generate.invoke(AugmentToGenType.class, args);
1003 } catch (final Exception e) {
1005 assertTrue(e instanceof InvocationTargetException);
1006 final Throwable cause = e.getCause();
1007 assertNotNull(cause);
1008 assertTrue(cause instanceof IllegalArgumentException);
1009 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1013 @SuppressWarnings({ "rawtypes" })
1015 public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
1016 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1017 final Method generate =
1018 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1019 assertNotNull(generate);
1020 generate.setAccessible(true);
1022 final Module module = mock(Module.class);
1023 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1024 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1025 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1026 when(schNode.getPath()).thenReturn(schemaPath);
1027 when(schNode.isAddedByUses()).thenReturn(true);
1028 final Set<GroupingDefinition> groupings = new HashSet<>();
1029 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1030 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1031 groupings.add(groupingDefinition);
1032 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1033 when(module.getGroupings()).thenReturn(groupings);
1035 final SchemaContext context = mock(SchemaContext.class);
1036 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1037 .thenReturn(module);
1038 final UsesNode usesNode = mock(UsesNode.class);
1039 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1041 final Object[] args = { context, schemaPath, usesNode };
1043 generate.invoke(AugmentToGenType.class, args);
1045 } catch (final Exception e) {
1047 assertTrue(e instanceof InvocationTargetException);
1048 final Throwable cause = e.getCause();
1049 assertNotNull(cause);
1050 assertTrue(cause instanceof IllegalStateException);
1051 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1055 @SuppressWarnings({ "rawtypes" })
1057 public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
1058 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1059 final Method generate =
1060 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1061 assertNotNull(generate);
1062 generate.setAccessible(true);
1064 final Module module = mock(Module.class);
1065 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1066 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1067 final DataSchemaNode schNode = null;
1068 final Set<GroupingDefinition> groupings = new HashSet<>();
1069 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1070 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1071 groupings.add(groupingDefinition);
1072 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1073 when(module.getGroupings()).thenReturn(groupings);
1075 final SchemaContext context = mock(SchemaContext.class);
1076 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1077 .thenReturn(module);
1078 final UsesNode usesNode = mock(UsesNode.class);
1079 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1081 final Object[] args = { context, schemaPath, usesNode };
1082 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1083 assertEquals(null, result);
1086 @SuppressWarnings({ "rawtypes", "unchecked" })
1088 public void findOriginalTargetFromGroupingTest() throws Exception {
1089 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1090 final Method generate =
1091 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1092 assertNotNull(generate);
1093 generate.setAccessible(true);
1095 final Module module = mock(Module.class);
1096 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1097 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1098 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1099 when(schNode.getPath()).thenReturn(schemaPath);
1100 final Set<GroupingDefinition> groupings = new HashSet<>();
1101 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1102 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1103 groupings.add(groupingDefinition);
1104 final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
1105 when(derivSchNode.isAddedByUses()).thenReturn(true);
1106 final Optional optional = Optional.of(schNode);
1107 when(derivSchNode.getOriginal()).thenReturn(optional);
1108 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
1109 when(module.getGroupings()).thenReturn(groupings);
1111 final SchemaContext context = mock(SchemaContext.class);
1112 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1113 .thenReturn(module);
1114 final UsesNode usesNode = mock(UsesNode.class);
1115 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1117 final Object[] args = { context, schemaPath, usesNode };
1118 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1119 assertEquals(schNode, result);
1122 @SuppressWarnings({ "rawtypes" })
1124 public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1125 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1126 final Method generate =
1127 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1128 assertNotNull(generate);
1129 generate.setAccessible(true);
1131 final Module module = mock(Module.class);
1132 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1133 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1134 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1135 when(schNode.getPath()).thenReturn(schemaPath);
1136 final Set<GroupingDefinition> groupings = new HashSet<>();
1137 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1138 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1139 groupings.add(groupingDefinition);
1140 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1141 when(module.getGroupings()).thenReturn(groupings);
1143 final SchemaContext context = mock(SchemaContext.class);
1144 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1145 .thenReturn(module);
1146 final UsesNode usesNode = mock(UsesNode.class);
1147 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1149 final Object[] args = { context, schemaPath, usesNode };
1150 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1151 assertEquals(schNode, result);
1154 @SuppressWarnings({ "rawtypes" })
1156 public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1157 final Class[] parameterTypes =
1158 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1159 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1160 BindingNamespaceType.class};
1161 final Method generate =
1162 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1163 assertNotNull(generate);
1164 generate.setAccessible(true);
1166 final SchemaContext schemaContext = null;
1167 final Module module = null;
1168 final String pckgName = null;
1169 final Type targetType = null;
1170 final ChoiceSchemaNode targetNode = null;
1171 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1172 final DataNodeContainer usesNodeParent = null;
1173 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1174 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1176 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1177 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1179 generate.invoke(AugmentToGenType.class, args);
1181 } catch (final Exception e) {
1183 assertTrue(e instanceof InvocationTargetException);
1184 final Throwable cause = e.getCause();
1185 assertNotNull(cause);
1186 assertTrue(cause instanceof IllegalArgumentException);
1187 assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1191 @SuppressWarnings({ "rawtypes" })
1193 public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1194 final Class[] parameterTypes =
1195 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1196 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1197 BindingNamespaceType.class };
1198 final Method generate =
1199 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1200 assertNotNull(generate);
1201 generate.setAccessible(true);
1203 final SchemaContext schemaContext = null;
1204 final Module module = null;
1205 final String pckgName = "";
1206 final Type targetType = null;
1207 final ChoiceSchemaNode targetNode = null;
1208 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1209 final DataNodeContainer usesNodeParent = null;
1210 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1211 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1213 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1214 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1216 generate.invoke(AugmentToGenType.class, args);
1218 } catch (final Exception e) {
1220 assertTrue(e instanceof InvocationTargetException);
1221 final Throwable cause = e.getCause();
1222 assertNotNull(cause);
1223 assertTrue(cause instanceof IllegalArgumentException);
1224 assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1228 @SuppressWarnings({ "rawtypes" })
1230 public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1231 final Class[] parameterTypes =
1232 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1233 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1234 BindingNamespaceType.class };
1235 final Method generate =
1236 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1237 assertNotNull(generate);
1238 generate.setAccessible(true);
1240 final SchemaContext schemaContext = null;
1241 final Module module = null;
1242 final String pckgName = "";
1243 final Type targetType = mock(Type.class);
1244 final ChoiceSchemaNode targetNode = null;
1245 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1246 final DataNodeContainer usesNodeParent = null;
1247 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1248 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1250 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1251 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1253 generate.invoke(AugmentToGenType.class, args);
1255 } catch (final Exception e) {
1257 assertTrue(e instanceof InvocationTargetException);
1258 final Throwable cause = e.getCause();
1259 assertNotNull(cause);
1260 assertTrue(cause instanceof IllegalArgumentException);
1261 assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1265 @SuppressWarnings({ "rawtypes", "unchecked" })
1267 public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1268 final Class[] parameterTypes =
1269 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1270 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1271 BindingNamespaceType.class };
1272 final Method generate =
1273 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1274 assertNotNull(generate);
1275 generate.setAccessible(true);
1277 final SchemaContext schemaContext = null;
1278 final Module module = null;
1279 final String pckgName = "";
1280 final Type targetType = mock(Type.class);
1281 final ChoiceSchemaNode targetNode = null;
1282 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1283 final DataSchemaNode caseNode = null;
1284 augmentNodes.add(caseNode);
1286 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1287 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1288 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1289 schemaPathAugmentListEntry.add(augmentationSchema);
1291 final DataNodeContainer usesNodeParent = null;
1292 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1293 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1295 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1296 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1297 final Map<Module, ModuleContext> result =
1298 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1299 assertEquals(genCtx, result);
1302 @SuppressWarnings({ "rawtypes" })
1304 public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1305 final Class[] parameterTypes =
1306 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1307 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1308 BindingNamespaceType.class };
1309 final Method generate =
1310 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1311 assertNotNull(generate);
1312 generate.setAccessible(true);
1314 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1315 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1316 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1318 final SchemaContext schemaContext = mock(SchemaContext.class);
1319 final Module module = mock(Module.class);
1320 when(module.getName()).thenReturn("test-module-case");
1321 final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1322 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1323 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1324 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1325 final String pckgName = "test.augment.choice.cases";
1326 final Type targetType = mock(Type.class);
1327 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1328 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1329 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1330 when(caseNode.getPath()).thenReturn(path);
1331 when(caseNode.getQName()).thenReturn(qnamePath);
1332 augmentNodes.add(caseNode);
1334 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1335 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1336 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1337 schemaPathAugmentListEntry.add(augmentationSchema);
1339 final DataNodeContainer usesNodeParent = null;
1340 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1341 when(targetNode.getPath()).thenReturn(path);
1342 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1343 final ModuleContext moduleContext = new ModuleContext();
1344 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1345 moduleContext.addCaseType(path, gtb);
1346 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1348 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1349 .thenReturn(module);
1351 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1352 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1354 generate.invoke(AugmentToGenType.class, args);
1356 } catch (final Exception e) {
1358 assertTrue(e instanceof InvocationTargetException);
1359 final Throwable cause = e.getCause();
1360 assertNotNull(cause);
1361 assertTrue(cause instanceof IllegalArgumentException);
1362 assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1366 @SuppressWarnings({ "rawtypes", "unchecked" })
1368 public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1369 final Class[] parameterTypes =
1370 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1371 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1372 BindingNamespaceType.class };
1373 final Method generate =
1374 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1375 assertNotNull(generate);
1376 generate.setAccessible(true);
1378 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1379 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1380 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1382 final SchemaContext schemaContext = mock(SchemaContext.class);
1383 final Module module = mock(Module.class);
1384 when(module.getName()).thenReturn("test-module-case");
1385 final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1386 when(schemaNode.getPath()).thenReturn(path);
1387 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1388 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1389 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1390 final String pckgName = "test.augment.choice.cases";
1391 final Type targetType = mock(Type.class);
1392 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1393 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1394 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1395 when(caseNode.getPath()).thenReturn(path);
1396 when(caseNode.getQName()).thenReturn(qnamePath);
1397 augmentNodes.add(caseNode);
1399 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1400 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1401 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1402 schemaPathAugmentListEntry.add(augmentationSchema);
1404 final DataNodeContainer usesNodeParent = null;
1405 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1406 when(targetNode.getPath()).thenReturn(path);
1407 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1408 final ModuleContext moduleContext = new ModuleContext();
1409 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1410 moduleContext.addCaseType(path, gtb);
1411 genCtx.put(module, moduleContext);
1412 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1414 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1415 .thenReturn(module);
1417 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1418 genCtx, false, genTypeBuilder, null ,BindingNamespaceType.Data };
1419 final Map<Module, ModuleContext> result =
1420 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1421 assertNotNull(result);
1422 assertEquals(result.get(module), moduleContext);