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