Update to MD-SAL APIs
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / sal / binding / test / RuntimeCodeGeneratorTest.java
1 package org.opendaylight.controller.sal.binding.test;
2 import static org.junit.Assert.*;
3
4 import java.util.Arrays;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import javassist.ClassPool;
9
10 import org.junit.Before;
11 import org.junit.Test;
12
13 import static org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.*;
14
15 import org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator;
16 import org.opendaylight.controller.sal.binding.test.mock.FooService;
17 import org.opendaylight.controller.sal.binding.test.mock.ReferencableObject;
18 import org.opendaylight.controller.sal.binding.test.mock.ReferencableObjectKey;
19 import org.opendaylight.controller.sal.binding.test.mock.SimpleInput;
20 import org.opendaylight.yangtools.yang.binding.Augmentation;
21 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
26
27 import static org.mockito.Mockito.*;
28
29
30 public class RuntimeCodeGeneratorTest {
31
32     private RuntimeCodeGenerator codeGenerator;
33
34     
35     @Before
36     public void initialize() {
37         this.codeGenerator = new RuntimeCodeGenerator(ClassPool.getDefault());
38     }
39     
40     @Test
41     public void testGenerateDirectProxy() {
42         Class<? extends FooService> product = codeGenerator.generateDirectProxy(FooService.class);
43         assertNotNull(product);
44     }
45
46     @Test
47     public void testGenerateRouter() throws Exception {
48         Class<? extends FooService> product = codeGenerator.generateRouter(FooService.class);
49         assertNotNull(product);
50         assertNotNull(product.getSimpleName());
51         assertEquals("2 fields should be generated.",2,product.getFields().length);
52         
53         verifyRouting(product.newInstance());
54     }
55
56     private void verifyRouting(FooService product) {
57         Map<InstanceIdentifier<? extends Object>,FooService> routingTable = new HashMap<>();
58         setRoutingTable(product, BaseIdentity.class, routingTable);
59         
60         assertSame("Returned routing table should be same instance",routingTable,getRoutingTable(product, BaseIdentity.class));
61         
62         int servicesCount = 2;
63         int instancesPerService = 3;
64         
65         InstanceIdentifier<?>[][] identifiers = identifiers(servicesCount,instancesPerService);
66         FooService service[] = new FooService[] {
67                 mock(FooService.class, "Instance 0"),
68                 mock(FooService.class,"Instance 1")
69         };
70         
71         for(int i = 0;i<service.length;i++) {
72             for (InstanceIdentifier<?> instance : identifiers[i]) {
73                 routingTable.put(instance, service[i]);
74             }
75         }
76         
77         assertEquals("All instances should be registered.", servicesCount*instancesPerService, routingTable.size());
78         
79         SimpleInput[] instance_0_input = new SimpleInputImpl[] {
80             new SimpleInputImpl(identifiers[0][0]),
81             new SimpleInputImpl(identifiers[0][1]),
82             new SimpleInputImpl(identifiers[0][2])
83         };
84         
85         SimpleInput[] instance_1_input = new SimpleInputImpl[] {
86                 new SimpleInputImpl(identifiers[1][0]),
87                 new SimpleInputImpl(identifiers[1][1]),
88                 new SimpleInputImpl(identifiers[1][2])
89         };
90         
91         // We test sending mock messages
92         
93         product.simple(instance_0_input[0]);
94         verify(service[0]).simple(instance_0_input[0]);
95         
96         product.simple(instance_0_input[1]);
97         product.simple(instance_0_input[2]);
98         
99         verify(service[0]).simple(instance_0_input[1]);
100         verify(service[0]).simple(instance_0_input[2]);
101         
102         product.simple(instance_1_input[0]);
103         verify(service[1]).simple(instance_1_input[0]);
104     }
105
106     private InstanceIdentifier<?>[][] identifiers(int serviceSize, int instancesPerService) {
107         InstanceIdentifier<?>[][] ret = new InstanceIdentifier[serviceSize][];
108         int service = 0;
109         for (int i = 0;i<serviceSize;i++) {
110             
111             InstanceIdentifier<?>[] instanceIdentifiers = new InstanceIdentifier[instancesPerService];
112             ret[i] = instanceIdentifiers;
113             for(int id = 0;id<instancesPerService;id++) {
114                 instanceIdentifiers[id] = referencableIdentifier(service*instancesPerService+id);
115             }
116             service++;
117         }
118         
119         return ret;
120     }
121
122     private InstanceIdentifier<?> referencableIdentifier(int i) {
123         ReferencableObjectKey key = new ReferencableObjectKey(i);
124         IdentifiableItem<ReferencableObject,ReferencableObjectKey> pathArg = new IdentifiableItem<>(ReferencableObject.class,key);
125         return new InstanceIdentifier<ReferencableObject>(Arrays.<PathArgument>asList(pathArg), ReferencableObject.class);
126     }
127
128     private static class SimpleInputImpl implements SimpleInput {
129         private final InstanceIdentifier<?> identifier;
130
131         public SimpleInputImpl(InstanceIdentifier<?> _identifier) {
132             this.identifier = _identifier;
133         }
134
135         @Override
136         public <E extends Augmentation<SimpleInput>> E getAugmentation(Class<E> augmentationType) {
137             return null;
138         }
139
140         @Override
141         public InstanceIdentifier<?> getIdentifier() {
142             return this.identifier;
143         }
144     }
145 }