Integrate JavaTypeName as Identifier
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / GeneratorUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator;
9
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.java.api.generator.GeneratorUtil.createImports;
18 import static org.opendaylight.mdsal.binding.model.util.TypeConstants.PATTERN_CONSTANT_NAME;
19
20 import com.google.common.collect.ImmutableList;
21 import java.lang.reflect.Constructor;
22 import java.util.Map;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
26 import org.opendaylight.mdsal.binding.model.api.Constant;
27 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
28 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
29 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
30 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
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;
34
35 public class GeneratorUtilTest {
36     private static final JavaTypeName ANNOTATION = JavaTypeName.create("tst.package", "tstAnnotationName");
37     private static final JavaTypeName PARAMETERIZED_TYPE = JavaTypeName.create("tst.package", "tstParametrizedType");
38     private static final JavaTypeName TYPE = JavaTypeName.create("tst.package", "tstName");
39
40     private final GeneratedType generatedType = mock(GeneratedType.class);
41     private final GeneratedTransferObject enclosedType = mock(GeneratedTransferObject.class);
42     private final MethodSignature methodSignature = mock(MethodSignature.class);
43     private final Type type = mock(Type.class);
44     private final AnnotationType annotationType = mock(AnnotationType.class);
45     private final MethodSignature.Parameter parameter = mock(MethodSignature.Parameter.class);
46     private final GeneratedProperty property = mock(GeneratedProperty.class);
47     private final ParameterizedType parameterizedType = mock(ParameterizedType.class);
48
49     @Before
50     public void setUp() {
51         reset(enclosedType);
52
53         doReturn("tst.package").when(parameterizedType).getPackageName();
54         doReturn("tstParametrizedType").when(parameterizedType).getName();
55         doReturn(PARAMETERIZED_TYPE).when(parameterizedType).getIdentifier();
56         doReturn("tst.package").when(type).getPackageName();
57         doReturn("tstName").when(type).getName();
58         doReturn(TYPE).when(type).getIdentifier();
59         doReturn(parameterizedType).when(property).getReturnType();
60         doReturn(new Type[] { type }).when(parameterizedType).getActualTypeArguments();
61         doReturn(ImmutableList.of(property)).when(enclosedType).getProperties();
62         doReturn(true).when(property).isReadOnly();
63         doReturn("tst.package").when(enclosedType).getPackageName();
64         doReturn("tstName").when(enclosedType).getName();
65         doReturn(TYPE).when(enclosedType).getIdentifier();
66
67         doReturn(ImmutableList.of(parameter)).when(methodSignature).getParameters();
68
69         doReturn("tst.package").when(annotationType).getPackageName();
70         doReturn("tstAnnotationName").when(annotationType).getName();
71         doReturn(ANNOTATION).when(annotationType).getIdentifier();
72
73         doReturn(type).when(parameter).getType();
74         doReturn(type).when(methodSignature).getReturnType();
75         doReturn(ImmutableList.of(annotationType)).when(methodSignature).getAnnotations();
76         doReturn(ImmutableList.of(methodSignature)).when(enclosedType).getMethodDefinitions();
77
78         final Constant constant = mock(Constant.class);
79         doReturn(PATTERN_CONSTANT_NAME).when(constant).getName();
80         doReturn(ImmutableList.of(constant)).when(enclosedType).getConstantDefinitions();
81
82         doReturn(ImmutableList.of()).when(enclosedType).getEnclosedTypes();
83         doReturn(ImmutableList.of(enclosedType)).when(generatedType).getEnclosedTypes();
84     }
85
86     @Test
87     public void createChildImportsTest() throws Exception {
88         doReturn("tst.package").when(enclosedType).getPackageName();
89         doReturn("tstName").when(enclosedType).getName();
90         doReturn(ImmutableList.of()).when(enclosedType).getEnclosedTypes();
91         doReturn(ImmutableList.of(enclosedType)).when(generatedType).getEnclosedTypes();
92         final Map<String, String> generated = GeneratorUtil.createChildImports(generatedType);
93         assertNotNull(generated);
94         assertTrue(generated.get("tstName").equals("tst.package"));
95     }
96
97     @Test(expected = UnsupportedOperationException.class)
98     public void constructTest() throws Throwable {
99         final Constructor<GeneratorUtil> constructor = GeneratorUtil.class.getDeclaredConstructor();
100         constructor.setAccessible(true);
101         try {
102             constructor.newInstance();
103         } catch (Exception e) {
104             throw e.getCause();
105         }
106     }
107
108     @Test(expected = IllegalArgumentException.class)
109     public void createImportsWithExceptionTest() throws Exception {
110         GeneratorUtil.createImports(null);
111     }
112
113     @Test
114     public void createImportsTest() throws Exception {
115         final Map<String, JavaTypeName> generated = createImports(generatedType);
116         assertNotNull(generated);
117         assertEquals(JavaTypeName.create("tst.package", "tstAnnotationName"), generated.get("tstAnnotationName"));
118     }
119
120     @Test(expected = IllegalArgumentException.class)
121     public void isConstantToExceptionTest() throws Exception {
122         GeneratorUtil.isConstantInTO(null, null);
123     }
124
125     @Test
126     public void isConstantToTest() throws Exception {
127         final Constant constant = mock(Constant.class);
128         doReturn("tst2").when(constant).getName();
129         doReturn(ImmutableList.of(constant)).when(enclosedType).getConstantDefinitions();
130         assertFalse(GeneratorUtil.isConstantInTO("tst", enclosedType));
131     }
132
133     @Test
134     public void getPropertiesOfAllParentsTest() throws Exception {
135         final GeneratedTransferObject superType = mock(GeneratedTransferObject.class);
136         doReturn(enclosedType).when(superType).getSuperType();
137         assertTrue(GeneratorUtil.getPropertiesOfAllParents(superType).contains(property));
138     }
139
140     @Test
141     public void getExplicitTypeTest() throws Exception {
142         assertEquals(annotationType.getName(), GeneratorUtil.getExplicitType(
143                 generatedType, annotationType, createImports(generatedType)));
144
145         assertTrue(GeneratorUtil.getExplicitType(generatedType, parameterizedType,
146                 createImports(generatedType)).contains(parameterizedType.getName()));
147
148     }
149
150     @Test(expected = IllegalArgumentException.class)
151     public void getTopParentTransportObjectWithExceptionTest() throws Exception {
152         GeneratorUtil.getTopParentTransportObject(null);
153     }
154
155     @Test
156     public void getTopParentTransportObjectTest() throws Exception {
157         assertEquals(enclosedType, GeneratorUtil.getTopParentTransportObject(enclosedType));
158
159         final GeneratedTransferObject parent = mock(GeneratedTransferObject.class);
160         doReturn(parent).when(enclosedType).getSuperType();
161         assertEquals(parent, GeneratorUtil.getTopParentTransportObject(enclosedType));
162     }
163 }