Do not generate union builders
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / InterfaceGeneratorTest.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.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.spy;
14
15 import com.google.common.collect.ImmutableList;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
21 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
22 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.mdsal.binding.model.ri.Types;
25
26 public class InterfaceGeneratorTest {
27     private static final String TEST = "test";
28     private static final JavaTypeName TYPE_NAME = JavaTypeName.create(TEST, TEST);
29
30     @Test
31     public void basicTest() {
32         assertEquals("", new InterfaceGenerator().generate(mock(Type.class)));
33     }
34
35     @Test
36     public void builderTemplateListenerMethodTest() {
37         final MethodSignature methSign = mockMethSign("on" + TEST);
38         final GeneratedType genType = mockGenType(methSign);
39
40         String expected = String.join(System.lineSeparator(),
41             "package test;",
42             "import javax.annotation.processing.Generated;",
43             "",
44             "@Generated(\"mdsal-binding-generator\")",
45             "public interface test",
46             "{",
47             "",
48             "",
49             "",
50             "",
51             "    void ontest();",
52             "",
53             "}",
54             "",
55             ""
56         );
57         assertEquals(expected, new InterfaceGenerator().generate(genType));
58     }
59
60     @Test
61     public void builderTemplateDeprecatedListenerMethodTest() {
62         final MethodSignature methSign = mockMethSign("on" + TEST);
63         addMethodStatus(methSign, JavaTypeName.create(Deprecated.class));
64         final GeneratedType genType = mockGenType(methSign);
65
66         String expected = String.join(System.lineSeparator(),
67             "package test;",
68             "import java.lang.Deprecated;",
69             "import javax.annotation.processing.Generated;",
70             "",
71             "@Generated(\"mdsal-binding-generator\")",
72             "public interface test",
73             "{",
74             "",
75             "",
76             "",
77             "",
78             "    @Deprecated",
79             "    void ontest();",
80             "",
81             "}",
82             "",
83             ""
84         );
85         assertEquals(expected, new InterfaceGenerator().generate(genType));
86     }
87
88     @Test
89     public void builderTemplateGenerateObsoleteListenerMethodTest() {
90         final MethodSignature methSign = mockMethSign("on" + TEST);
91         addMethodStatus(methSign, JavaTypeName.create(Deprecated.class));
92         doReturn(true).when(methSign).isDefault();
93         final GeneratedType genType = mockGenType(methSign);
94
95         String expected = String.join(System.lineSeparator(),
96             "package test;",
97             "import java.lang.Deprecated;",
98             "import javax.annotation.processing.Generated;",
99             "",
100             "@Generated(\"mdsal-binding-generator\")",
101             "public interface test",
102             "{",
103             "",
104             "",
105             "",
106             "",
107             "    @Deprecated",
108             "    default void ontest() {",
109             "        // No-op",
110             "    }",
111             "",
112             "}",
113             "",
114             ""
115         );
116         assertEquals(expected, new InterfaceGenerator().generate(genType));
117     }
118
119     private static GeneratedType mockGenType(final MethodSignature methSign) {
120         final GeneratedType genType = spy(GeneratedType.class);
121         doReturn(TYPE_NAME).when(genType).getIdentifier();
122         doReturn(TEST).when(genType).getName();
123         doReturn(TEST).when(genType).getPackageName();
124         final List<MethodSignature> listMethodSign = new ArrayList<>();
125         listMethodSign.add(methSign);
126         doReturn(listMethodSign).when(genType).getMethodDefinitions();
127
128         final List<Type> impls = new ArrayList<>();
129         doReturn(impls).when(genType).getImplements();
130         return genType;
131     }
132
133     private static MethodSignature mockMethSign(final String methodeName) {
134         final MethodSignature methSign = mock(MethodSignature.class);
135         doReturn(methodeName).when(methSign).getName();
136         final Type methType = Types.typeForClass(void.class);
137         doReturn(methType).when(methSign).getReturnType();
138         doReturn(MethodSignature.ValueMechanics.NORMAL).when(methSign).getMechanics();
139         return methSign;
140     }
141
142     private static void addMethodStatus(final MethodSignature methSign, final JavaTypeName annotationJavaType) {
143         final AnnotationType annotationType = mock(AnnotationType.class);
144         doReturn(annotationJavaType).when(annotationType).getIdentifier();
145         doReturn(ImmutableList.of(annotationType)).when(methSign).getAnnotations();
146     }
147 }