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.user.vnspacemanager.structurestyle.updateintent;
10 import org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent.UpdateNode;
11 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.SettableFuture;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.definitions.NodeDefinition;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Node;
25 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
26 import org.opendaylight.nemo.user.vnspacemanager.languagestyle.NEMOConstants;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.NodeType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.Users;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.NodeBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.UserKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.NodeDefinitions;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.definitions.NodeDefinition;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.Property;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.StringValue;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.definitions.NodeDefinition;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.PropertyName;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues;
45 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.NodeName;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.NodeKey;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import java.lang.reflect.Field;
53 import java.lang.reflect.Method;
54 import java.util.HashMap;
55 import java.util.List;
57 import java.util.concurrent.ExecutionException;
58 import java.util.concurrent.TimeUnit;
59 import java.util.concurrent.TimeoutException;
61 import java.util.LinkedList;
62 import org.junit.Assert;
63 import org.junit.Before;
64 import org.junit.Test;
66 import java.util.List;
68 import static org.mockito.Matchers.any;
69 import static org.mockito.Mockito.mock;
70 import static org.mockito.Mockito.times;
71 import static org.mockito.Mockito.verify;
72 import static org.mockito.Mockito.when;
74 import com.google.common.util.concurrent.CheckedFuture;
77 * Created by ldzd11 on 2015/12/18.
79 public class UpdateNodeTest {
81 private UpdateNode updateNode;
84 private NodeName nodeName;
85 private NodeName nodeName2;
86 private List<Node> nodeList;
87 private DataBroker dataBroker;
88 private TenantManage tenantManage;
89 private Property property;
90 private List<Property> propertyList;
91 private PropertyName propertyName;
92 private PropertyName propertyName2;
93 private StringValue stringValue;
94 private List<StringValue> stringValues;
95 private PropertyValues propertyValues;
96 private ValueCheck valueCheck;
97 private PropertyDefinition propertyDefinition;
98 private NodeType nodeType;
99 private NodeType nodeType2;
100 private List<PropertyDefinition> propertyDefinitions;
101 private UserId userId;
102 private WriteTransaction writeTransaction;
104 private NodeId nodeId;
105 private NodeKey nodeKey;
106 private Objects objects;
108 public void setUp() throws Exception {
109 dataBroker = mock(DataBroker.class);
110 tenantManage = mock(TenantManage.class);
111 propertyValues = mock(PropertyValues.class);
112 valueCheck = mock(ValueCheck.class);
113 nodeId = mock(NodeId.class);
114 nodeName = mock(NodeName.class);
115 nodeName2 = mock(NodeName.class);
117 propertyName = mock(PropertyName.class);
118 propertyName2 = mock(PropertyName.class);
120 property = mock(Property.class);
121 propertyList = new ArrayList<Property>(1);
122 propertyList.add(property);
124 stringValue = mock(StringValue.class);
125 stringValues = new ArrayList<StringValue>(1);
126 stringValues.add(stringValue);
127 nodeType = mock(NodeType.class);
129 propertyDefinition= mock(PropertyDefinition.class);
130 propertyDefinitions = new ArrayList<PropertyDefinition>(1);
131 propertyDefinitions.add(propertyDefinition);
133 node = mock(Node.class);
134 node2 = mock(Node.class);
135 userId = mock(UserId.class);
136 user = mock(User.class);
137 objects = mock(Objects.class);
138 nodeKey = mock(NodeKey.class);
140 nodeList = new ArrayList<Node>(1);
142 writeTransaction = mock(WriteTransaction.class);
146 updateNode = new UpdateNode(dataBroker,tenantManage);
147 nodeType2 = mock(NodeType.class);
154 public void testNodeHandling() throws Exception {
155 //into checkDefinition
156 CheckedFuture connectiondefinitionFuture = mock(CheckedFuture.class);
157 ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
158 when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
159 when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(connectiondefinitionFuture);
161 Class<UpdateNode> class1 = UpdateNode.class;
162 Class<GetDefinitions> class2 = GetDefinitions.class;
163 Field field = class1.getDeclaredField("getDefinitions");
164 field.setAccessible(true);
165 Field field1 = class2.getDeclaredField("nodeDefinitionList");
166 field1.setAccessible(true);
169 List<NodeDefinition> nodeDefinitionList = new ArrayList<NodeDefinition>();
170 NodeDefinition nodeDefinition = mock(NodeDefinition.class);
171 when(nodeDefinition.getNodeType()).thenReturn(nodeType);
172 nodeDefinitionList.add(nodeDefinition);
174 field1.set(field.get(updateNode), nodeDefinitionList);
175 Assert.assertTrue((List<NodeDefinition>) field1.get(field.get(updateNode)) == nodeDefinitionList);
177 when(node.getNodeType()).thenReturn(nodeType);
178 when(node.getProperty()).thenReturn(null);
179 when(nodeDefinition.getPropertyDefinition()).thenReturn(null);
183 when(tenantManage.getUser()).thenReturn(null);
184 //error = null continue
185 when(node.getNodeId()).thenReturn(nodeId);
186 when(node.getKey()).thenReturn(nodeKey);
187 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
188 CheckedFuture<Void, TransactionCommitFailedException> f;
189 f=mock(CheckedFuture.class);
190 when(writeTransaction.submit()).thenReturn(f);
191 Assert.assertEquals(updateNode.NodeHandling(userId, node), null);
192 //verify(node).getProperty();
193 //Assert.assertEquals(updateNode.NodeHandling(userId,node),"The property propertyname has not been defined.")
197 public void testcheckInstance() throws Exception {
198 Class<UpdateNode> class1 = UpdateNode.class;
199 Method method = class1.getDeclaredMethod("checkInstance", new Class[]{UserId.class, Node.class});
200 method.setAccessible(true);
202 when(tenantManage.getUser()).thenReturn(user);
203 when(user.getObjects()).thenReturn(objects);
204 when(user.getObjects().getNode()).thenReturn(nodeList);
205 when(node.getNodeId()).thenReturn(nodeId);
206 when(node2.getNodeId()).thenReturn(nodeId);
207 when(node.getNodeName()).thenReturn(nodeName);
208 when(node2.getNodeName()).thenReturn(nodeName2);
209 Assert.assertEquals(method.invoke(updateNode, userId, node), "The node name should not be changed.");
211 when(node.getNodeName()).thenReturn(nodeName);
212 when(node2.getNodeName()).thenReturn(nodeName);
213 when(node.getNodeType()).thenReturn(nodeType);
214 when(node2.getNodeType()).thenReturn(nodeType2);
215 Assert.assertEquals(method.invoke(updateNode, userId, node), "The node type should not be changed.");
221 public void testcheckDefinition() throws Exception {
223 Class<UpdateNode> class3 = UpdateNode.class;
224 Method method = class3.getDeclaredMethod("checkDefinition", new Class[]{Node.class});
225 method.setAccessible(true);
227 CheckedFuture nodedefinitionFuture = mock(CheckedFuture.class);
228 ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
229 when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
230 when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(nodedefinitionFuture);
232 List<NodeDefinition> nodeDefinitionList = new ArrayList<NodeDefinition>();
233 List<NodeDefinition> nodeDefinitionListnull = new ArrayList<NodeDefinition>();
234 NodeDefinition nodeDefinition = mock(NodeDefinition.class);
235 nodeDefinitionList.add(nodeDefinition);
236 when(nodeDefinition.getNodeType()).thenReturn(nodeType);
238 Class<UpdateNode> class1 = UpdateNode.class;
239 Class<GetDefinitions> class2 = GetDefinitions.class;
240 Field field = class1.getDeclaredField("getDefinitions");
241 field.setAccessible(true);
242 Field field1 = class2.getDeclaredField("nodeDefinitionList");
243 field1.setAccessible(true);
246 System.out.println(field1.get(field.get(updateNode)));
247 field1.set(field.get(updateNode), nodeDefinitionList);
248 System.out.println(field1.get(field.get(updateNode)));
249 Assert.assertTrue((List<NodeDefinition>) field1.get(field.get(updateNode)) == nodeDefinitionList);
251 when(node.getNodeType()).thenReturn(nodeType);
252 when(node.getProperty()).thenReturn(propertyList);
253 when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitions);
255 when(property.getPropertyName()).thenReturn(propertyName);
256 when(propertyDefinition.getPropertyName()).thenReturn(propertyName2);
258 when(property.getPropertyName().getValue()).thenReturn(new String("propertyname"));
259 Assert.assertEquals(method.invoke(updateNode, node), "The property propertyname has not been defined.");
261 field1.set(field.get(updateNode), nodeDefinitionList);
262 when(node.getNodeType()).thenReturn(nodeType);
263 List<Property> propertyListnull = new ArrayList<Property>();
264 List<PropertyDefinition> propertyDefinitionListnull = new ArrayList<PropertyDefinition>();
266 when(node.getProperty()).thenReturn(propertyListnull);
267 when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitionListnull);
268 Assert.assertEquals(method.invoke(updateNode, node), null);
270 field1.set(field.get(updateNode), nodeDefinitionListnull);
271 Assert.assertEquals(method.invoke(updateNode, node), "This type of Node has not been defined.");
273 field1.set(field.get(updateNode), nodeDefinitionList);
274 when(node.getNodeType()).thenReturn(nodeType2);
275 Assert.assertEquals(method.invoke(updateNode, node), "This type of Node has not been defined.");
279 (String)method.invoke(updateNode,node)*/
283 public void testcheckProperty() throws Exception {
284 Class<UpdateNode> class1 = UpdateNode.class;
285 Method method = class1.getDeclaredMethod("checkProperty", new Class[]{List.class, List.class});
286 method.setAccessible(true);
287 when(property.getPropertyName()).thenReturn(propertyName);
288 when(propertyDefinition.getPropertyName()).thenReturn(propertyName2);
289 when(property.getPropertyName().getValue()).thenReturn(new String("propertyname"));
290 Assert.assertEquals(method.invoke(updateNode, propertyList,propertyDefinitions), "The property propertyname has not been defined.");
296 public void testcheckPredefine() throws Exception {
297 Class<UpdateNode> class1 = UpdateNode.class;
298 Method method = class1.getDeclaredMethod("checkPredefine", new Class[]{List.class});
299 method.setAccessible(true);
301 when(property.getPropertyName()).thenReturn(propertyName);
302 when(property.getPropertyName().getValue()).thenReturn(new String("mac-address"));
303 when(property.getPropertyValues()).thenReturn(propertyValues);
304 when(property.getPropertyValues().getStringValue()).thenReturn(stringValues);
305 when(stringValue.getValue()).thenReturn(new String("test"));
306 when(valueCheck.checkMac(stringValue.getValue())).thenReturn(false);
307 Assert.assertEquals(method.invoke(updateNode, propertyList), "The mac-address is not legal.");
309 when(property.getPropertyName().getValue()).thenReturn(new String("ip-address"));
310 when(property.getPropertyValues().getStringValue()).thenReturn(stringValues);
311 when(stringValue.getValue()).thenReturn(new String("/"));
312 when(valueCheck.checkIpPrefix(stringValue.getValue())).thenReturn(false);
313 Assert.assertEquals(method.invoke(updateNode, propertyList), "The ip-address is not legal.");
315 when(property.getPropertyName().getValue()).thenReturn(new String("gateway-ip"));
316 when(property.getPropertyValues().getStringValue()).thenReturn(stringValues);
317 when(valueCheck.checkIpAddress(stringValue.getValue())).thenReturn(false);
318 Assert.assertEquals(method.invoke(updateNode, propertyList), "The gateway-ip is not legal.");
320 when(property.getPropertyName().getValue()).thenReturn(new String("ac-info-network"));
321 when(property.getPropertyValues().getStringValue()).thenReturn(stringValues);
322 when(stringValue.getValue()).thenReturn(new String("layer4"));
323 Assert.assertEquals(method.invoke(updateNode, propertyList), "The ac-info-network is not legal.");
325 when(property.getPropertyName().getValue()).thenReturn(new String("operating-mode"));
326 when(property.getPropertyValues().getStringValue()).thenReturn(stringValues);
327 when(stringValue.getValue()).thenReturn(new String("layer4"));
328 Assert.assertEquals(method.invoke(updateNode, propertyList), "The operating-mode is not legal.");