c3c349fe7af4e59bc08416ddd6a5a5334cd181f9
[nemo.git] / nemo-impl / src / test / java / org / opendaylight / nemo / user / vnspacemanager / languagestyle / updateintentlang / UpdateConnectionLangTest.java
1 /*\r
2  * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.nemo.user.vnspacemanager.languagestyle.updateintentlang;\r
9 \r
10 import junit.framework.TestCase;\r
11 import org.junit.Assert;\r
12 import org.junit.Before;\r
13 import org.junit.Test;\r
14 import static org.mockito.Mockito.*;\r
15 import static org.junit.Assert.*;\r
16 import java.util.ArrayList;\r
17 import java.util.List;\r
18 import java.util.HashMap;\r
19 import java.util.LinkedHashMap;\r
20 import java.lang.reflect.Method; \r
21 import java.util.*;\r
22 \r
23 \r
24 import org.opendaylight.nemo.user.tenantmanager.TenantManage;\r
25 import org.opendaylight.nemo.user.vnspacemanager.languagestyle.NEMOConstants;\r
26 import org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent.UpdateConnection;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.*;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.ConnectionBuilder;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.ConnectionKey;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Node;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.*;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValuesBuilder;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.*;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.NodeName;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConnectionName;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConnectionType;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConnectionId;\r
41 import org.opendaylight.controller.md.sal.binding.api.DataBroker;\r
42 \r
43 \r
44 import java.util.ArrayList;\r
45 import java.util.List;\r
46 import java.util.HashMap;\r
47 import java.util.LinkedHashMap;\r
48 \r
49 import  java.io.*;\r
50 \r
51 /**\r
52  * Created by Thomas Liu on 2015/12/14.\r
53  */\r
54 public class UpdateConnectionLangTest extends TestCase {\r
55     private UpdateConnectionLang updateConnectionLangTest;\r
56     private DataBroker dataBroker;\r
57     private TenantManage tenantManage;\r
58     private UserId userId;\r
59     private String connectionname;\r
60     private String connectiontype;\r
61     private List<String> endnodes;\r
62     private LinkedHashMap<String, LinkedHashMap<String,String>> propertyList;\r
63     private LinkedHashMap<String,String> properties;\r
64 \r
65     //createConnection\r
66     private User user;\r
67     private Objects object;\r
68     private List<Node> nodeList;\r
69     private List<Connection> connList;\r
70     private ConnectionName connectionName;\r
71     private ConnectionId connectionId;\r
72     private NodeName nodeName;\r
73     private NodeId nodeId;\r
74 \r
75     @org.junit.Before\r
76     public void setUp() throws Exception {\r
77         dataBroker = mock(DataBroker.class);\r
78         tenantManage = mock(TenantManage.class);\r
79         updateConnectionLangTest = new UpdateConnectionLang(dataBroker,tenantManage);\r
80         userId = mock(UserId.class);\r
81         propertyList = new LinkedHashMap<String, LinkedHashMap<String,String>>();\r
82         connectionname = new String("conn1");\r
83         connectiontype = new String("p2p");\r
84         endnodes = new ArrayList<String>(){{add(new String("123"));}};\r
85 \r
86         //createConnection\r
87         user = mock(User.class);\r
88         object = mock(Objects.class);\r
89         nodeList = new ArrayList<Node>(){{add(mock(Node.class));}};\r
90         connList = new ArrayList<Connection>(){{add(mock(Connection.class));}};\r
91         connectionName = mock(ConnectionName.class);\r
92         connectionId = mock(ConnectionId.class);\r
93         nodeName = mock(NodeName.class);\r
94         nodeId = mock(NodeId.class);\r
95 \r
96 \r
97 \r
98     }\r
99 \r
100     @org.junit.Test\r
101     public void testConnectionHandling() throws Exception {\r
102 \r
103 \r
104         //errorInfo == null\r
105         properties = new LinkedHashMap<String,String>(){{\r
106             put(new String("1"),NEMOConstants.range);\r
107             put(new String("group"),NEMOConstants.string);\r
108             //put(new String("100"),NEMOConstants.integer);\r
109             put(new String("100,200"),NEMOConstants.range);\r
110         }};\r
111         propertyList.put(new String("p1"),properties);\r
112         //createConnection()\r
113         //userId,connectionname,connectiontype,endnodes,propertyList\r
114         doNothing().when(tenantManage).fetchVNSpace(userId);\r
115         when(tenantManage.getUser()).thenReturn(user);\r
116         when(user.getObjects()).thenReturn(object);\r
117         when(object.getNode()).thenReturn(nodeList);\r
118         when(object.getNode()).thenReturn(nodeList);\r
119         when(object.getConnection()).thenReturn(connList);\r
120         when(object.getConnection()).thenReturn(connList);\r
121 \r
122         //connection\r
123         //connection exists\r
124         when(connList.get(0).getConnectionName()).thenReturn(connectionName);\r
125         when(connectionName.getValue()).thenReturn(new String("conn1"));\r
126         when(connList.get(0).getConnectionId()).thenReturn(connectionId);\r
127 \r
128         //endnodes\r
129         when(nodeList.get(0).getNodeName()).thenReturn(nodeName);\r
130         when(nodeName.getValue()).thenReturn(new String("node1"));\r
131         when(nodeList.get(0).getNodeId()).thenReturn(nodeId);\r
132         when(nodeList.get(0).getNodeId()).thenReturn(nodeId);\r
133 \r
134         //propertylist\r
135         updateConnectionLangTest.ConnectionHandling(userId, connectionname, connectiontype, endnodes, propertyList);\r
136 \r
137         properties = new LinkedHashMap<String,String>(){{\r
138             //put(new String("1"),NEMOConstants.range);\r
139             //put(new String("group"),NEMOConstants.string);\r
140             //put(new String("100"),NEMOConstants.integer);\r
141             put(new String("100,200"),NEMOConstants.range);\r
142         }};\r
143         propertyList.put(new String("p1"),properties);\r
144         //createConnection()\r
145         //userId,connectionname,connectiontype,endnodes,propertyList\r
146         doNothing().when(tenantManage).fetchVNSpace(userId);\r
147         when(tenantManage.getUser()).thenReturn(user);\r
148         when(user.getObjects()).thenReturn(object);\r
149         when(object.getNode()).thenReturn(nodeList);\r
150         when(object.getNode()).thenReturn(nodeList);\r
151         when(object.getConnection()).thenReturn(connList);\r
152         when(object.getConnection()).thenReturn(connList);\r
153 \r
154         //connection\r
155         //connection exists\r
156         when(connList.get(0).getConnectionName()).thenReturn(connectionName);\r
157         when(connectionName.getValue()).thenReturn(new String("conn1"));\r
158         when(connList.get(0).getConnectionId()).thenReturn(connectionId);\r
159 \r
160         //endnodes\r
161         when(nodeList.get(0).getNodeName()).thenReturn(nodeName);\r
162         when(nodeName.getValue()).thenReturn(new String("node1"));\r
163         when(nodeList.get(0).getNodeId()).thenReturn(nodeId);\r
164         when(nodeList.get(0).getNodeId()).thenReturn(nodeId);\r
165 \r
166         //propertylist\r
167         updateConnectionLangTest.ConnectionHandling(userId, connectionname, connectiontype, endnodes, propertyList);\r
168 \r
169         /*connList = new ArrayList<Connection>();\r
170         endnodes = new ArrayList<String>();\r
171         properties = new LinkedHashMap<String,String>(){{\r
172             //put(new String("1"),NEMOConstants.range);\r
173             //put(new String("group"),NEMOConstants.string);\r
174             //put(new String("100"),NEMOConstants.integer);\r
175             put(new String("100,200"),NEMOConstants.range);\r
176         }};\r
177         propertyList.put(new String("p1"),properties);\r
178         //createConnection()\r
179         //userId,connectionname,connectiontype,endnodes,propertyList\r
180         doNothing().when(tenantManage).fetchVNSpace(userId);\r
181         when(tenantManage.getUser()).thenReturn(user);\r
182         when(user.getObjects()).thenReturn(object);\r
183         when(object.getNode()).thenReturn(nodeList);\r
184         when(object.getNode()).thenReturn(nodeList);\r
185         when(object.getConnection()).thenReturn(connList);\r
186         when(object.getConnection()).thenReturn(connList);\r
187 \r
188 \r
189         updateConnectionLangTest.ConnectionHandling(userId, connectionname, connectiontype, endnodes, propertyList);*/\r
190 \r
191 \r
192         properties = new LinkedHashMap<String,String>(){{\r
193             //put(new String("1"),NEMOConstants.range);\r
194             //put(new String("group"),NEMOConstants.string);\r
195             //put(new String("100"),NEMOConstants.integer);\r
196             //put(new String("100,200"),NEMOConstants.range);\r
197         }};\r
198         propertyList.put(new String("p1"),properties);\r
199         //createConnection()\r
200         //userId,connectionname,connectiontype,endnodes,propertyList\r
201 \r
202         //updateConnectionLangTest.ConnectionHandling(userId, connectionname, connectiontype, endnodes, propertyList);\r
203 \r
204 \r
205 \r
206     }\r
207         @org.junit.Test\r
208         public void createConnectionTest() throws Exception{\r
209      UserId userId1=mock(UserId.class);\r
210          String connectionname1;\r
211          String connectiontype1;\r
212          connectionname1 = new String("conn1");\r
213      connectiontype1 = new String("p2p");\r
214          List<String> endnodes1=null;\r
215          LinkedHashMap<String, LinkedHashMap<String,String>> propertyList1=null;\r
216          Class<?>[] args=new Class<?>[5];\r
217                 args[0]=userId1.getClass();\r
218                 args[1]=connectionname1.getClass();\r
219                 args[2]=connectiontype1.getClass();\r
220                 args[3]=endnodes1.getClass();\r
221                 args[4]=propertyList1.getClass();\r
222                 Object[] args1 = new Object[4]; \r
223                 for(int i=0;i<5;i++)\r
224             args1[i]=new Object();\r
225                 args1[0]=userId1;\r
226                 args1[1]=connectionname1;\r
227                 args1[2]=connectiontype1;\r
228                 args1[3]=endnodes1;\r
229                 args1[4]=propertyList1;\r
230         Method methon=updateConnectionLangTest.getClass().getDeclaredMethod("createConnection",args);\r
231         methon.setAccessible(true);\r
232         //branch 1 \r
233          when(tenantManage.getObjectId(userId1,connectionname1)).thenReturn(null);\r
234      Assert.assertNull(methon.invoke(updateConnectionLangTest,args1));\r
235         //branch 2 \r
236         Map<ConnectionId, Connection> map=new HashMap<ConnectionId,Connection>();\r
237         ConnectionId connectionId1=new ConnectionId("111111111111");\r
238         Connection connection1=mock(Connection.class);\r
239         map.put(connectionId1,connection1);\r
240         ConnectionKey connectionkey=mock(ConnectionKey.class);\r
241         when(tenantManage.getObjectId(userId1,connectionname1)).thenReturn("111111111111");\r
242         when(tenantManage.getObjectId(userId1,connectionname1)).thenReturn("111111111111");\r
243         when(tenantManage.getConnection(userId1)).thenReturn(map);\r
244     when(connection1.getKey()).thenReturn(connectionkey);\r
245         when(connection1.getConnectionId()).thenReturn(connectionId1);\r
246         Assert.assertNull(methon.invoke(updateConnectionLangTest,args1));\r
247         //branch 3\r
248         when(tenantManage.getObjectId(userId1,connectionname1)).thenReturn(null);\r
249          endnodes1 = new ArrayList<String>(){{add(new String("123"));}};\r
250         args1[3]=endnodes1;\r
251         when(tenantManage.getObjectId(userId,"123")).thenReturn(null);\r
252         Assert.assertNotNull(methon.invoke(updateConnectionLangTest,args1));\r
253         //branch 4\r
254         when(tenantManage.getObjectId(userId1,connectionname1)).thenReturn(null);\r
255         propertyList1 = new LinkedHashMap<String, LinkedHashMap<String,String>>();\r
256         LinkedHashMap<String,String> properties1 = new LinkedHashMap<String,String>(){{\r
257             put(new String("1"),NEMOConstants.range);\r
258             put(new String("group"),NEMOConstants.string);\r
259             put(new String("100"),NEMOConstants.integer);\r
260             put(new String("100,200"),NEMOConstants.range);\r
261         }};\r
262     propertyList1.put(new String("p1"),properties1);\r
263         endnodes1=null;\r
264         args1[3]=endnodes1;\r
265         args1[4]=propertyList1;\r
266         Assert.assertNull(methon.invoke(updateConnectionLangTest,args1));\r
267         }\r
268 }