HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / DisabledInterfacesManagerTest.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.translate.v3po;
18
19
20
21
22 import static org.hamcrest.CoreMatchers.hasItems;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertThat;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.verify;
28
29 import com.google.common.base.Optional;
30 import io.fd.honeycomb.translate.MappingContext;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.stream.Collectors;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.DisabledInterfaces;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.DisabledInterfacesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndex;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndexBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndexKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
45
46 public class DisabledInterfacesManagerTest {
47
48     private static final InstanceIdentifier<DisabledInterfaces>
49             ROOT_ID = InstanceIdentifier.create(DisabledInterfaces.class);
50     private static final KeyedInstanceIdentifier<DisabledInterfaceIndex, DisabledInterfaceIndexKey> SPECIFIC_ID_1 =
51             ROOT_ID.child(DisabledInterfaceIndex.class, new DisabledInterfaceIndexKey(1));
52     private static final KeyedInstanceIdentifier<DisabledInterfaceIndex, DisabledInterfaceIndexKey> SPECIFIC_ID_4 =
53             ROOT_ID.child(DisabledInterfaceIndex.class, new DisabledInterfaceIndexKey(4));
54
55     @Mock
56     private MappingContext mappingContext;
57     private DisabledInterfacesManager manager;
58
59     @Before
60     public void setUp() throws Exception {
61         MockitoAnnotations.initMocks(this);
62         manager = new DisabledInterfacesManager();
63         doReturn(Optional.of(new DisabledInterfacesBuilder().setDisabledInterfaceIndex(toIndices(1, 2, 3)).build()))
64                 .when(mappingContext)
65                 .read(ROOT_ID);
66         doReturn(Optional.of(toIndex(1)))
67                 .when(mappingContext)
68                 .read(SPECIFIC_ID_1);
69         doReturn(Optional.absent())
70                 .when(mappingContext)
71                 .read(SPECIFIC_ID_4);
72     }
73
74     @Test
75     public void testGetAll() throws Exception {
76         final List<Integer> disabledInterfaces = manager.getDisabledInterfaces(mappingContext);
77         assertThat(disabledInterfaces, hasItems(1, 2, 3));
78     }
79
80     @Test
81     public void testCheckOne() throws Exception {
82         assertTrue(manager.isInterfaceDisabled(1, mappingContext));
83         assertFalse(manager.isInterfaceDisabled(4, mappingContext));
84     }
85
86     @Test
87     public void testDisable() throws Exception {
88         manager.disableInterface(1, mappingContext);
89         verify(mappingContext).put(SPECIFIC_ID_1, toIndex(1));
90     }
91
92     @Test
93     public void testRemoveDisability() throws Exception {
94         manager.removeDisabledInterface(1, mappingContext);
95         verify(mappingContext).delete(SPECIFIC_ID_1);
96     }
97
98     private List<DisabledInterfaceIndex> toIndices(final int... indices) {
99         return Arrays.stream(indices)
100                 .mapToObj(this::toIndex)
101                 .collect(Collectors.toList());
102     }
103
104     private DisabledInterfaceIndex toIndex(final int idx) {
105         return new DisabledInterfaceIndexBuilder()
106                 .setIndex(idx)
107                 .build();
108     }
109 }