2 * Copyright (c) 2016 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.java.api.generator;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.reset;
17 import static org.opendaylight.mdsal.binding.generator.util.TypeConstants.PATTERN_CONSTANT_NAME;
18 import static org.opendaylight.mdsal.binding.java.api.generator.GeneratorUtil.createImports;
20 import com.google.common.collect.ImmutableList;
21 import java.lang.reflect.Constructor;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorUtil;
26 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
27 import org.opendaylight.mdsal.binding.model.api.Constant;
28 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
29 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
30 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
31 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
32 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
33 import org.opendaylight.mdsal.binding.model.api.Type;
35 public class GeneratorUtilTest {
37 private final GeneratedType generatedType = mock(GeneratedType.class);
38 private final GeneratedTransferObject enclosedType = mock(GeneratedTransferObject.class);
39 private final MethodSignature methodSignature = mock(MethodSignature.class);
40 private final Type type = mock(Type.class);
41 private final AnnotationType annotationType = mock(AnnotationType.class);
42 private final MethodSignature.Parameter parameter = mock(MethodSignature.Parameter.class);
43 private final GeneratedProperty property = mock(GeneratedProperty.class);
44 private final ParameterizedType parameterizedType = mock(ParameterizedType.class);
50 doReturn("tst.package").when(parameterizedType).getPackageName();
51 doReturn("tstParametrizedType").when(parameterizedType).getName();
52 doReturn("tst.package").when(type).getPackageName();
53 doReturn("tstName").when(type).getName();
54 doReturn(parameterizedType).when(property).getReturnType();
55 doReturn(new Type[] { type }).when(parameterizedType).getActualTypeArguments();
56 doReturn(ImmutableList.of(property)).when(enclosedType).getProperties();
57 doReturn(true).when(property).isReadOnly();
58 doReturn("tst.package").when(enclosedType).getPackageName();
59 doReturn("tstName").when(enclosedType).getName();
60 doReturn(ImmutableList.of(parameter)).when(methodSignature).getParameters();
62 doReturn("tst.package").when(annotationType).getPackageName();
63 doReturn("tstAnnotationName").when(annotationType).getName();
65 doReturn(type).when(parameter).getType();
66 doReturn(type).when(methodSignature).getReturnType();
67 doReturn(ImmutableList.of(annotationType)).when(methodSignature).getAnnotations();
68 doReturn(ImmutableList.of(methodSignature)).when(enclosedType).getMethodDefinitions();
70 final Constant constant = mock(Constant.class);
71 doReturn(PATTERN_CONSTANT_NAME).when(constant).getName();
72 doReturn(ImmutableList.of(constant)).when(enclosedType).getConstantDefinitions();
74 doReturn(ImmutableList.of()).when(enclosedType).getEnclosedTypes();
75 doReturn(ImmutableList.of(enclosedType)).when(generatedType).getEnclosedTypes();
79 public void createChildImportsTest() throws Exception {
80 doReturn("tst.package").when(enclosedType).getPackageName();
81 doReturn("tstName").when(enclosedType).getName();
82 doReturn(ImmutableList.of()).when(enclosedType).getEnclosedTypes();
83 doReturn(ImmutableList.of(enclosedType)).when(generatedType).getEnclosedTypes();
84 final Map<String, String> generated = GeneratorUtil.createChildImports(generatedType);
85 assertNotNull(generated);
86 assertTrue(generated.get("tstName").equals("tst.package"));
89 @Test(expected = UnsupportedOperationException.class)
90 public void constructTest() throws Throwable {
91 final Constructor constructor = GeneratorUtil.class.getDeclaredConstructor();
92 constructor.setAccessible(true);
94 constructor.newInstance();
95 } catch (Exception e) {
100 @Test(expected = IllegalArgumentException.class)
101 public void createImportsWithExceptionTest() throws Exception {
102 GeneratorUtil.createImports(null);
106 public void createImportsTest() throws Exception {
107 final Map<String, String> generated = createImports(generatedType);
108 assertNotNull(generated);
109 assertTrue(generated.get("tstAnnotationName").equals("tst.package"));
112 @Test(expected = IllegalArgumentException.class)
113 public void isConstantToExceptionTest() throws Exception {
114 GeneratorUtil.isConstantInTO(null, null);
118 public void isConstantToTest() throws Exception {
119 final Constant constant = mock(Constant.class);
120 doReturn("tst2").when(constant).getName();
121 doReturn(ImmutableList.of(constant)).when(enclosedType).getConstantDefinitions();
122 assertFalse(GeneratorUtil.isConstantInTO("tst", enclosedType));
126 public void getPropertiesOfAllParentsTest() throws Exception {
127 final GeneratedTransferObject superType = mock(GeneratedTransferObject.class);
128 doReturn(enclosedType).when(superType).getSuperType();
129 assertTrue(GeneratorUtil.getPropertiesOfAllParents(superType).contains(property));
133 public void getExplicitTypeTest() throws Exception {
134 assertEquals(annotationType.getName(), GeneratorUtil.getExplicitType(
135 generatedType, annotationType, createImports(generatedType)));
137 assertTrue(GeneratorUtil.getExplicitType(generatedType, parameterizedType,
138 createImports(generatedType)).contains(parameterizedType.getName()));
142 @Test(expected = IllegalArgumentException.class)
143 public void getTopParentTransportObjectWithExceptionTest() throws Exception {
144 GeneratorUtil.getTopParentTransportObject(null);
148 public void getTopParentTransportObjectTest() throws Exception {
149 assertEquals(enclosedType, GeneratorUtil.getTopParentTransportObject(enclosedType));
151 final GeneratedTransferObject parent = mock(GeneratedTransferObject.class);
152 doReturn(parent).when(enclosedType).getSuperType();
153 assertEquals(parent, GeneratorUtil.getTopParentTransportObject(enclosedType));