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.util.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(),
43 "public interface test",
55 assertEquals(expected, new InterfaceGenerator().generate(genType));
59 public void builderTemplateDeprecatedListenerMethodTest() {
60 final MethodSignature methSign = mockMethSign("on" + TEST);
61 addMethodStatus(methSign, JavaTypeName.create(Deprecated.class));
62 final GeneratedType genType = mockGenType(methSign);
64 String expected = String.join(System.lineSeparator(),
66 "import java.lang.Deprecated;",
68 "public interface test",
81 assertEquals(expected, new InterfaceGenerator().generate(genType));
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);
91 String expected = String.join(System.lineSeparator(),
93 "import java.lang.Deprecated;",
95 "public interface test",
102 " default void ontest() {",
110 assertEquals(expected, new InterfaceGenerator().generate(genType));
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();
122 final List<Type> impls = new ArrayList<>();
123 doReturn(impls).when(genType).getImplements();
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();
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();