76cf7bf45cdc81912219ea0294d78565e9a7f672
[nemo.git] / nemo-impl / src / test / java / org / opendaylight / nemo / user / vnspacemanager / structurestyle / updateintent / UpdateNodeTest.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.user.vnspacemanager.structurestyle.updateintent;
9
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;
51
52 import java.lang.reflect.Field;
53 import java.lang.reflect.Method;
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Map;
57 import java.util.concurrent.ExecutionException;
58 import java.util.concurrent.TimeUnit;
59 import java.util.concurrent.TimeoutException;
60
61 import java.util.LinkedList;
62 import org.junit.Assert;
63 import org.junit.Before;
64 import org.junit.Test;
65 import java.util.*;
66 import java.util.List;
67
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;
73
74 import com.google.common.util.concurrent.CheckedFuture;
75
76 /**
77  * Created by ldzd11 on 2015/12/18.
78  */
79 public class UpdateNodeTest {
80
81     private UpdateNode updateNode;
82     private Node node;
83     private Node node2;
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;
103     private User user;
104     private NodeId nodeId;
105     private NodeKey nodeKey;
106     private Objects objects;
107     @org.junit.Before
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);
116
117         propertyName = mock(PropertyName.class);
118         propertyName2 = mock(PropertyName.class);
119
120         property = mock(Property.class);
121         propertyList = new ArrayList<Property>(1);
122         propertyList.add(property);
123
124         stringValue = mock(StringValue.class);
125         stringValues = new ArrayList<StringValue>(1);
126         stringValues.add(stringValue);
127         nodeType = mock(NodeType.class);
128
129         propertyDefinition=  mock(PropertyDefinition.class);
130         propertyDefinitions = new ArrayList<PropertyDefinition>(1);
131         propertyDefinitions.add(propertyDefinition);
132
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);
139
140         nodeList = new ArrayList<Node>(1);
141         nodeList.add(node2);
142         writeTransaction = mock(WriteTransaction.class);
143
144
145
146         updateNode = new UpdateNode(dataBroker,tenantManage);
147         nodeType2 = mock(NodeType.class);
148
149
150
151     }
152
153     @org.junit.Test
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);
160
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);
167
168
169         List<NodeDefinition> nodeDefinitionList = new ArrayList<NodeDefinition>();
170         NodeDefinition nodeDefinition = mock(NodeDefinition.class);
171         when(nodeDefinition.getNodeType()).thenReturn(nodeType);
172         nodeDefinitionList.add(nodeDefinition);
173
174         field1.set(field.get(updateNode), nodeDefinitionList);
175         Assert.assertTrue((List<NodeDefinition>) field1.get(field.get(updateNode)) == nodeDefinitionList);
176
177         when(node.getNodeType()).thenReturn(nodeType);
178         when(node.getProperty()).thenReturn(null);
179         when(nodeDefinition.getPropertyDefinition()).thenReturn(null);
180
181
182
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.")
194
195     }
196     @Test
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);
201
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.");
210
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.");
216
217
218     }
219
220 @Test
221     public void testcheckDefinition() throws Exception {
222
223         Class<UpdateNode> class3 = UpdateNode.class;
224         Method method = class3.getDeclaredMethod("checkDefinition", new Class[]{Node.class});
225         method.setAccessible(true);
226
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);
231
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);
237
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);
244
245
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);
250
251         when(node.getNodeType()).thenReturn(nodeType);
252         when(node.getProperty()).thenReturn(propertyList);
253         when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitions);
254
255         when(property.getPropertyName()).thenReturn(propertyName);
256         when(propertyDefinition.getPropertyName()).thenReturn(propertyName2);
257
258         when(property.getPropertyName().getValue()).thenReturn(new String("propertyname"));
259         Assert.assertEquals(method.invoke(updateNode, node), "The property propertyname has not been defined.");
260
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>();
265
266         when(node.getProperty()).thenReturn(propertyListnull);
267         when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitionListnull);
268         Assert.assertEquals(method.invoke(updateNode, node), null);
269
270         field1.set(field.get(updateNode), nodeDefinitionListnull);
271         Assert.assertEquals(method.invoke(updateNode, node), "This type of Node has not been defined.");
272
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.");
276
277
278 /*
279         (String)method.invoke(updateNode,node)*/
280     }
281
282     @Test
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.");
291
292     }
293
294
295     @Test
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);
300
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.");
308
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.");
314
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.");
319
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.");
324
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.");
329
330     }
331 }