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.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.ri.TypeConstants.PATTERN_CONSTANT_NAME;
21 import com.google.common.collect.ImmutableList;
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.InvocationTargetException;
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;
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");
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);
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();
69 doReturn(ImmutableList.of(parameter)).when(methodSignature).getParameters();
71 doReturn("tst.package").when(annotationType).getPackageName();
72 doReturn("tstAnnotationName").when(annotationType).getName();
73 doReturn(ANNOTATION).when(annotationType).getIdentifier();
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();
80 final Constant constant = mock(Constant.class);
81 doReturn(PATTERN_CONSTANT_NAME).when(constant).getName();
82 doReturn(ImmutableList.of(constant)).when(enclosedType).getConstantDefinitions();
84 doReturn(ImmutableList.of()).when(enclosedType).getEnclosedTypes();
85 doReturn(ImmutableList.of(enclosedType)).when(generatedType).getEnclosedTypes();
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"));
99 public void constructTest() throws ReflectiveOperationException {
100 final Constructor<GeneratorUtil> constructor = GeneratorUtil.class.getDeclaredConstructor();
101 constructor.setAccessible(true);
103 constructor.newInstance();
105 } catch (InvocationTargetException e) {
106 assertTrue(e.getCause() instanceof UnsupportedOperationException);
110 @Test(expected = IllegalArgumentException.class)
111 public void createImportsWithExceptionTest() throws Exception {
112 GeneratorUtil.createImports(null);
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"));
122 @Test(expected = IllegalArgumentException.class)
123 public void isConstantToExceptionTest() throws Exception {
124 GeneratorUtil.isConstantInTO(null, null);
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));
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));
143 public void getExplicitTypeTest() throws Exception {
144 assertEquals(annotationType.getName(), GeneratorUtil.getExplicitType(
145 generatedType, annotationType, createImports(generatedType)));
147 assertTrue(GeneratorUtil.getExplicitType(generatedType, parameterizedType,
148 createImports(generatedType)).contains(parameterizedType.getName()));