1ff6cc631469d3a724d27f5417ecaa445e1f198a
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / read / VniTableCustomizerTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.lisp.translate.read;
18
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.when;
25
26 import com.google.common.collect.ImmutableList;
27 import io.fd.honeycomb.translate.read.ReadFailedException;
28 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
29 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
30 import io.fd.vpp.jvpp.VppCallbackException;
31 import io.fd.vpp.jvpp.core.dto.LispEidTableVniDetails;
32 import io.fd.vpp.jvpp.core.dto.LispEidTableVniDetailsReplyDump;
33 import java.util.List;
34 import java.util.concurrent.CompletableFuture;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.EidTable;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.EidTableBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTable;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTableBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTableKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 public class VniTableCustomizerTest extends ListReaderCustomizerTest<VniTable, VniTableKey, VniTableBuilder> {
46
47     private InstanceIdentifier<VniTable> validId;
48
49     public VniTableCustomizerTest() {
50         super(VniTable.class, EidTableBuilder.class);
51     }
52
53     @Before
54     public void init() {
55         validId = InstanceIdentifier.create(EidTable.class).child(VniTable.class, new VniTableKey(12L));
56     }
57
58     @Test
59     public void testReadAllSuccessfull() throws ReadFailedException {
60         whenLispEidTableVniDumpReturnValid();
61         final List<VniTableKey> keys = getCustomizer().getAllIds(validId, ctx);
62
63         assertNotNull(keys);
64         assertEquals(3, keys.size());
65         assertTrue(keys.contains(new VniTableKey(12L)));
66         assertTrue(keys.contains(new VniTableKey(14L)));
67         assertTrue(keys.contains(new VniTableKey(16L)));
68     }
69
70     @Test
71     public void testReadAllFailed() {
72         whenLispEidTableVniDumpThrowException();
73         try {
74             getCustomizer().getAllIds(validId, ctx);
75         } catch (ReadFailedException e) {
76             assertTrue(e.getCause() instanceof VppCallbackException);
77             return;
78         }
79
80         fail("Test should have thrown ReadFailedException");
81     }
82
83     @Test
84     public void testReadAttributes() throws ReadFailedException {
85         whenLispEidTableVniDumpReturnValid();
86         VniTableBuilder builder = new VniTableBuilder();
87
88         customizer.readCurrentAttributes(validId, builder, ctx);
89
90         final VniTable table = builder.build();
91         assertNotNull(table);
92         assertEquals(12L, table.getVirtualNetworkIdentifier().longValue());
93     }
94
95     private void whenLispEidTableVniDumpReturnValid() {
96
97         LispEidTableVniDetailsReplyDump dump = new LispEidTableVniDetailsReplyDump();
98         LispEidTableVniDetails details1 = new LispEidTableVniDetails();
99         details1.vni = 14;
100
101         LispEidTableVniDetails details2 = new LispEidTableVniDetails();
102         details2.vni = 12;
103
104         LispEidTableVniDetails details3 = new LispEidTableVniDetails();
105         details3.vni = 16;
106
107         dump.lispEidTableVniDetails = ImmutableList.of(details1, details2, details3);
108
109         when(api.lispEidTableVniDump(Mockito.any())).thenReturn(CompletableFuture.completedFuture(dump));
110     }
111
112     private void whenLispEidTableVniDumpThrowException() {
113         when(api.lispEidTableVniDump(Mockito.any()))
114                 .thenReturn(failedFuture());
115     }
116
117     @Override
118     protected ReaderCustomizer<VniTable, VniTableBuilder> initCustomizer() {
119         return new VniTableCustomizer(api);
120     }
121 }