HONEYCOMB-424: bump ODL dependencies to Oxygen
[honeycomb.git] / infra / bgp-translate-impl / src / main / test / java / io / fd / honeycomb / bgp / translate / impl / LocRibChangeListenerTest.java
1 /*
2  * Copyright (c) 2017 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.bgp.translate.impl;
18
19 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
20
21 import io.fd.honeycomb.translate.bgp.RouteWriter;
22 import io.fd.honeycomb.translate.write.WriteFailedException;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentMatchers;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
32 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
33 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36
37 public class LocRibChangeListenerTest {
38
39     private static final DataTreeIdentifier<Route> ID =
40         new DataTreeIdentifier<>(OPERATIONAL, InstanceIdentifier.create(Route.class));
41
42     @Mock
43     private RouteWriter<Route> routeWriter;
44     @Mock
45     private DataObjectModification<Route> rootNode;
46
47     private LocRibChangeListener locRibListener;
48
49     @Before
50     public void setUp() {
51         MockitoAnnotations.initMocks(this);
52         locRibListener = new LocRibChangeListener(routeWriter);
53     }
54
55     @Test
56     public void testDataTreeChanged() throws WriteFailedException {
57         final Route route1 = Mockito.mock(Route.class);
58         final Route route2 = Mockito.mock(Route.class);
59         locRibListener.onDataTreeChanged(Arrays.asList(
60             mockDateTreeModification(null, route1),
61             mockDateTreeModification(route1, route2),
62             mockDateTreeModification(route2, null))
63         );
64         Mockito.verify(routeWriter).create(ID.getRootIdentifier(), route1);
65         Mockito.verify(routeWriter).update(ID.getRootIdentifier(), route1, route2);
66         Mockito.verify(routeWriter).delete(ID.getRootIdentifier(), route2);
67     }
68
69     @Test
70     public void testDataTreeChangedFailed() throws WriteFailedException.CreateFailedException {
71         final Route dataAfter = Mockito.mock(Route.class);
72         Mockito.doThrow(new WriteFailedException.CreateFailedException(ID.getRootIdentifier(), dataAfter))
73             .when(routeWriter)
74             .create(ArgumentMatchers.any(), ArgumentMatchers.any());
75         locRibListener.onDataTreeChanged(Collections.singletonList(mockDateTreeModification(null, dataAfter)));
76         Mockito.verify(routeWriter).create(ID.getRootIdentifier(), dataAfter);
77     }
78
79     @SuppressWarnings("unchecked")
80     private DataTreeModification<Route> mockDateTreeModification(final Route dataBefore, final Route dataAfter) {
81         final DataTreeModification<Route> modification = Mockito.mock(DataTreeModification.class);
82         final DataObjectModification<Route> rootNode = Mockito.mock(DataObjectModification.class);
83         Mockito.when(rootNode.getDataBefore()).thenReturn(dataBefore);
84         Mockito.when(rootNode.getDataAfter()).thenReturn(dataAfter);
85         Mockito.when(modification.getRootPath()).thenReturn(ID);
86         Mockito.when(modification.getRootNode()).thenReturn(rootNode);
87         return modification;
88     }
89 }