Migrate to MD-SAL APIs
[bgpcep.git] / testtool-util / src / test / java / org / opendaylight / protocol / util / CheckUtilTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.util;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.when;
16 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
17 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentConfiguration;
18 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentOperational;
19 import static org.opendaylight.protocol.util.CheckUtil.checkPresentConfiguration;
20 import static org.opendaylight.protocol.util.CheckUtil.checkPresentOperational;
21 import static org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages;
22 import static org.opendaylight.protocol.util.CheckUtil.readDataConfiguration;
23 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
24 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
25
26 import com.google.common.base.VerifyException;
27 import io.netty.channel.ChannelFuture;
28 import io.netty.util.concurrent.GenericFutureListener;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.opendaylight.mdsal.binding.api.WriteTransaction;
37 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
38 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
39 import org.opendaylight.protocol.util.CheckUtil.ListenerCheck;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47
48 public class CheckUtilTest extends AbstractConcurrentDataBrokerTest {
49     private static final TopologyId TOPOLOGY_ID = new TopologyId("topotest");
50     private final KeyedInstanceIdentifier<Topology, TopologyKey> topologyIIdKeyed =
51             InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
52                     new TopologyKey(TOPOLOGY_ID));
53     private static final int TIMEOUT = 1;
54     @Mock
55     private ListenerCheck listenerCheck;
56     @Mock
57     private ChannelFuture future;
58
59     @Before
60     public void setUp() throws Exception {
61         MockitoAnnotations.initMocks(this);
62     }
63
64     @Test(expected = VerifyException.class)
65     public void testWaitFutureSuccessFail() {
66         when(this.future.isDone()).thenReturn(false);
67         doReturn(this.future).when(this.future).addListener(any());
68         waitFutureSuccess(this.future, 10L, TimeUnit.MILLISECONDS);
69     }
70
71     @Test
72     public void testWaitFutureSuccess() {
73         when(this.future.isSuccess()).thenReturn(true);
74         doAnswer(invocation -> {
75             ((GenericFutureListener)invocation.getArguments()[0]).operationComplete(CheckUtilTest.this.future);
76             return CheckUtilTest.this.future;
77         }).when(this.future).addListener(any());
78         waitFutureSuccess(this.future);
79     }
80
81     @Test(expected = NullPointerException.class)
82     public void testReadDataOperationalNull() throws Exception {
83         readDataOperational(getDataBroker(), topologyIIdKeyed, test -> false, TIMEOUT);
84     }
85
86     @Test(expected = NullPointerException.class)
87     public void testReadDataConfigurationNull() throws Exception {
88         readDataConfiguration(getDataBroker(), topologyIIdKeyed, test -> false, TIMEOUT);
89     }
90
91     @Test(expected = AssertionError.class)
92     public void testReadDataOperationalFail() throws Exception {
93         storeTopo(LogicalDatastoreType.OPERATIONAL);
94         readDataOperational(getDataBroker(), this.topologyIIdKeyed, result -> {
95             assertNotNull(result.getNode());
96             return result;
97         }, TIMEOUT);
98     }
99
100     @Test(expected = AssertionError.class)
101     public void testReadDataConfigurationFail() throws Exception {
102         storeTopo(LogicalDatastoreType.CONFIGURATION);
103         readDataConfiguration(getDataBroker(), this.topologyIIdKeyed, result -> {
104             assertNotNull(result.getNode());
105             return result;
106         }, TIMEOUT);
107     }
108
109     @Test
110     public void testReadDataOperational() throws Exception {
111         storeTopo(LogicalDatastoreType.OPERATIONAL);
112         readDataOperational(getDataBroker(), this.topologyIIdKeyed, result -> {
113             assertNull(result.getNode());
114             return result;
115         }, TIMEOUT);
116     }
117
118     @Test
119     public void testReadDataConfiguration() throws Exception {
120         storeTopo(LogicalDatastoreType.CONFIGURATION);
121         readDataConfiguration(getDataBroker(), this.topologyIIdKeyed, result -> {
122             assertNull(result.getNode());
123             return result;
124         }, TIMEOUT);
125     }
126
127     private void storeTopo(final LogicalDatastoreType dsType) throws ExecutionException, InterruptedException {
128         final WriteTransaction wt = getDataBroker().newWriteOnlyTransaction();
129         wt.put(dsType, this.topologyIIdKeyed,
130                 new TopologyBuilder()
131                         .setTopologyId(TOPOLOGY_ID)
132                         .build(), true);
133         wt.commit().get();
134     }
135
136     @Test
137     public void testCheckPresentConfiguration() throws Exception {
138         storeTopo(LogicalDatastoreType.CONFIGURATION);
139         checkPresentConfiguration(getDataBroker(), this.topologyIIdKeyed);
140     }
141
142     @Test
143     public void testCheckPresentOperational() throws Exception {
144         storeTopo(LogicalDatastoreType.OPERATIONAL);
145         checkPresentOperational(getDataBroker(), this.topologyIIdKeyed);
146     }
147
148     @Test(expected = AssertionError.class)
149     public void testCheckNotPresentOperationalFail() throws Exception {
150         storeTopo(LogicalDatastoreType.OPERATIONAL);
151         checkNotPresentOperational(getDataBroker(), this.topologyIIdKeyed);
152     }
153
154     @Test
155     public void testCheckNotPresentOperational() throws Exception {
156         checkNotPresentOperational(getDataBroker(), this.topologyIIdKeyed);
157     }
158
159     @Test
160     public void testCheckNotPresentConfiguration() throws Exception {
161         checkNotPresentConfiguration(getDataBroker(), this.topologyIIdKeyed);
162     }
163
164     @Test(expected = AssertionError.class)
165     public void testCheckEquals() throws Exception {
166         checkEquals(Assert::fail, TIMEOUT);
167     }
168
169     @Test(expected = AssertionError.class)
170     public void testCheckReceivedMessagesNotEqual() {
171         doReturn(0).when(this.listenerCheck).getListMessageSize();
172         checkReceivedMessages(this.listenerCheck, 1, TIMEOUT);
173     }
174
175     @Test
176     public void testCheckReceivedMessagesEqual() {
177         doReturn(1).when(this.listenerCheck).getListMessageSize();
178         checkReceivedMessages(this.listenerCheck, 1, TIMEOUT);
179     }
180 }