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.java.api.generator.GeneratorUtil.createImports;
18 import static org.opendaylight.mdsal.binding.model.util.TypeConstants.PATTERN_CONSTANT_NAME;
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.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;
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");
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);
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();
67 doReturn(ImmutableList.of(parameter)).when(methodSignature).getParameters();
69 doReturn("tst.package").when(annotationType).getPackageName();
70 doReturn("tstAnnotationName").when(annotationType).getName();
71 doReturn(ANNOTATION).when(annotationType).getIdentifier();
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();
78 final Constant constant = mock(Constant.class);
79 doReturn(PATTERN_CONSTANT_NAME).when(constant).getName();
80 doReturn(ImmutableList.of(constant)).when(enclosedType).getConstantDefinitions();
82 doReturn(ImmutableList.of()).when(enclosedType).getEnclosedTypes();
83 doReturn(ImmutableList.of(enclosedType)).when(generatedType).getEnclosedTypes();
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"));
97 @Test(expected = UnsupportedOperationException.class)
98 public void constructTest() throws Throwable {
99 final Constructor<GeneratorUtil> constructor = GeneratorUtil.class.getDeclaredConstructor();
100 constructor.setAccessible(true);
102 constructor.newInstance();
103 } catch (Exception e) {
108 @Test(expected = IllegalArgumentException.class)
109 public void createImportsWithExceptionTest() throws Exception {
110 GeneratorUtil.createImports(null);
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"));
120 @Test(expected = IllegalArgumentException.class)
121 public void isConstantToExceptionTest() throws Exception {
122 GeneratorUtil.isConstantInTO(null, null);
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));
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));
141 public void getExplicitTypeTest() throws Exception {
142 assertEquals(annotationType.getName(), GeneratorUtil.getExplicitType(
143 generatedType, annotationType, createImports(generatedType)));
145 assertTrue(GeneratorUtil.getExplicitType(generatedType, parameterizedType,
146 createImports(generatedType)).contains(parameterizedType.getName()));
150 @Test(expected = IllegalArgumentException.class)
151 public void getTopParentTransportObjectWithExceptionTest() throws Exception {
152 GeneratorUtil.getTopParentTransportObject(null);
156 public void getTopParentTransportObjectTest() throws Exception {
157 assertEquals(enclosedType, GeneratorUtil.getTopParentTransportObject(enclosedType));
159 final GeneratedTransferObject parent = mock(GeneratedTransferObject.class);
160 doReturn(parent).when(enclosedType).getSuperType();
161 assertEquals(parent, GeneratorUtil.getTopParentTransportObject(enclosedType));