Add nemo-impl partical test files
[nemo.git] / nemo-renderers / cli-renderer / src / test / java / org / opendaylight / nemo / renderer / cli / physicalnetwork / DataBrokerAdapterTest.java
1 /*
2  * Copyright (c) 2015 Huawei, Inc. 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.nemo.renderer.cli.physicalnetwork;
9
10 import com.google.common.util.concurrent.CheckedFuture;
11 import junit.framework.TestCase;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey;
23
24 import java.lang.reflect.Method;
25
26 import static org.mockito.Mockito.*;
27
28 /**
29  * Created by zhangmeng on 2015/11/30.
30  */
31 public class DataBrokerAdapterTest extends TestCase {
32     private DataBrokerAdapter dataBrokerAdapter;
33     private DataBroker dataBroker;
34     @Override
35     @Before
36     public void setUp() throws Exception {
37         dataBroker = mock(DataBroker.class);
38         dataBrokerAdapter = new DataBrokerAdapter(dataBroker);
39     }
40
41     @Test
42     public void testGetPhysicalHostIdentifier() throws Exception {
43         PhysicalHostKey physicalHostKey = mock(PhysicalHostKey.class);
44         Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
45         Method method  = class1.getDeclaredMethod("getPhysicalHostIdentifier", new Class[]{PhysicalHostKey.class});
46
47         method.setAccessible(true);
48         Object object =
49                 method.invoke(dataBrokerAdapter, new Object[]{physicalHostKey = mock(PhysicalHostKey.class)});
50         Assert.assertTrue(object != null);
51     }
52
53     @Test
54     public void testGetPhysicalNodeIdentifier() throws Exception {
55         PhysicalNodeKey physicalNodeKey = mock(PhysicalNodeKey.class);
56         Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
57         Method method = class1.getDeclaredMethod("getPhysicalNodeIdentifier",new Class[]{PhysicalNodeKey.class});
58
59         method.setAccessible(true);
60         Object object = method.invoke(dataBrokerAdapter,physicalNodeKey);
61         Assert.assertTrue(object != null);
62
63     }
64
65     @Test
66     public void testGetPhysicalLinkIdentifier() throws Exception {
67         PhysicalLinkKey physicalLinkKey  = mock(PhysicalLinkKey.class);
68         Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
69         Method method = class1.getDeclaredMethod("getPhysicalLinkIdentifier",new Class[]{PhysicalLinkKey.class});
70
71         method.setAccessible(true);
72         Object object = method.invoke(dataBrokerAdapter,physicalLinkKey);
73         Assert.assertTrue(object != null);
74     }
75
76     @Test
77     public void testAddPhysicalHost() throws Exception {
78         final PhysicalHost physicalHost = mock(PhysicalHost.class);
79         WriteTransaction writeTransaction = mock(WriteTransaction.class);
80         PhysicalHostKey physicalHostKey = mock(PhysicalHostKey.class);
81         CheckedFuture checkedFuture = mock(CheckedFuture.class);
82
83         Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
84         Method method = class1.getDeclaredMethod("addPhysicalHost",new Class[]{PhysicalHost.class});
85         method.setAccessible(true);
86
87         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
88         when(physicalHost.getKey()).thenReturn(physicalHostKey);
89         when(writeTransaction.submit()).thenReturn(checkedFuture);
90
91         method.invoke(dataBrokerAdapter, physicalHost);
92
93         verify(dataBroker).newWriteOnlyTransaction();
94         verify(physicalHost).getKey();
95         verify(writeTransaction).submit();
96     }
97     @Test
98     public void testAddPhysicalNode() throws Exception {
99         final PhysicalNode physicalNode = mock(PhysicalNode.class);
100         WriteTransaction writeTransaction = mock(WriteTransaction.class);
101         PhysicalNodeKey physicalNodeKey = mock(PhysicalNodeKey.class);
102         CheckedFuture checkedFuture = mock(CheckedFuture.class);
103
104         Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
105         Method method = class1.getDeclaredMethod("addPhysicalNode",new Class[]{PhysicalNode.class});
106         method.setAccessible(true);
107
108         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
109         when(physicalNode.getKey()).thenReturn(physicalNodeKey);
110         when(writeTransaction.submit()).thenReturn(checkedFuture);
111
112 //        boolean b = false;
113         Assert.assertNotNull(method.invoke(dataBrokerAdapter, physicalNode));
114
115         verify(dataBroker).newWriteOnlyTransaction();
116         verify(physicalNode).getKey();
117         verify(writeTransaction).submit();
118
119     }
120     @Test
121     public void testRemovePhysicalNode() throws Exception {
122         final PhysicalNodeKey nodeKey = mock(PhysicalNodeKey.class);
123         WriteTransaction writeTransaction = mock(WriteTransaction.class);
124         CheckedFuture checkedFuture = mock(CheckedFuture.class);
125
126         Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
127         Method method = class1.getDeclaredMethod("removePhysicalNode",new Class[]{PhysicalNodeKey.class});
128         method.setAccessible(true);
129
130         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
131         when(writeTransaction.submit()).thenReturn(checkedFuture);
132
133         Assert.assertNotNull(method.invoke(dataBrokerAdapter, nodeKey));
134
135         verify(dataBroker).newWriteOnlyTransaction();
136         verify(writeTransaction).submit();
137
138     }
139     @Test
140     public void testAddPhysicalLink() throws Exception {
141         final PhysicalLink physicalLink = mock(PhysicalLink.class);
142         WriteTransaction writeTransaction = mock(WriteTransaction.class);
143         CheckedFuture checkedFuture = mock(CheckedFuture.class);
144         PhysicalLinkKey physicalLinkKey = mock(PhysicalLinkKey.class);
145
146         Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
147         Method method = class1.getDeclaredMethod("addPhysicalLink",new Class[]{PhysicalLink.class});
148         method.setAccessible(true);
149
150         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
151         when(physicalLink.getKey()).thenReturn(physicalLinkKey);
152         when(writeTransaction.submit()).thenReturn(checkedFuture);
153
154         method.invoke(dataBrokerAdapter, physicalLink);
155
156         verify(dataBroker).newWriteOnlyTransaction();
157         verify(physicalLink).getKey();
158         verify(writeTransaction).submit();
159     }
160
161     @Test
162     public void testRemovePhysicalLink() throws Exception {
163         final PhysicalLinkKey physicalLinkKey = mock(PhysicalLinkKey.class);
164         WriteTransaction writeTransaction = mock(WriteTransaction.class);
165         CheckedFuture checkedFuture = mock(CheckedFuture.class);
166
167         Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
168         Method method = class1.getDeclaredMethod("removePhysicalLink", new Class[]{PhysicalLinkKey.class});
169         method.setAccessible(true);
170
171         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
172         when(writeTransaction.submit()).thenReturn(checkedFuture);
173
174         method.invoke(dataBrokerAdapter, physicalLinkKey);
175
176         verify(dataBroker).newWriteOnlyTransaction();
177         verify(writeTransaction).submit();
178
179     }
180
181 }