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.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.spy;
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;
26 public class InterfaceGeneratorTest {
27 private static final String TEST = "test";
28 private static final JavaTypeName TYPE_NAME = JavaTypeName.create(TEST, TEST);
31 public void basicTest() {
32 assertEquals("", new InterfaceGenerator().generate(mock(Type.class)));
36 public void builderTemplateListenerMethodTest() {
37 final MethodSignature methSign = mockMethSign("on" + TEST);
38 final GeneratedType genType = mockGenType(methSign);
40 String expected = String.join(System.lineSeparator(),
42 "import javax.annotation.processing.Generated;",
44 "@Generated(\"mdsal-binding-generator\")",
45 "public interface test",
57 assertEquals(expected, new InterfaceGenerator().generate(genType));
61 public void builderTemplateDeprecatedListenerMethodTest() {
62 final MethodSignature methSign = mockMethSign("on" + TEST);
63 addMethodStatus(methSign, JavaTypeName.create(Deprecated.class));
64 final GeneratedType genType = mockGenType(methSign);
66 String expected = String.join(System.lineSeparator(),
68 "import java.lang.Deprecated;",
69 "import javax.annotation.processing.Generated;",
71 "@Generated(\"mdsal-binding-generator\")",
72 "public interface test",
85 assertEquals(expected, new InterfaceGenerator().generate(genType));
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);
95 String expected = String.join(System.lineSeparator(),
97 "import java.lang.Deprecated;",
98 "import javax.annotation.processing.Generated;",
100 "@Generated(\"mdsal-binding-generator\")",
101 "public interface test",
108 " default void ontest() {",
116 assertEquals(expected, new InterfaceGenerator().generate(genType));
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();
128 final List<Type> impls = new ArrayList<>();
129 doReturn(impls).when(genType).getImplements();
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();
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();