f08b77ff7b368b186b5da4f4a1ac51d57f0fd88a
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / test / java / org / opendaylight / controller / sal / binding / generator / impl / AnnotationBuilderTest.java
1 /*
2  * Copyright (c) 2013 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.controller.sal.binding.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.binding.model.api.AnnotationType;
20 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
22 import org.opendaylight.controller.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
23 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder;
24 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
25
26 public class AnnotationBuilderTest {
27
28     @Test
29     public void generatedTypeAnnotationTest() {
30         GeneratedTypeBuilder genTypeBuilder = new GeneratedTOBuilderImpl(
31                 "org.opendaylight.controller", "AnnotInterface");
32
33         genTypeBuilder.addAnnotation("javax.management", "MXBean");
34         final AnnotationTypeBuilder annotDesc = genTypeBuilder.addAnnotation(
35                 "javax.management", "Description");
36         annotDesc.addParameter("description", "some sort of interface");
37         
38         final GeneratedType genType = genTypeBuilder.toInstance();
39         
40         assertNotNull(genType);
41         assertNotNull(genType.getAnnotations());
42         assertEquals(2, genType.getAnnotations().size());
43         
44         int annotCount = 0;
45         for (final AnnotationType annotation : genType.getAnnotations()) {
46             if (annotation.getPackageName().equals("javax.management")
47                     && annotation.getName().equals("MXBean")) {
48                 annotCount++;
49                 assertEquals(0, annotation.getParameters().size());
50             }
51             if (annotation.getPackageName().equals("javax.management")
52                     && annotation.getName().equals("Description")) {
53                 annotCount++;
54                 assertEquals(1, annotation.getParameters().size());
55                 AnnotationType.Parameter param = annotation.getParameter("description");
56                 assertNotNull(param);
57                 assertEquals("description", param.getName());
58                 assertNotNull(param.getValue());
59                 assertEquals("some sort of interface", param.getValue());
60                 assertNotNull(param.getValues());
61                 assertTrue(param.getValues().isEmpty());
62             }
63         }
64         assertEquals(2, annotCount);
65     }
66
67     @Test
68     public void methodSignatureAnnotationTest() {
69         //TODO  add test for method annotations
70     }
71
72     @Test
73     public void generatedPropertyAnnotationTest() {
74         //TODO add test for property annotations
75     }
76
77     @Test
78     public void generatedTransfeObjectAnnotationTest() {
79         final GeneratedTOBuilder genTypeBuilder = new GeneratedTOBuilderImpl(
80                 "org.opendaylight.controller", "AnnotClassCache");
81
82         genTypeBuilder.addAnnotation("javax.management", "MBean");
83         final AnnotationTypeBuilder annotNotify = genTypeBuilder.addAnnotation(
84                 "javax.management", "NotificationInfo");
85
86         final List<String> notifyList = new ArrayList<String>();
87         notifyList.add("\"my.notif.type\"");
88         annotNotify.addParameters("types", notifyList);
89         annotNotify.addParameter("description",
90                 "@Description(\"my notification\")");
91
92         GeneratedTransferObject genTO = genTypeBuilder.toInstance();
93
94         assertNotNull(genTO);
95         assertNotNull(genTO.getAnnotations());
96         assertEquals(2, genTO.getAnnotations().size());
97
98         int annotCount = 0;
99         for (final AnnotationType annotation : genTO.getAnnotations()) {
100             if (annotation.getPackageName().equals("javax.management")
101                     && annotation.getName().equals("MBean")) {
102                 annotCount++;
103                 assertEquals(0, annotation.getParameters().size());
104             }
105             if (annotation.getPackageName().equals("javax.management")
106                     && annotation.getName().equals("NotificationInfo")) {
107                 annotCount++;
108                 assertEquals(2, annotation.getParameters().size());
109                 AnnotationType.Parameter param = annotation.getParameter("types");
110                 assertNotNull(param);
111                 assertEquals("types", param.getName());
112                 assertNull(param.getValue());
113                 assertNotNull(param.getValues());
114                 assertEquals(1, param.getValues().size());
115                 assertEquals("\"my.notif.type\"", param.getValues().get(0));
116                 
117                 param = annotation.getParameter("description");
118                 assertNotNull(param);
119                 assertEquals("description", param.getName());
120                 assertNotNull(param.getValue());
121                 assertEquals("@Description(\"my notification\")", param.getValue());
122             }
123         }
124         assertEquals(2, annotCount);
125     }
126 }