2 * Copyright (c) 2015 Huawei, Inc. 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.nemo.renderer.cli.physicalnetwork;
10 import junit.framework.TestCase;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
15 import static org.junit.Assert.*;
16 import com.fasterxml.jackson.databind.JsonNode;
17 import com.fasterxml.jackson.databind.ObjectMapper;
18 import com.google.common.collect.HashBasedTable;
19 import com.google.common.collect.Table;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNodeInstance;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalPortInstance;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.attribute.instance.AttributeValueBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.host.instance.IpAddressesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPort;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPortBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.Attribute;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.*;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import javax.xml.crypto.Data;
46 import java.io.IOException;
47 import java.lang.reflect.Member;
48 import java.lang.reflect.Method;
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.List;
53 import static org.mockito.Mockito.*;
56 * Created by zhangmeng on 2015/12/8.
58 public class PhysicalResourceLoaderTest extends TestCase {
59 private DataBroker dataBroker;
60 private PhysicalResourceLoader physicalResourceLoader;
62 public void setUp() throws Exception {
63 dataBroker = mock(DataBroker.class);
64 physicalResourceLoader = new PhysicalResourceLoader(dataBroker);
68 public void testGetPhysicalNode() throws Exception {
69 PhysicalNodeId physicalNodeId = mock(PhysicalNodeId.class);
70 PhysicalNode physicalNode ;
72 Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
73 Method method = class1.getDeclaredMethod("getPhysicalNode",new Class[]{PhysicalNodeId.class});
74 method.setAccessible(true);
76 physicalNode = (PhysicalNode) method.invoke(physicalResourceLoader,physicalNodeId);
78 Assert.assertTrue( physicalNode == null);
82 public void testGetPhysicalPort() throws Exception {
83 PhysicalPortId physicalPortId = mock(PhysicalPortId.class);
84 PhysicalPort physicalPort;
86 Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
87 Method method = class1.getDeclaredMethod("getPhysicalPort",new Class[]{PhysicalPortId.class});
88 method.setAccessible(true);
90 physicalPort = (PhysicalPort) method.invoke(physicalResourceLoader,physicalPortId);
92 Assert.assertTrue(physicalPort == null);
96 public void testGetPhysicalLink() throws Exception {
97 PhysicalLinkId physicalLinkId = mock(PhysicalLinkId.class);
98 PhysicalLink physicalLink;
100 Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
101 Method method = class1.getDeclaredMethod("getPhysicalLink",new Class[]{PhysicalLinkId.class});
102 method.setAccessible(true);
104 physicalLink = (PhysicalLink) method.invoke(physicalResourceLoader,physicalLinkId);
106 Assert.assertTrue(physicalLink == null);
110 public void testClose() throws Exception {
111 physicalResourceLoader.close();
115 public void testBuildHosts()throws Exception{
116 Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
117 Method method = class1.getDeclaredMethod("buildHosts", new Class[]{JsonNode.class});
118 method.setAccessible(true);
120 JsonNode hostsNode = mock(JsonNode.class);
121 JsonNode hosts = mock(JsonNode.class);
122 JsonNode host = mock(JsonNode.class);
123 JsonNode host_temp = mock(JsonNode.class);
124 JsonNode host_temp1 = mock(JsonNode.class);
125 JsonNode ipaddrs = mock(JsonNode.class);
126 JsonNode ipaddr = mock(JsonNode.class);
127 List<PhysicalHost> list;
129 when(hostsNode.path(any(String.class))).thenReturn(hosts);
130 when(hosts.size()).thenReturn(1);
131 when(hosts.get(any(Integer.class))).thenReturn(host);
132 //get into method "buildhost"
133 when(host.get(any(String.class))).thenReturn(host_temp);
134 when(host_temp.asText())
135 .thenReturn(new String("00001111-0000-0000-0000-000011112222")) //HOST_ID
136 .thenReturn(new String("hostName")) //HOST_NAME
137 .thenReturn(new String("00:11:22:33:44:55")) //MAC_ADDRESS
138 .thenReturn(new String("nodeId")) //NODE_ID
139 .thenReturn(new String("connetionId"));//PhysicalPortId
140 when(host.path(any(String.class))).thenReturn(ipaddrs);
141 when(ipaddrs.size()).thenReturn(1);
142 when(ipaddrs.get(any(Integer.class))).thenReturn(ipaddr);
143 when(ipaddr.get(any(String.class))).thenReturn(host_temp1);
144 when(host_temp1.asText()).thenReturn(new String("192.168.1.1"));//ipv4_address
147 list = (List<PhysicalHost>) method.invoke(physicalResourceLoader,hostsNode);
148 Assert.assertTrue(list.size() == 1);
152 public void testBuildPortAttributes()throws Exception{
153 Class<PhysicalResourceLoader>class1 = PhysicalResourceLoader.class;
154 Method method = class1.getDeclaredMethod("buildPortAttributes",new Class[]{JsonNode.class});
155 method.setAccessible(true);
157 List<Attribute> attributeList;
158 JsonNode attributes = mock(JsonNode.class);
159 JsonNode portAttribute = mock(JsonNode.class);
160 JsonNode jsonNode_temp = mock(JsonNode.class);
162 when(attributes.size()).thenReturn(1);
163 when(attributes.get(any(Integer.class))).thenReturn(portAttribute);
164 //get into method "buildPortAttribute"
165 when(portAttribute.path(any(String.class))).thenReturn(jsonNode_temp);
166 when(jsonNode_temp.asText())
167 .thenReturn(new String(""))//branch null
168 .thenReturn(new String("zm"));
169 attributeList = (List<Attribute>)method.invoke(physicalResourceLoader,attributes);
170 Assert.assertTrue(attributeList.size() == 0);
171 //new AttributeName("zm");
172 attributeList = (List<Attribute>)method.invoke(physicalResourceLoader,attributes);
173 Assert.assertTrue(attributeList.size() == 1);
174 verify(portAttribute,times(3)).path(any(String.class));
175 verify(jsonNode_temp,times(3)).asText();
179 public void testBuildLinks()throws Exception{
180 List<PhysicalLink> list;
181 Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
182 Method method = class1.getDeclaredMethod("buildLinks", new Class[]{JsonNode.class});
183 method.setAccessible(true);
185 JsonNode linksRoot = mock(JsonNode.class);
186 JsonNode links = mock(JsonNode.class);
187 JsonNode link = mock(JsonNode.class);
188 JsonNode link_temp_buildlink = mock(JsonNode.class);
190 when(linksRoot.path(any(String.class))).thenReturn(links);
191 when(links.size()).thenReturn(1);
192 when(links.get(any(Integer.class))).thenReturn(link);
193 //get into method "build link" args(link)
194 when(link.get(any(String.class))).thenReturn(link_temp_buildlink);
195 when(link_temp_buildlink.asText())
196 .thenReturn(new String("1"))//new PhysicalLinkId(strLinkId)
197 .thenReturn(new String("2"))//new PhysicalNodeId
198 .thenReturn(new String("3"))//new PhysicalPortId
199 .thenReturn(new String("4"))//new PhysicalNodeId
200 .thenReturn(new String("5"))//new PhysicalPortId
201 .thenReturn(new String(""));//linkNode.get("link-bandwidth").asText().equals("")
202 when(link_temp_buildlink.asLong()).thenReturn((long) 1);
204 list = (List<PhysicalLink>)method.invoke(physicalResourceLoader,linksRoot);
205 Assert.assertTrue(list.size() == 1);
206 verify(link_temp_buildlink,times(6)).asText();
210 public void testBuildNodes()throws Exception{
211 Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
212 Method method = class1.getDeclaredMethod("buildNodes", new Class[]{JsonNode.class});
213 method.setAccessible(true);
215 List<PhysicalNode> result = new ArrayList<PhysicalNode>();
216 JsonNode nodesRoot = mock(JsonNode.class);
217 JsonNode nodes = mock(JsonNode.class);
218 JsonNode node = mock(JsonNode.class);
219 JsonNode node_temp_buildnode = mock(JsonNode.class);
220 JsonNode ports = mock(JsonNode.class);
221 JsonNode attributes = mock(JsonNode.class);
222 JsonNode port = mock(JsonNode.class);
223 JsonNode port_temp_buildport = mock(JsonNode.class);
224 JsonNode portAttributes = mock(JsonNode.class);
225 JsonNode portAttribute = mock(JsonNode.class);
226 JsonNode port_temp_buildPortAttribute = mock(JsonNode.class);
227 JsonNode portAttribute_father = mock(JsonNode.class);
228 JsonNode attribute_temp_father = mock(JsonNode.class);
230 when(nodesRoot.path(any(String.class))).thenReturn(nodes);
231 when(nodes.size()).thenReturn(1);
232 when(nodes.get(any(Integer.class))).thenReturn(node);
233 //get into method "build Node"
234 when(node.get(any(String.class))).thenReturn(node_temp_buildnode);
235 when(node_temp_buildnode.asText())
236 .thenReturn(new String(""))//test null
237 .thenReturn(new String("test"))//node id
238 .thenReturn(new String("switch"));// node type
239 result = (List<PhysicalNode>) method.invoke(physicalResourceLoader,nodesRoot);
240 Assert.assertTrue(result.size() == 0);
241 verify(node_temp_buildnode).asText();
242 when(node.path(any(String.class)))
243 .thenReturn(ports)//get into method "build ports"
244 .thenReturn(attributes); //get into method "build attributes"
245 ////get into method"build ports"
246 when(ports.size()).thenReturn(1);
247 when(ports.get(any(Integer.class))).thenReturn(port);
248 //////get into method "build port"
249 when(port.get(any(String.class))).thenReturn(port_temp_buildport);
250 when(port_temp_buildport.asText())
251 .thenReturn(new String("test"))//port id
252 .thenReturn(new String("external"))//port type
253 .thenReturn(new String("00:11:22:33:44:55"))//if(!(port.get("port-mac-address").asText().equals(""))) get in
254 .thenReturn(new String("00:11:22:33:44:55"))//mac address
255 .thenReturn(new String(""));// if(port.get("bandwidth").asText().equals("")) get in
256 when(port.path(any(String.class))).thenReturn(portAttributes);
257 ////////get into method "buildPortAttributes" args(portAttributes)
258 when(portAttributes.size()).thenReturn(1);
259 when(portAttributes.get(any(Integer.class))).thenReturn(portAttribute);
260 ////////// get into method "buildPortAttribute" args(portAttribute)
261 when(portAttribute.path(any(String.class))).thenReturn(port_temp_buildPortAttribute);
262 when(port_temp_buildPortAttribute.asText())
263 .thenReturn(new String("test"))//ATTRIBUTE_NAME
264 .thenReturn(new String("test"));//ATTRIBUTE_VALUE
265 ////return to method "buildnode" and get into method "..............buildNodeAttributes"
266 when(attributes.size()).thenReturn(1);
267 when(attributes.get(any(Integer.class))).thenReturn(portAttribute_father);
268 //////get into method "..............buildNodeAttribute"
269 when(portAttribute_father.path(any(String.class))).thenReturn(attribute_temp_father);
270 when(attribute_temp_father.asText())
271 .thenReturn(new String("test"))//ATTRIBUTE_NAME
272 .thenReturn(new String("test"));//ATTRIBUTE_VALUE
273 result = (List<PhysicalNode>) method.invoke(physicalResourceLoader,nodesRoot); //return empty list
274 Assert.assertTrue(result.size() == 1);
275 verify(port_temp_buildport,times(5)).asText();
276 verify(attribute_temp_father,times(2)).asText();