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