885d480e1f55d211b0cd33eb4b383a5f4c6f4e57
[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.generator.util.TypeConstants.PATTERN_CONSTANT_NAME;
18 import static org.opendaylight.mdsal.binding.java.api.generator.GeneratorUtil.createImports;
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.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;
34
35 public class GeneratorUtilTest {
36
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);
45
46     @Before
47     public void setUp() {
48         reset(enclosedType);
49
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();
61
62         doReturn("tst.package").when(annotationType).getPackageName();
63         doReturn("tstAnnotationName").when(annotationType).getName();
64
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();
69
70         final Constant constant = mock(Constant.class);
71         doReturn(PATTERN_CONSTANT_NAME).when(constant).getName();
72         doReturn(ImmutableList.of(constant)).when(enclosedType).getConstantDefinitions();
73
74         doReturn(ImmutableList.of()).when(enclosedType).getEnclosedTypes();
75         doReturn(ImmutableList.of(enclosedType)).when(generatedType).getEnclosedTypes();
76     }
77
78     @Test
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"));
87     }
88
89     @Test(expected = UnsupportedOperationException.class)
90     public void constructTest() throws Throwable {
91         final Constructor constructor = GeneratorUtil.class.getDeclaredConstructor();
92         constructor.setAccessible(true);
93         try {
94             constructor.newInstance();
95         } catch (Exception e) {
96             throw e.getCause();
97         }
98     }
99
100     @Test(expected = IllegalArgumentException.class)
101     public void createImportsWithExceptionTest() throws Exception {
102         GeneratorUtil.createImports(null);
103     }
104
105     @Test
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"));
110     }
111
112     @Test(expected = IllegalArgumentException.class)
113     public void isConstantToExceptionTest() throws Exception {
114         GeneratorUtil.isConstantInTO(null, null);
115     }
116
117     @Test
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));
123     }
124
125     @Test
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));
130     }
131
132     @Test
133     public void getExplicitTypeTest() throws Exception {
134         assertEquals(annotationType.getName(), GeneratorUtil.getExplicitType(
135                 generatedType, annotationType, createImports(generatedType)));
136
137         assertTrue(GeneratorUtil.getExplicitType(generatedType, parameterizedType,
138                 createImports(generatedType)).contains(parameterizedType.getName()));
139
140     }
141
142     @Test(expected = IllegalArgumentException.class)
143     public void getTopParentTransportObjectWithExceptionTest() throws Exception {
144         GeneratorUtil.getTopParentTransportObject(null);
145     }
146
147     @Test
148     public void getTopParentTransportObjectTest() throws Exception {
149         assertEquals(enclosedType, GeneratorUtil.getTopParentTransportObject(enclosedType));
150
151         final GeneratedTransferObject parent = mock(GeneratedTransferObject.class);
152         doReturn(parent).when(enclosedType).getSuperType();
153         assertEquals(parent, GeneratorUtil.getTopParentTransportObject(enclosedType));
154     }
155 }