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.MethodSignature;
31 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
32 import org.opendaylight.mdsal.binding.model.api.Type;
34 public class GeneratorUtilTest {
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);
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();
61 doReturn("tst.package").when(annotationType).getPackageName();
62 doReturn("tstAnnotationName").when(annotationType).getName();
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();
69 final Constant constant = mock(Constant.class);
70 doReturn(PATTERN_CONSTANT_NAME).when(constant).getName();
71 doReturn(ImmutableList.of(constant)).when(enclosedType).getConstantDefinitions();
73 doReturn(ImmutableList.of()).when(enclosedType).getEnclosedTypes();
74 doReturn(ImmutableList.of(enclosedType)).when(generatedType).getEnclosedTypes();
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"));
88 @Test(expected = UnsupportedOperationException.class)
89 public void constructTest() throws Throwable {
90 final Constructor constructor = GeneratorUtil.class.getDeclaredConstructor();
91 constructor.setAccessible(true);
93 constructor.newInstance();
94 } catch (Exception e) {
99 @Test(expected = IllegalArgumentException.class)
100 public void createImportsWithExceptionTest() throws Exception {
101 GeneratorUtil.createImports(null);
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"));
111 @Test(expected = IllegalArgumentException.class)
112 public void isConstantToExceptionTest() throws Exception {
113 GeneratorUtil.isConstantInTO(null, null);
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));
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));
132 public void getExplicitTypeTest() throws Exception {
133 assertEquals(annotationType.getName(), GeneratorUtil.getExplicitType(
134 generatedType, annotationType, createImports(generatedType)));
136 assertTrue(GeneratorUtil.getExplicitType(generatedType, parameterizedType,
137 createImports(generatedType)).contains(parameterizedType.getName()));
141 @Test(expected = IllegalArgumentException.class)
142 public void getTopParentTransportObjectWithExceptionTest() throws Exception {
143 GeneratorUtil.getTopParentTransportObject(null);
147 public void getTopParentTransportObjectTest() throws Exception {
148 assertEquals(enclosedType, GeneratorUtil.getTopParentTransportObject(enclosedType));
150 final GeneratedTransferObject parent = mock(GeneratedTransferObject.class);
151 doReturn(parent).when(enclosedType).getSuperType();
152 assertEquals(parent, GeneratorUtil.getTopParentTransportObject(enclosedType));