2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.util;
10 import static junit.framework.TestCase.assertNull;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.when;
17 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
18 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentConfiguration;
19 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentOperational;
20 import static org.opendaylight.protocol.util.CheckUtil.checkPresentConfiguration;
21 import static org.opendaylight.protocol.util.CheckUtil.checkPresentOperational;
22 import static org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages;
23 import static org.opendaylight.protocol.util.CheckUtil.readDataConfiguration;
24 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
25 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
27 import com.google.common.base.VerifyException;
28 import io.netty.channel.ChannelFuture;
29 import io.netty.util.concurrent.GenericFutureListener;
30 import java.util.concurrent.ExecutionException;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
36 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
37 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
38 import org.opendaylight.protocol.util.CheckUtil.ListenerCheck;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 public class CheckUtilTest extends AbstractConcurrentDataBrokerTest {
48 private static final TopologyId TOPOLOGY_ID = new TopologyId("topotest");
49 private final KeyedInstanceIdentifier<Topology, TopologyKey> topologyIIdKeyed =
50 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
51 new TopologyKey(TOPOLOGY_ID));
52 private static final int TIMEOUT = 1;
54 private ListenerCheck listenerCheck;
56 private ChannelFuture future;
59 public void setUp() throws Exception {
60 MockitoAnnotations.initMocks(this);
63 @Test(expected = VerifyException.class)
64 public void testWaitFutureSuccessFail() {
65 when(this.future.isDone()).thenReturn(false);
66 doReturn(this.future).when(this.future).addListener(any());
67 waitFutureSuccess(this.future);
71 public void testWaitFutureSuccess() {
72 when(this.future.isSuccess()).thenReturn(true);
73 doAnswer(invocation -> {
74 invocation.getArgumentAt(0, GenericFutureListener.class).operationComplete(CheckUtilTest.this.future);
75 return CheckUtilTest.this.future;
76 }).when(this.future).addListener(any());
77 waitFutureSuccess(this.future);
80 @Test(expected = NullPointerException.class)
81 public void testReadDataOperationalNull() throws Exception {
82 readDataOperational(getDataBroker(), topologyIIdKeyed, test -> false, TIMEOUT);
85 @Test(expected = NullPointerException.class)
86 public void testReadDataConfigurationNull() throws Exception {
87 readDataConfiguration(getDataBroker(), topologyIIdKeyed, test -> false, TIMEOUT);
90 @Test(expected = AssertionError.class)
91 public void testReadDataOperationalFail() throws Exception {
92 storeTopo(LogicalDatastoreType.OPERATIONAL);
93 readDataOperational(getDataBroker(), this.topologyIIdKeyed, result -> {
94 assertNotNull(result.getNode());
99 @Test(expected = AssertionError.class)
100 public void testReadDataConfigurationFail() throws Exception {
101 storeTopo(LogicalDatastoreType.CONFIGURATION);
102 readDataConfiguration(getDataBroker(), this.topologyIIdKeyed, result -> {
103 assertNotNull(result.getNode());
109 public void testReadDataOperational() throws Exception {
110 storeTopo(LogicalDatastoreType.OPERATIONAL);
111 readDataOperational(getDataBroker(), this.topologyIIdKeyed, result -> {
112 assertNull(result.getNode());
118 public void testReadDataConfiguration() throws Exception {
119 storeTopo(LogicalDatastoreType.CONFIGURATION);
120 readDataConfiguration(getDataBroker(), this.topologyIIdKeyed, result -> {
121 assertNull(result.getNode());
126 private void storeTopo(final LogicalDatastoreType dsType) throws ExecutionException, InterruptedException {
127 final WriteTransaction wt = getDataBroker().newWriteOnlyTransaction();
128 wt.put(dsType, this.topologyIIdKeyed,
129 new TopologyBuilder()
130 .setTopologyId(TOPOLOGY_ID)
136 public void testCheckPresentConfiguration() throws Exception {
137 storeTopo(LogicalDatastoreType.CONFIGURATION);
138 checkPresentConfiguration(getDataBroker(), this.topologyIIdKeyed);
142 public void testCheckPresentOperational() throws Exception {
143 storeTopo(LogicalDatastoreType.OPERATIONAL);
144 checkPresentOperational(getDataBroker(), this.topologyIIdKeyed);
147 @Test(expected = AssertionError.class)
148 public void testCheckNotPresentOperationalFail() throws Exception {
149 storeTopo(LogicalDatastoreType.OPERATIONAL);
150 checkNotPresentOperational(getDataBroker(), this.topologyIIdKeyed);
154 public void testCheckNotPresentOperational() throws Exception {
155 checkNotPresentOperational(getDataBroker(), this.topologyIIdKeyed);
159 public void testCheckNotPresentConfiguration() throws Exception {
160 checkNotPresentConfiguration(getDataBroker(), this.topologyIIdKeyed);
163 @Test(expected = AssertionError.class)
164 public void testCheckEquals() throws Exception {
165 checkEquals(() -> fail(), TIMEOUT);
168 @Test(expected = AssertionError.class)
169 public void testCheckReceivedMessagesNotEqual() {
170 doReturn(0).when(this.listenerCheck).getListMessageSize();
171 checkReceivedMessages(this.listenerCheck, 1, TIMEOUT);
175 public void testCheckReceivedMessagesEqual() {
176 doReturn(1).when(this.listenerCheck).getListMessageSize();
177 checkReceivedMessages(this.listenerCheck, 1, TIMEOUT);