2 * Copyright (c) 2017 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.bgp.translate.impl;
19 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
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;
37 public class LocRibChangeListenerTest {
39 private static final DataTreeIdentifier<Route> ID =
40 new DataTreeIdentifier<>(OPERATIONAL, InstanceIdentifier.create(Route.class));
43 private RouteWriter<Route> routeWriter;
45 private DataObjectModification<Route> rootNode;
47 private LocRibChangeListener locRibListener;
51 MockitoAnnotations.initMocks(this);
52 locRibListener = new LocRibChangeListener(routeWriter);
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))
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);
70 public void testDataTreeChangedFailed() throws WriteFailedException.CreateFailedException {
71 final Route dataAfter = Mockito.mock(Route.class);
72 Mockito.doThrow(new WriteFailedException.CreateFailedException(ID.getRootIdentifier(), dataAfter))
74 .create(ArgumentMatchers.any(), ArgumentMatchers.any());
75 locRibListener.onDataTreeChanged(Collections.singletonList(mockDateTreeModification(null, dataAfter)));
76 Mockito.verify(routeWriter).create(ID.getRootIdentifier(), dataAfter);
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);