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 };
213 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
214 assertNotNull(generate);
215 generate.setAccessible(true);
217 final Module m = null;
219 final Object[] args = { m };
221 generate.invoke(AugmentToGenType.class, args);
223 } catch (final Exception e) {
225 assertTrue(e instanceof InvocationTargetException);
226 final Throwable cause = e.getCause();
227 assertNotNull(cause);
228 assertTrue(cause instanceof IllegalArgumentException);
229 assertEquals("Module reference cannot be NULL.", cause.getMessage());
233 @SuppressWarnings("rawtypes")
235 public void resolveAugmentationsNullAugmentationsTest() throws Exception {
236 final Class[] parameterTypes = { Module.class };
237 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
238 assertNotNull(generate);
239 generate.setAccessible(true);
241 final Module m = mock(Module.class);
242 when(m.getAugmentations()).thenReturn(null);
244 final Object[] args = { m };
246 generate.invoke(AugmentToGenType.class, args);
248 } catch (final Exception e) {
250 assertTrue(e instanceof InvocationTargetException);
251 final Throwable cause = e.getCause();
252 assertNotNull(cause);
253 assertTrue(cause instanceof IllegalStateException);
254 assertEquals("Augmentations Set cannot be NULL.", cause.getMessage());
258 @SuppressWarnings({ "rawtypes", "unchecked" })
260 public void resolveAugmentationsTest() throws Exception {
261 final Class[] parameterTypes = { Module.class };
262 final Method generate = AugmentToGenType.class.getDeclaredMethod("resolveAugmentations", parameterTypes);
263 assertNotNull(generate);
264 generate.setAccessible(true);
266 final Module m = mock(Module.class);
268 final Set<AugmentationSchema> augmentations = new HashSet<>();
270 final QName q1 = QName.create("q1", "2017-04-04", "q1");
271 final QName q2 = QName.create("q2", "2017-04-04", "q2");
272 final QName q3 = QName.create("q3", "2017-04-04", "q3");
273 final QName q4 = QName.create("q4", "2017-04-04", "q4");
274 final QName q5 = QName.create("q5", "2017-04-04", "q5");
276 final AugmentationSchema augmentationSchema1 = mock(AugmentationSchema.class);
277 when(augmentationSchema1.getTargetPath()).thenReturn(SchemaPath.create(true, q1, q2));
278 final AugmentationSchema augmentationSchema2 = mock(AugmentationSchema.class);
279 when(augmentationSchema2.getTargetPath()).thenReturn(SchemaPath.create(true, q3, q4, q5));
280 augmentations.add(augmentationSchema1);
281 augmentations.add(augmentationSchema2);
283 when(m.getAugmentations()).thenReturn(augmentations);
285 final Object[] args = { m };
287 final List<AugmentationSchema> result =
288 (List<AugmentationSchema>) generate.invoke(AugmentToGenType.class, args);
289 assertNotNull(result);
290 assertTrue(!result.isEmpty());
291 assertEquals(result.get(0), augmentationSchema1);
292 assertEquals(result.get(1), augmentationSchema2);
295 @SuppressWarnings({ "rawtypes" })
297 public void augmentationToGenTypesNullPckgNameTest() throws Exception {
298 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
299 boolean.class, Map.class, Map.class, TypeProvider.class };
300 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
301 assertNotNull(generate);
302 generate.setAccessible(true);
304 final String augmPackName = null;
305 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
306 final SchemaContext context = null;
307 final TypeProvider typeProvider = null;
308 final Map<Module, ModuleContext> genCtx = new HashMap<>();
309 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
310 final Module m = null;
312 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
314 generate.invoke(AugmentToGenType.class, args);
316 } catch (final Exception e) {
318 assertTrue(e instanceof InvocationTargetException);
319 final Throwable cause = e.getCause();
320 assertNotNull(cause);
321 assertTrue(cause instanceof IllegalArgumentException);
322 assertEquals("Package Name cannot be NULL.", cause.getMessage());
326 @SuppressWarnings({ "rawtypes" })
328 public void augmentationToGenTypesNullAugSchemaTest() throws Exception {
329 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
330 boolean.class, Map.class, Map.class, TypeProvider.class };
331 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
332 assertNotNull(generate);
333 generate.setAccessible(true);
335 final String augmPackName = "pckg.name";
336 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = null;
337 final SchemaContext context = null;
338 final TypeProvider typeProvider = null;
339 final Map<Module, ModuleContext> genCtx = new HashMap<>();
340 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
341 final Module m = null;
343 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
345 generate.invoke(AugmentToGenType.class, args);
347 } catch (final Exception e) {
349 assertTrue(e instanceof InvocationTargetException);
350 final Throwable cause = e.getCause();
351 assertNotNull(cause);
352 assertTrue(cause instanceof IllegalArgumentException);
353 assertEquals("Augmentation List Entry cannot be NULL.", cause.getMessage());
357 @SuppressWarnings({ "rawtypes" })
359 public void augmentationToGenTypesNullAugSchemaTargetPathTest() throws Exception {
360 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
361 boolean.class, Map.class, Map.class, TypeProvider.class };
362 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
363 assertNotNull(generate);
364 generate.setAccessible(true);
366 final String augmPackName = "pckg.name";
367 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
368 when(augmSchema.getTargetPath()).thenReturn(null);
370 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
371 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
372 when(schemaPathAugmentListEntry.getKey()).thenReturn(null);
373 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
375 final SchemaContext context = null;
376 final TypeProvider typeProvider = null;
377 final Map<Module, ModuleContext> genCtx = new HashMap<>();
378 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
379 final Module m = null;
381 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
383 generate.invoke(AugmentToGenType.class, args);
385 } catch (final Exception e) {
387 assertTrue(e instanceof InvocationTargetException);
388 final Throwable cause = e.getCause();
389 assertNotNull(cause);
390 assertTrue(cause instanceof IllegalStateException);
391 assertEquals("Augmentation List Entry does not contain Target Path (Target Path is NULL).", cause.getMessage());
395 @SuppressWarnings({ "rawtypes" })
397 public void augmentationToGenTypesNullAugSchemaListTest() throws Exception {
398 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
399 boolean.class, Map.class, Map.class, TypeProvider.class };
400 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
401 assertNotNull(generate);
402 generate.setAccessible(true);
404 final String augmPackName = "pckg.name";
405 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
406 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
407 final SchemaPath path = SchemaPath.create(true, qnamePath);
408 when(augmSchema.getTargetPath()).thenReturn(path);
410 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
411 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
412 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
413 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
415 final SchemaContext context = null;
416 final TypeProvider typeProvider = null;
417 final Map<Module, ModuleContext> genCtx = new HashMap<>();
418 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
419 final Module m = null;
421 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
423 generate.invoke(AugmentToGenType.class, args);
425 } catch (final Exception e) {
427 assertTrue(e instanceof InvocationTargetException);
428 final Throwable cause = e.getCause();
429 assertNotNull(cause);
430 assertTrue(cause instanceof IllegalStateException);
431 assertEquals("Augmentation List cannot be empty.", cause.getMessage());
435 @SuppressWarnings({ "rawtypes" })
437 public void augmentationToGenTypesNullAugSchemaTargetNodeTest() throws Exception {
438 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
439 boolean.class, Map.class, Map.class, TypeProvider.class };
440 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
441 assertNotNull(generate);
442 generate.setAccessible(true);
444 final String augmPackName = "pckg.name";
446 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
447 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
448 final SchemaPath path = SchemaPath.create(true, qnamePath);
449 when(augmSchema.getTargetPath()).thenReturn(path);
450 final Set<UsesNode> uses = new HashSet<>();
451 when(augmSchema.getUses()).thenReturn(uses);
453 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
454 augmentationSchemaList.add(augmSchema);
455 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
456 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
457 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
459 final SchemaContext context = mock(SchemaContext.class);
460 final Module moduleAug = mock(Module.class);
461 final DataSchemaNode schNode = null;
462 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
463 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
464 .thenReturn(moduleAug);
466 final TypeProvider typeProvider = null;
467 final Map<Module, ModuleContext> genCtx = new HashMap<>();
468 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
470 final Module m = mock(Module.class);
471 when(m.getName()).thenReturn("augm-module");
472 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
473 when(m.getRevision()).thenReturn(qnamePath.getRevision());
475 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
477 generate.invoke(AugmentToGenType.class, args);
479 } catch (final Exception e) {
481 assertTrue(e instanceof InvocationTargetException);
482 final Throwable cause = e.getCause();
483 assertNotNull(cause);
484 assertTrue(cause instanceof IllegalArgumentException);
485 assertEquals("augment target not found: " + path, cause.getMessage());
489 @SuppressWarnings({ "rawtypes" })
491 public void augmentationToGenTypesNullAugTargetGTBTest() throws Exception {
492 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
493 boolean.class, Map.class, Map.class, TypeProvider.class };
494 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
495 assertNotNull(generate);
496 generate.setAccessible(true);
498 final String augmPackName = "pckg.name";
500 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
501 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
502 final SchemaPath path = SchemaPath.create(true, qnamePath);
503 when(augmSchema.getTargetPath()).thenReturn(path);
504 final Set<UsesNode> uses = new HashSet<>();
505 when(augmSchema.getUses()).thenReturn(uses);
507 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
508 augmentationSchemaList.add(augmSchema);
509 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
510 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
511 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
513 final SchemaContext context = mock(SchemaContext.class);
514 final Module moduleAug = mock(Module.class);
515 final DataSchemaNode schNode = mock(DataSchemaNode.class);
516 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(schNode);
517 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
518 .thenReturn(moduleAug);
520 final TypeProvider typeProvider = null;
521 final Map<Module, ModuleContext> genCtx = new HashMap<>();
522 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
524 final Module m = mock(Module.class);
525 when(m.getName()).thenReturn("augm-module");
526 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
527 when(m.getRevision()).thenReturn(qnamePath.getRevision());
529 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
531 generate.invoke(AugmentToGenType.class, args);
533 } catch (final Exception e) {
535 assertTrue(e instanceof InvocationTargetException);
536 final Throwable cause = e.getCause();
537 assertNotNull(cause);
538 assertTrue(cause instanceof NullPointerException);
539 assertEquals("Target type not yet generated: " + schNode, cause.getMessage());
543 @SuppressWarnings({ "rawtypes", "unchecked" })
545 public void augmentationToGenTypesAugUsesNullOrigTargetTest() throws Exception {
546 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
547 boolean.class, Map.class, Map.class, TypeProvider.class };
548 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
549 assertNotNull(generate);
550 generate.setAccessible(true);
552 final String augmPackName = "pckg.name";
554 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
555 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
556 final SchemaPath path = SchemaPath.create(true, qnamePath);
557 when(augmSchema.getTargetPath()).thenReturn(path);
558 final Set<UsesNode> uses = new HashSet<>();
559 when(augmSchema.getUses()).thenReturn(uses);
561 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
562 augmentationSchemaList.add(augmSchema);
563 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
564 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
565 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
567 final SchemaContext context = mock(SchemaContext.class);
568 final Module moduleAug = mock(Module.class);
569 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
570 when(targetSchNode.getPath()).thenReturn(path);
571 when(targetSchNode.isAddedByUses()).thenReturn(true);
572 final Optional optionalSchemaNode = Optional.absent();
573 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
574 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
575 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
576 .thenReturn(moduleAug);
578 final TypeProvider typeProvider = null;
579 final Map<Module, ModuleContext> genCtx = new HashMap<>();
580 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
582 final Module m = mock(Module.class);
583 when(m.getName()).thenReturn("augm-module");
584 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
585 when(m.getRevision()).thenReturn(qnamePath.getRevision());
587 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
589 generate.invoke(AugmentToGenType.class, args);
591 } catch (final Exception e) {
593 assertTrue(e instanceof InvocationTargetException);
594 final Throwable cause = e.getCause();
595 assertNotNull(cause);
596 assertTrue(cause instanceof IllegalStateException);
597 assertEquals("Failed to find target node from grouping in augmentation " + augmSchema + " in module "
598 + m.getName(), cause.getMessage());
602 @SuppressWarnings({ "rawtypes" })
604 public void augmentationToGenTypesTargetChoicSchemaNodeTest() throws Exception {
605 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
606 boolean.class, Map.class, Map.class, TypeProvider.class };
607 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
608 assertNotNull(generate);
609 generate.setAccessible(true);
611 final String augmPackName = "pckg.name";
613 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
614 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
615 final SchemaPath path = SchemaPath.create(true, qnamePath);
616 when(augmSchema.getTargetPath()).thenReturn(path);
617 final Set<UsesNode> uses = new HashSet<>();
618 when(augmSchema.getUses()).thenReturn(uses);
620 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
621 augmentationSchemaList.add(augmSchema);
622 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
623 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
624 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
626 final SchemaContext context = mock(SchemaContext.class);
627 final Module moduleAug = mock(Module.class);
628 final ChoiceSchemaNode targetSchNode = mock(ChoiceSchemaNode.class);
629 when(targetSchNode.getPath()).thenReturn(path);
630 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
631 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
632 .thenReturn(moduleAug);
634 final TypeProvider typeProvider = null;
636 final Map genCtx = mock(Map.class);
637 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
638 final ModuleContext mc = new ModuleContext();
639 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
640 mc.addChildNodeType(targetSchNode, gtb);
641 moduleContexts.add(mc);
642 when(genCtx.values()).thenReturn(moduleContexts);
644 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
646 final Module m = mock(Module.class);
647 when(m.getName()).thenReturn("augm-module");
648 when(m.getNamespace()).thenReturn(qnamePath.getNamespace());
649 when(m.getRevision()).thenReturn(qnamePath.getRevision());
651 final Object[] args = { augmPackName, schemaPathAugmentListEntry, m, context, false, genCtx, genTypeBuilders, typeProvider };
652 final Map result = (Map) generate.invoke(AugmentToGenType.class, args);
653 assertNotNull(result);
656 @SuppressWarnings({ "rawtypes", "unchecked" })
658 public void augmentationToGenTypesTest() throws Exception {
659 final Class[] parameterTypes = { String.class, Map.Entry.class, Module.class, SchemaContext.class,
660 boolean.class, Map.class, Map.class, TypeProvider.class };
661 final Method generate = AugmentToGenType.class.getDeclaredMethod("augmentationToGenTypes", parameterTypes);
662 assertNotNull(generate);
663 generate.setAccessible(true);
665 final String augmPackName = "pckg.name";
667 final AugmentationSchema augmSchema = mock(AugmentationSchema.class);
668 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
669 final SchemaPath path = SchemaPath.create(true, qnamePath);
670 when(augmSchema.getTargetPath()).thenReturn(path);
671 final Set<UsesNode> uses = new HashSet<>();
672 when(augmSchema.getUses()).thenReturn(uses);
673 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
674 when(augmSchema.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
676 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
677 augmentationSchemaList.add(augmSchema);
678 final Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry = mock(Map.Entry.class);
679 when(schemaPathAugmentListEntry.getKey()).thenReturn(path);
680 when(schemaPathAugmentListEntry.getValue()).thenReturn(augmentationSchemaList);
682 final SchemaContext context = mock(SchemaContext.class);
683 final Module moduleAug = mock(Module.class);
684 final DerivableSchemaNode targetSchNode = mock(DerivableSchemaNode.class);
685 when(targetSchNode.getPath()).thenReturn(path);
686 when(targetSchNode.isAddedByUses()).thenReturn(true);
687 final DataSchemaNode origSchNode = mock(DataSchemaNode.class);
688 when(origSchNode.getPath()).thenReturn(path);
689 when(origSchNode.isAddedByUses()).thenReturn(true);
690 final Optional optionalSchemaNode = Optional.of(origSchNode);
691 when(targetSchNode.getOriginal()).thenReturn(optionalSchemaNode);
692 when(moduleAug.getDataChildByName(qnamePath)).thenReturn(targetSchNode);
693 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
694 .thenReturn(moduleAug);
696 final TypeProvider typeProvider = null;
698 final Map<Module, ModuleContext> genCtx = new HashMap<>();
700 final Collection<ModuleContext> moduleContexts = new ArrayList<>();
701 final ModuleContext mc = new ModuleContext();
702 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(augmPackName, "augm");
703 mc.addChildNodeType(targetSchNode, gtb);
704 moduleContexts.add(mc);
705 genCtx.put(moduleAug, mc);
707 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
709 when(moduleAug.getName()).thenReturn("augm-module");
710 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
711 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
713 final Object[] args =
714 { augmPackName, schemaPathAugmentListEntry, moduleAug, context, false, genCtx, genTypeBuilders, typeProvider };
715 final Map<Module, ModuleContext> result =
716 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
717 assertNotNull(result);
718 final ModuleContext moduleContext = result.get(moduleAug);
719 assertTrue(moduleContext.getAugmentations().get(0).getName().contains("Augm"));
720 assertEquals("pckg.name.data.aug", moduleContext.getAugmentations().get(0).getPackageName());
721 assertTrue(moduleContext.getChildNode(path).getName().contains("Augm"));
722 assertEquals("pckg.name", moduleContext.getChildNode(path).getPackageName());
726 public void usesAugmentationToGenTypesNullPckgNameTest() throws Exception {
728 AugmentToGenType.usesAugmentationToGenTypes(null, null, null, null, null, null, null, null, false, null, null);
729 } catch (final Exception e) {
731 assertTrue(e instanceof IllegalArgumentException);
732 assertEquals(e.getMessage(), "Package Name cannot be NULL.");
737 public void usesAugmentationToGenTypesNullAugSchemaListEntryTest() throws Exception {
739 AugmentToGenType.usesAugmentationToGenTypes(null, "", null, null, null, null, null, null, false, null, null);
740 } catch (final Exception e) {
742 assertTrue(e instanceof IllegalArgumentException);
743 assertEquals(e.getMessage(), "Augmentation Schema List Entry cannot be NULL.");
748 public void usesAugmentationToGenTypesEmptyAugSchemaListTest() throws Exception {
749 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
751 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
753 } catch (final Exception e) {
755 assertTrue(e instanceof IllegalStateException);
756 assertEquals(e.getMessage(), "Augmentation Schema List cannot be empty");
761 public void usesAugmentationToGenTypesNullAugSchemaNodeTargetPathTest() throws Exception {
762 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
763 when(augmentationSchema.getTargetPath()).thenReturn(null);
764 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
765 augmentationSchemaList.add(augmentationSchema);
767 AugmentToGenType.usesAugmentationToGenTypes(null, "", augmentationSchemaList, null, null, null, null, null,
769 } catch (final Exception e) {
771 assertTrue(e instanceof IllegalStateException);
772 assertEquals(e.getMessage(), "Augmentation Schema does not contain Target Path (Target Path is NULL).");
777 public void usesAugmentationToGenTypesNullAugmentTargetTest() throws Exception {
778 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
779 final SchemaPath path = SchemaPath.create(true, qnamePath);
780 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
781 when(augmentationSchema.getTargetPath()).thenReturn(path);
782 final Set<UsesNode> uses = new HashSet<>();
783 when(augmentationSchema.getUses()).thenReturn(uses);
785 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
786 augmentationSchemaList.add(augmentationSchema);
788 final SchemaContext context = mock(SchemaContext.class);
789 final Module moduleAug = mock(Module.class);
790 when(moduleAug.getName()).thenReturn("augm-module");
791 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
792 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
793 final Set<GroupingDefinition> groupings = new HashSet<>();
794 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
795 when(groupingDefinition.getQName()).thenReturn(qnamePath);
796 groupings.add(groupingDefinition);
797 when(moduleAug.getGroupings()).thenReturn(groupings);
799 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
800 .thenReturn(moduleAug);
802 final Map<Module, ModuleContext> genCtx = new HashMap<>();
803 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
806 final UsesNode usesNode = mock(UsesNode.class);
807 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
809 when(usesNode.getGroupingPath()).thenReturn(path);
812 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
813 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
814 } catch (final Exception e) {
816 assertTrue(e instanceof IllegalArgumentException);
817 assertEquals(e.getMessage(), "augment target not found: " + path);
822 public void usesAugmentationToGenTypesNullTargetGTBTest() throws Exception {
823 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
824 final SchemaPath path = SchemaPath.create(true, qnamePath);
825 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
826 when(augmentationSchema.getTargetPath()).thenReturn(path);
827 final Set<UsesNode> uses = new HashSet<>();
828 when(augmentationSchema.getUses()).thenReturn(uses);
830 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
831 augmentationSchemaList.add(augmentationSchema);
833 final SchemaContext context = mock(SchemaContext.class);
834 final Module moduleAug = mock(Module.class);
835 when(moduleAug.getName()).thenReturn("augm-module");
836 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
837 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
838 final Set<GroupingDefinition> groupings = new HashSet<>();
839 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
840 when(groupingDefinition.getQName()).thenReturn(qnamePath);
841 final DataSchemaNode schNode = mock(DataSchemaNode.class);
842 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
843 groupings.add(groupingDefinition);
844 when(moduleAug.getGroupings()).thenReturn(groupings);
846 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
847 .thenReturn(moduleAug);
849 final Map<Module, ModuleContext> genCtx = new HashMap<>();
850 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
852 final UsesNode usesNode = mock(UsesNode.class);
853 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
855 when(usesNode.getGroupingPath()).thenReturn(path);
858 AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm", augmentationSchemaList, moduleAug,
859 usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null, null);
860 } catch (final Exception e) {
862 assertTrue(e instanceof NullPointerException);
863 assertEquals(e.getMessage(), "Target type not yet generated: " + schNode);
868 public void usesAugmentationToGenTypesTest() throws Exception {
869 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
870 final SchemaPath path = SchemaPath.create(true, qnamePath);
871 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
872 when(augmentationSchema.getTargetPath()).thenReturn(path);
873 final Set<UsesNode> uses = new HashSet<>();
874 when(augmentationSchema.getUses()).thenReturn(uses);
876 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
877 augmentationSchemaList.add(augmentationSchema);
879 final SchemaContext context = mock(SchemaContext.class);
880 final Module moduleAug = mock(Module.class);
881 when(moduleAug.getName()).thenReturn("augm-module");
882 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
883 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
884 final Set<GroupingDefinition> groupings = new HashSet<>();
885 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
886 when(groupingDefinition.getQName()).thenReturn(qnamePath);
887 final DataSchemaNode schNode = mock(DataSchemaNode.class);
888 when(schNode.getPath()).thenReturn(path);
889 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
890 groupings.add(groupingDefinition);
891 when(moduleAug.getGroupings()).thenReturn(groupings);
893 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
894 .thenReturn(moduleAug);
896 final Map<Module, ModuleContext> genCtx = new HashMap<>();
897 final ModuleContext mc = new ModuleContext();
898 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
899 mc.addChildNodeType(schNode, gtb);
900 genCtx.put(moduleAug, mc);
902 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
904 final UsesNode usesNode = mock(UsesNode.class);
905 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
907 when(usesNode.getGroupingPath()).thenReturn(path);
909 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
910 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
911 BindingNamespaceType.Data);
912 assertNotNull(result);
916 public void usesAugmentationToGenTypesChoiceTest() throws Exception {
917 final QName qnamePath = QName.create("test", "2017-04-04", "aug");
918 final SchemaPath path = SchemaPath.create(true, qnamePath);
919 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
920 when(augmentationSchema.getTargetPath()).thenReturn(path);
921 final Set<UsesNode> uses = new HashSet<>();
922 when(augmentationSchema.getUses()).thenReturn(uses);
924 final List<AugmentationSchema> augmentationSchemaList = new ArrayList<>();
925 augmentationSchemaList.add(augmentationSchema);
927 final SchemaContext context = mock(SchemaContext.class);
928 final Module moduleAug = mock(Module.class);
929 when(moduleAug.getName()).thenReturn("augm-module");
930 when(moduleAug.getNamespace()).thenReturn(qnamePath.getNamespace());
931 when(moduleAug.getRevision()).thenReturn(qnamePath.getRevision());
932 final Set<GroupingDefinition> groupings = new HashSet<>();
933 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
934 when(groupingDefinition.getQName()).thenReturn(qnamePath);
935 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
936 when(schNode.getPath()).thenReturn(path);
937 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
938 groupings.add(groupingDefinition);
939 when(moduleAug.getGroupings()).thenReturn(groupings);
941 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
942 .thenReturn(moduleAug);
944 final Map<Module, ModuleContext> genCtx = new HashMap<>();
945 final ModuleContext mc = new ModuleContext();
946 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl("pckg.test.augm", "GtbAugm");
947 mc.addChildNodeType(schNode, gtb);
948 genCtx.put(moduleAug, mc);
950 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
952 final UsesNode usesNode = mock(UsesNode.class);
953 final DataNodeContainer usesNodeParent = mock(DataNodeContainer.class);
955 when(usesNode.getGroupingPath()).thenReturn(path);
957 final Map<Module, ModuleContext> result = AugmentToGenType.usesAugmentationToGenTypes(context, "pckg.test.augm",
958 augmentationSchemaList, moduleAug, usesNode, usesNodeParent, genCtx, genTypeBuilders, false, null,
959 BindingNamespaceType.Data);
960 assertNotNull(result);
963 @SuppressWarnings({ "rawtypes" })
965 public void findOriginalTargetFromGroupingNonGroupingTest() throws Exception {
966 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
967 final Method generate =
968 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
969 assertNotNull(generate);
970 generate.setAccessible(true);
972 final Module module = mock(Module.class);
973 final QName qnamePath = QName.create("test", "2017-04-04", "test");
974 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
975 final DataSchemaNode schNode = mock(DataSchemaNode.class);
976 when(schNode.getPath()).thenReturn(schemaPath);
977 when(module.getDataChildByName(qnamePath)).thenReturn(schNode);
979 final SchemaContext context = mock(SchemaContext.class);
980 when(context.findModuleByNamespaceAndRevision(qnamePath .getNamespace(), qnamePath.getRevision()))
982 final UsesNode usesNode = mock(UsesNode.class);
983 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
985 final Object[] args = { context, schemaPath, usesNode };
987 generate.invoke(AugmentToGenType.class, args);
989 } catch (final Exception e) {
991 assertTrue(e instanceof InvocationTargetException);
992 final Throwable cause = e.getCause();
993 assertNotNull(cause);
994 assertTrue(cause instanceof IllegalArgumentException);
995 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
999 @SuppressWarnings({ "rawtypes" })
1001 public void findOriginalTargetFromGroupingAsUsesFailedTest() throws Exception {
1002 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1003 final Method generate =
1004 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1005 assertNotNull(generate);
1006 generate.setAccessible(true);
1008 final Module module = mock(Module.class);
1009 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1010 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1011 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1012 when(schNode.getPath()).thenReturn(schemaPath);
1013 when(schNode.isAddedByUses()).thenReturn(true);
1014 final Set<GroupingDefinition> groupings = new HashSet<>();
1015 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1016 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1017 groupings.add(groupingDefinition);
1018 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1019 when(module.getGroupings()).thenReturn(groupings);
1021 final SchemaContext context = mock(SchemaContext.class);
1022 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1023 .thenReturn(module);
1024 final UsesNode usesNode = mock(UsesNode.class);
1025 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1027 final Object[] args = { context, schemaPath, usesNode };
1029 generate.invoke(AugmentToGenType.class, args);
1031 } catch (final Exception e) {
1033 assertTrue(e instanceof InvocationTargetException);
1034 final Throwable cause = e.getCause();
1035 assertNotNull(cause);
1036 assertTrue(cause instanceof IllegalStateException);
1037 assertEquals("Failed to generate code for augment in " + usesNode, cause.getMessage());
1041 @SuppressWarnings({ "rawtypes" })
1043 public void findOriginalTargetFromGroupingReturnNullTest() throws Exception {
1044 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1045 final Method generate =
1046 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1047 assertNotNull(generate);
1048 generate.setAccessible(true);
1050 final Module module = mock(Module.class);
1051 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1052 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1053 final DataSchemaNode schNode = null;
1054 final Set<GroupingDefinition> groupings = new HashSet<>();
1055 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1056 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1057 groupings.add(groupingDefinition);
1058 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1059 when(module.getGroupings()).thenReturn(groupings);
1061 final SchemaContext context = mock(SchemaContext.class);
1062 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1063 .thenReturn(module);
1064 final UsesNode usesNode = mock(UsesNode.class);
1065 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1067 final Object[] args = { context, schemaPath, usesNode };
1068 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1069 assertEquals(null, result);
1072 @SuppressWarnings({ "rawtypes", "unchecked" })
1074 public void findOriginalTargetFromGroupingTest() throws Exception {
1075 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1076 final Method generate =
1077 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1078 assertNotNull(generate);
1079 generate.setAccessible(true);
1081 final Module module = mock(Module.class);
1082 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1083 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1084 final DataSchemaNode schNode = mock(DataSchemaNode.class);
1085 when(schNode.getPath()).thenReturn(schemaPath);
1086 final Set<GroupingDefinition> groupings = new HashSet<>();
1087 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1088 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1089 groupings.add(groupingDefinition);
1090 final DerivableSchemaNode derivSchNode = mock(DerivableSchemaNode.class);
1091 when(derivSchNode.isAddedByUses()).thenReturn(true);
1092 final Optional optional = Optional.of(schNode);
1093 when(derivSchNode.getOriginal()).thenReturn(optional);
1094 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(derivSchNode);
1095 when(module.getGroupings()).thenReturn(groupings);
1097 final SchemaContext context = mock(SchemaContext.class);
1098 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1099 .thenReturn(module);
1100 final UsesNode usesNode = mock(UsesNode.class);
1101 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1103 final Object[] args = { context, schemaPath, usesNode };
1104 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1105 assertEquals(schNode, result);
1108 @SuppressWarnings({ "rawtypes" })
1110 public void findOriginalTargetFromGroupingChoiceTest() throws Exception {
1111 final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
1112 final Method generate =
1113 AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
1114 assertNotNull(generate);
1115 generate.setAccessible(true);
1117 final Module module = mock(Module.class);
1118 final QName qnamePath = QName.create("test", "2017-04-04", "test");
1119 final SchemaPath schemaPath = SchemaPath.create(true, qnamePath);
1120 final ChoiceSchemaNode schNode = mock(ChoiceSchemaNode.class);
1121 when(schNode.getPath()).thenReturn(schemaPath);
1122 final Set<GroupingDefinition> groupings = new HashSet<>();
1123 final GroupingDefinition groupingDefinition = mock(GroupingDefinition.class);
1124 when(groupingDefinition.getQName()).thenReturn(qnamePath);
1125 groupings.add(groupingDefinition);
1126 when(groupingDefinition.getDataChildByName(qnamePath)).thenReturn(schNode);
1127 when(module.getGroupings()).thenReturn(groupings);
1129 final SchemaContext context = mock(SchemaContext.class);
1130 when(context.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1131 .thenReturn(module);
1132 final UsesNode usesNode = mock(UsesNode.class);
1133 when(usesNode.getGroupingPath()).thenReturn(schemaPath);
1135 final Object[] args = { context, schemaPath, usesNode };
1136 final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
1137 assertEquals(schNode, result);
1140 @SuppressWarnings({ "rawtypes" })
1142 public void generateTypesFromAugmentedChoiceCasesNullPckgNameTest() throws Exception {
1143 final Class[] parameterTypes =
1144 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1145 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1146 BindingNamespaceType.class};
1147 final Method generate =
1148 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1149 assertNotNull(generate);
1150 generate.setAccessible(true);
1152 final SchemaContext schemaContext = null;
1153 final Module module = null;
1154 final String pckgName = null;
1155 final Type targetType = null;
1156 final ChoiceSchemaNode targetNode = null;
1157 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1158 final DataNodeContainer usesNodeParent = null;
1159 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1160 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1162 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1163 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1165 generate.invoke(AugmentToGenType.class, args);
1167 } catch (final Exception e) {
1169 assertTrue(e instanceof InvocationTargetException);
1170 final Throwable cause = e.getCause();
1171 assertNotNull(cause);
1172 assertTrue(cause instanceof IllegalArgumentException);
1173 assertEquals("Base Package Name cannot be NULL.", cause.getMessage());
1177 @SuppressWarnings({ "rawtypes" })
1179 public void generateTypesFromAugmentedChoiceCasesNullTargetType() throws Exception {
1180 final Class[] parameterTypes =
1181 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1182 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1183 BindingNamespaceType.class };
1184 final Method generate =
1185 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1186 assertNotNull(generate);
1187 generate.setAccessible(true);
1189 final SchemaContext schemaContext = null;
1190 final Module module = null;
1191 final String pckgName = "";
1192 final Type targetType = null;
1193 final ChoiceSchemaNode targetNode = null;
1194 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1195 final DataNodeContainer usesNodeParent = null;
1196 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1197 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1199 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry,
1200 usesNodeParent, genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1202 generate.invoke(AugmentToGenType.class, args);
1204 } catch (final Exception e) {
1206 assertTrue(e instanceof InvocationTargetException);
1207 final Throwable cause = e.getCause();
1208 assertNotNull(cause);
1209 assertTrue(cause instanceof IllegalArgumentException);
1210 assertEquals("Referenced Choice Type cannot be NULL.", cause.getMessage());
1214 @SuppressWarnings({ "rawtypes" })
1216 public void generateTypesFromAugmentedChoiceCasesNullAugmentNodes() throws Exception {
1217 final Class[] parameterTypes =
1218 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1219 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1220 BindingNamespaceType.class };
1221 final Method generate =
1222 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1223 assertNotNull(generate);
1224 generate.setAccessible(true);
1226 final SchemaContext schemaContext = null;
1227 final Module module = null;
1228 final String pckgName = "";
1229 final Type targetType = mock(Type.class);
1230 final ChoiceSchemaNode targetNode = null;
1231 final List<AugmentationSchema> schemaPathAugmentListEntry = null;
1232 final DataNodeContainer usesNodeParent = null;
1233 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1234 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1236 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1237 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1239 generate.invoke(AugmentToGenType.class, args);
1241 } catch (final Exception e) {
1243 assertTrue(e instanceof InvocationTargetException);
1244 final Throwable cause = e.getCause();
1245 assertNotNull(cause);
1246 assertTrue(cause instanceof IllegalArgumentException);
1247 assertEquals("Set of Choice Case Nodes cannot be NULL.", cause.getMessage());
1251 @SuppressWarnings({ "rawtypes", "unchecked" })
1253 public void generateTypesFromAugmentedChoiceCasesNullCaseNodeTest() throws Exception {
1254 final Class[] parameterTypes =
1255 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1256 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1257 BindingNamespaceType.class };
1258 final Method generate =
1259 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1260 assertNotNull(generate);
1261 generate.setAccessible(true);
1263 final SchemaContext schemaContext = null;
1264 final Module module = null;
1265 final String pckgName = "";
1266 final Type targetType = mock(Type.class);
1267 final ChoiceSchemaNode targetNode = null;
1268 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1269 final DataSchemaNode caseNode = null;
1270 augmentNodes.add(caseNode);
1272 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1273 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1274 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1275 schemaPathAugmentListEntry.add(augmentationSchema);
1277 final DataNodeContainer usesNodeParent = null;
1278 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1279 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1281 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1282 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1283 final Map<Module, ModuleContext> result =
1284 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1285 assertEquals(genCtx, result);
1288 @SuppressWarnings({ "rawtypes" })
1290 public void generateTypesFromAugmentedChoiceCasesNullChildTest() throws Exception {
1291 final Class[] parameterTypes =
1292 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1293 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1294 BindingNamespaceType.class };
1295 final Method generate =
1296 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1297 assertNotNull(generate);
1298 generate.setAccessible(true);
1300 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1301 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1302 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1304 final SchemaContext schemaContext = mock(SchemaContext.class);
1305 final Module module = mock(Module.class);
1306 when(module.getName()).thenReturn("test-module-case");
1307 final DataSchemaNode schemaNode = mock(DataSchemaNode.class);
1308 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1309 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1310 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1311 final String pckgName = "test.augment.choice.cases";
1312 final Type targetType = mock(Type.class);
1313 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1314 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1315 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1316 when(caseNode.getPath()).thenReturn(path);
1317 when(caseNode.getQName()).thenReturn(qnamePath);
1318 augmentNodes.add(caseNode);
1320 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1321 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1322 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1323 schemaPathAugmentListEntry.add(augmentationSchema);
1325 final DataNodeContainer usesNodeParent = null;
1326 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1327 when(targetNode.getPath()).thenReturn(path);
1328 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1329 final ModuleContext moduleContext = new ModuleContext();
1330 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1331 moduleContext.addCaseType(path, gtb);
1332 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1334 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1335 .thenReturn(module);
1337 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1338 genCtx, false, genTypeBuilder, null, BindingNamespaceType.Data };
1340 generate.invoke(AugmentToGenType.class, args);
1342 } catch (final Exception e) {
1344 assertTrue(e instanceof InvocationTargetException);
1345 final Throwable cause = e.getCause();
1346 assertNotNull(cause);
1347 assertTrue(cause instanceof IllegalArgumentException);
1348 assertEquals("Failed to find parent type of choice " + targetNode, cause.getMessage());
1352 @SuppressWarnings({ "rawtypes", "unchecked" })
1354 public void generateTypesFromAugmentedChoiceCasesTest() throws Exception {
1355 final Class[] parameterTypes =
1356 { SchemaContext.class, Module.class, String.class, Type.class, ChoiceSchemaNode.class, List.class,
1357 DataNodeContainer.class, Map.class, boolean.class, Map.class, TypeProvider.class,
1358 BindingNamespaceType.class };
1359 final Method generate =
1360 AugmentToGenType.class.getDeclaredMethod("generateTypesFromAugmentedChoiceCases", parameterTypes);
1361 assertNotNull(generate);
1362 generate.setAccessible(true);
1364 final QName qnamePath = QName.create("test", "2017-04-04", "chcase");
1365 final QName qnamePath2 = QName.create("test", "2017-04-04", "chcase2");
1366 final SchemaPath path = SchemaPath.create(true, qnamePath, qnamePath2);
1368 final SchemaContext schemaContext = mock(SchemaContext.class);
1369 final Module module = mock(Module.class);
1370 when(module.getName()).thenReturn("test-module-case");
1371 final ChoiceCaseNode schemaNode = mock(ChoiceCaseNode.class);
1372 when(schemaNode.getPath()).thenReturn(path);
1373 when(module.getDataChildByName(qnamePath)).thenReturn(schemaNode);
1374 when(module.getRevision()).thenReturn(qnamePath.getRevision());
1375 when(module.getNamespace()).thenReturn(qnamePath.getNamespace());
1376 final String pckgName = "test.augment.choice.cases";
1377 final Type targetType = mock(Type.class);
1378 when(targetType.getFullyQualifiedName()).thenReturn(Augmentable.class.getName());
1379 final Set<DataSchemaNode> augmentNodes = new HashSet<>();
1380 final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
1381 when(caseNode.getPath()).thenReturn(path);
1382 when(caseNode.getQName()).thenReturn(qnamePath);
1383 augmentNodes.add(caseNode);
1385 final AugmentationSchema augmentationSchema = mock(AugmentationSchema.class);
1386 when(augmentationSchema.getChildNodes()).thenReturn(augmentNodes);
1387 final List<AugmentationSchema> schemaPathAugmentListEntry = new ArrayList<>();
1388 schemaPathAugmentListEntry.add(augmentationSchema);
1390 final DataNodeContainer usesNodeParent = null;
1391 final ChoiceSchemaNode targetNode = mock(ChoiceSchemaNode.class);
1392 when(targetNode.getPath()).thenReturn(path);
1393 final Map<Module, ModuleContext> genCtx = new HashMap<>();
1394 final ModuleContext moduleContext = new ModuleContext();
1395 final GeneratedTypeBuilder gtb = new GeneratedTypeBuilderImpl(pckgName, "test-case-node-augment");
1396 moduleContext.addCaseType(path, gtb);
1397 genCtx.put(module, moduleContext);
1398 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilder = new HashMap<>();
1400 when(schemaContext.findModuleByNamespaceAndRevision(qnamePath.getNamespace(), qnamePath.getRevision()))
1401 .thenReturn(module);
1403 final Object[] args = { schemaContext, module, pckgName, targetType, targetNode, schemaPathAugmentListEntry, usesNodeParent,
1404 genCtx, false, genTypeBuilder, null ,BindingNamespaceType.Data };
1405 final Map<Module, ModuleContext> result =
1406 (Map<Module, ModuleContext>) generate.invoke(AugmentToGenType.class, args);
1407 assertNotNull(result);
1408 assertEquals(result.get(module), moduleContext);