Propagate notification status to generated listener methods
[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.util.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             "",
43             "public interface test",
44             "{",
45             "",
46             "",
47             "",
48             "",
49             "    void ontest();",
50             "",
51             "}",
52             "",
53             ""
54         );
55         assertEquals(expected, new InterfaceGenerator().generate(genType));
56     }
57
58     @Test
59     public void builderTemplateDeprecatedListenerMethodTest() {
60         final MethodSignature methSign = mockMethSign("on" + TEST);
61         addMethodStatus(methSign, JavaTypeName.create(Deprecated.class));
62         final GeneratedType genType = mockGenType(methSign);
63
64         String expected = String.join(System.lineSeparator(),
65             "package test;",
66             "import java.lang.Deprecated;",
67             "",
68             "public interface test",
69             "{",
70             "",
71             "",
72             "",
73             "",
74             "    @Deprecated",
75             "    void ontest();",
76             "",
77             "}",
78             "",
79             ""
80         );
81         assertEquals(expected, new InterfaceGenerator().generate(genType));
82     }
83
84     @Test
85     public void builderTemplateGenerateObsoleteListenerMethodTest() {
86         final MethodSignature methSign = mockMethSign("on" + TEST);
87         addMethodStatus(methSign, JavaTypeName.create(Deprecated.class));
88         doReturn(true).when(methSign).isDefault();
89         final GeneratedType genType = mockGenType(methSign);
90
91         String expected = String.join(System.lineSeparator(),
92             "package test;",
93             "import java.lang.Deprecated;",
94             "",
95             "public interface test",
96             "{",
97             "",
98             "",
99             "",
100             "",
101             "    @Deprecated",
102             "    default void ontest() {",
103             "        // No-op",
104             "    }",
105             "",
106             "}",
107             "",
108             ""
109         );
110         assertEquals(expected, new InterfaceGenerator().generate(genType));
111     }
112
113     private static GeneratedType mockGenType(final MethodSignature methSign) {
114         final GeneratedType genType = spy(GeneratedType.class);
115         doReturn(TYPE_NAME).when(genType).getIdentifier();
116         doReturn(TEST).when(genType).getName();
117         doReturn(TEST).when(genType).getPackageName();
118         final List<MethodSignature> listMethodSign = new ArrayList<>();
119         listMethodSign.add(methSign);
120         doReturn(listMethodSign).when(genType).getMethodDefinitions();
121
122         final List<Type> impls = new ArrayList<>();
123         doReturn(impls).when(genType).getImplements();
124         return genType;
125     }
126
127     private static MethodSignature mockMethSign(final String methodeName) {
128         final MethodSignature methSign = mock(MethodSignature.class);
129         doReturn(methodeName).when(methSign).getName();
130         final Type methType = Types.typeForClass(void.class);
131         doReturn(methType).when(methSign).getReturnType();
132         doReturn(MethodSignature.ValueMechanics.NORMAL).when(methSign).getMechanics();
133         return methSign;
134     }
135
136     private static void addMethodStatus(MethodSignature methSign, JavaTypeName annotationJavaType) {
137         final AnnotationType annotationType = mock(AnnotationType.class);
138         doReturn(annotationJavaType).when(annotationType).getIdentifier();
139         doReturn(ImmutableList.of(annotationType)).when(methSign).getAnnotations();
140     }
141 }