Modified one test file in NemoUser
[nemo.git] / nemo-impl / src / test / java / org / opendaylight / nemo / user / vnspacemanager / structurestyle / updateintent / UpdateTemplateDefinitionTest.java
1 /*
2  * Copyright (c) 2016 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 junit.framework.TestCase;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.action.instance.ParameterValues;
15 import static org.junit.Assert.*;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
18 import org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent.GetDefinitions;
19 import org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent.UpdateTemplateDefinition;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.PropertyName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.match.item.instance.MatchItemValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.*;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.template.definitions.TemplateDefinition;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.definitions.ConnectionDefinition;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.EndNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.flow.instance.MatchItem;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.match.item.definitions.MatchItemDefinition;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.definitions.NodeDefinition;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.Property;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.SubNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.StringValue;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.action.definitions.ActionDefinition;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.condition.instance.ConditionSegment;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.condition.parameter.definitions.ConditionParameterDefinition;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.operation.instance.Action;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.definition.grouping.AbstractIntents;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.definition.grouping.TemplateParameter;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.definition.grouping._abstract.intents._abstract.objects.AbstractConnection;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.definition.grouping._abstract.intents._abstract.objects.AbstractFlow;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.definition.grouping._abstract.intents._abstract.objects.AbstractNode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.definition.grouping._abstract.intents._abstract.operations.AbstractOperation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConditionParameterName;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.condition.instance.condition.segment.ConditionParameterTargetValue;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.TemplateName;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.definition.grouping._abstract.intents.AbstractOperations;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.definition.grouping._abstract.intents.AbstractObjects;
49 import java.lang.reflect.Field;
50 import java.lang.reflect.Method;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Map;
55 import static org.mockito.Mockito.*;
56 /**
57  * Created by zhangmeng on 2015/12/28.
58  */
59 public class UpdateTemplateDefinitionTest extends TestCase {
60     private DataBroker dataBroker;
61     private TenantManage tenantManage;
62     private UpdateTemplateDefinition updateTemplateDefinition;
63     private Class<UpdateTemplateDefinition> class1;
64     private Field field;
65     private Method method;
66     @Before
67     public void setUp() throws Exception {
68         class1 = UpdateTemplateDefinition.class;
69         dataBroker = mock(DataBroker.class);
70         tenantManage = mock(TenantManage.class);
71
72         updateTemplateDefinition = new UpdateTemplateDefinition(dataBroker,tenantManage);
73     }
74
75     @Test
76     public void testCheckTemplateDefinition() throws Exception {
77         field = class1.getDeclaredField("getDefinitions");
78         field.setAccessible(true);
79
80         UserId userId = mock(UserId.class);
81         NodeId nodeId = mock(NodeId.class);
82         SubNode subNode = mock(SubNode.class);
83         EndNode endNode = mock(EndNode.class);
84         MatchItem matchItem = mock(MatchItem.class);
85         ObjectId objectId = mock(ObjectId.class);
86         AbstractFlow abstractFlow = mock(AbstractFlow.class);
87         TemplateName templateName = mock(TemplateName.class);
88         ParameterName parameterName = mock(ParameterName.class);
89         AbstractOperation abstractOperation = mock(AbstractOperation.class);
90         TemplateParameter templateParameter = mock(TemplateParameter.class);
91         TemplateDefinition templateDefinition = mock(TemplateDefinition.class);
92         AbstractNode abstractNode = mock(AbstractNode.class);
93         AbstractIntents abstractIntents = mock(AbstractIntents.class);
94         AbstractObjects abstractObjects = mock(AbstractObjects.class);
95         AbstractOperations abstractOperations = mock(AbstractOperations.class);
96         AbstractConnection abstractConnection = mock(AbstractConnection.class);
97         GetDefinitions getDefinitions = mock(GetDefinitions.class);
98         List<TemplateParameter> list = new ArrayList<TemplateParameter>();
99         List<AbstractNode> nodeList = new ArrayList<AbstractNode>();
100         List<SubNode> subNodeList = new ArrayList<SubNode>();
101         List<AbstractConnection> connectionList = new ArrayList<AbstractConnection>();
102         List<EndNode> endNodeList = new ArrayList<EndNode>();
103         List<AbstractFlow> flowList = new ArrayList<AbstractFlow>();
104         List<MatchItem> matchItemList =  new ArrayList<MatchItem>();
105         List<AbstractOperation> operationList = new ArrayList<AbstractOperation>();
106         Map<TemplateName, TemplateDefinition> map = mock(Map.class);
107         Map<NodeType, NodeDefinition> nodeDefinitions = mock(Map.class);
108         Map<MatchItemName, MatchItemDefinition> matchItemDefinitionMap = mock(Map.class);
109
110         list.add(templateParameter);
111         nodeList.add(abstractNode);
112         subNodeList.add(subNode);
113         endNodeList.add(endNode);
114         connectionList.add(abstractConnection);
115         flowList.add(abstractFlow);
116         matchItemList.add(matchItem);
117         operationList.add(abstractOperation);
118
119         when(tenantManage.getTempalteDefinition(userId))
120                 .thenReturn(map)
121                 .thenReturn(map)
122                 .thenReturn(null);
123         when(templateDefinition.getTemplateName()).thenReturn(templateName);
124         when(map.containsKey(templateDefinition.getTemplateName())).thenReturn(true);
125         Assert.assertTrue(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition).equals("The template " + templateDefinition.getTemplateName().getValue() + " has been defined."));
126         verify(tenantManage, times(2)).getTempalteDefinition(userId);
127
128         when(templateDefinition.getTemplateParameter()).thenReturn(list);
129         when(templateParameter.getParameterName()).thenReturn(parameterName);
130         when(templateParameter.getParameterValueType()).thenReturn(TemplateParameter.ParameterValueType.Int);
131         when(templateDefinition.getAbstractIntents()).thenReturn(abstractIntents);
132 //        when(abstractIntents.getAbstractOperations())
133 //                .thenReturn(null);
134         when(abstractIntents.getAbstractObjects()).thenReturn(abstractObjects);
135         when(abstractObjects.getAbstractNode())
136                 .thenReturn(nodeList) //1
137                 .thenReturn(nodeList) //1
138                 .thenReturn(nodeList) //1
139                 .thenReturn(nodeList) //1
140                 .thenReturn(null) //1
141                 .thenReturn(nodeList)//2
142                 .thenReturn(nodeList)//2
143                 .thenReturn(null)//1
144                 .thenReturn(null)//2
145                 .thenReturn(null)//1
146                 .thenReturn(null)//1
147                 .thenReturn(nodeList)//3
148                 .thenReturn(nodeList);//3
149         when(abstractNode.getNodeId()).thenReturn(nodeId);
150         when(abstractNode.getSubNode())
151                 .thenReturn(subNodeList)
152                 .thenReturn(subNodeList)
153                 .thenReturn(null);
154         when(subNode.getNodeId()).thenReturn(null);
155         Assert.assertTrue(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition).equals("The sub node is not defined."));
156         verify(abstractNode,times(2)).getSubNode();
157         verify(abstractObjects,times(2)).getAbstractNode();
158         //get into method "checkNodeTemplate" args(abstractNode,map)
159         field.set(updateTemplateDefinition, getDefinitions);
160         when(abstractNode.getNodeType()).thenReturn(mock(NodeType.class));
161         when(getDefinitions.getNodeDefinition()).thenReturn(nodeDefinitions);
162         when(nodeDefinitions.containsKey(any(NodeType.class))).thenReturn(false);
163         Assert.assertTrue(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition).equals("The node type " + abstractNode.getNodeType().getValue() + " is not defined."));
164         verify(abstractNode,times(3)).getSubNode();
165         verify(abstractObjects, times(4)).getAbstractNode();
166
167         when(abstractObjects.getAbstractConnection())
168                 .thenReturn(connectionList)//1
169                 .thenReturn(connectionList)//1
170                 .thenReturn(connectionList)//1
171                 .thenReturn(connectionList)//1
172                 .thenReturn(null)//1
173                 .thenReturn(null)//1
174                 .thenReturn(connectionList)//2
175                 .thenReturn(connectionList);//2
176         when(abstractConnection.getEndNode()).thenReturn(endNodeList);
177         when(endNode.getNodeId()).thenReturn(null);
178         Assert.assertTrue(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition).equals("The end node is not exist."));
179         verify(abstractObjects,times(7)).getAbstractNode();
180         verify(abstractObjects,times(2)).getAbstractConnection();
181 //        Assert.assertTrue(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition).equals("The end node is not exist."));
182         Assert.assertTrue(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition).equals("There are no end nodes exist."));
183         verify(abstractObjects, times(9)).getAbstractNode();
184         verify(abstractObjects, times(4)).getAbstractConnection();
185
186         when(abstractObjects.getAbstractFlow())
187                 .thenReturn(flowList)//1
188                 .thenReturn(flowList)//1
189                 .thenReturn(null)//1
190                 .thenReturn(flowList)//2
191                 .thenReturn(flowList);//2
192         //get into method "checkFlowTemplate" args(abstractFlow,map)
193         when(getDefinitions.getMatchItemDefinition()).thenReturn(matchItemDefinitionMap);
194         when(abstractFlow.getMatchItem()).thenReturn(matchItemList);
195         when(matchItem.getMatchItemName()).thenReturn(mock(MatchItemName.class));
196         when(matchItemDefinitionMap.containsKey(any(MatchItemName.class))).thenReturn(false);
197         Assert.assertTrue(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition).equals("The match item " + matchItem.getMatchItemName().getValue() + " is not defined."));
198         verify(abstractObjects, times(10)).getAbstractNode();
199         verify(abstractObjects, times(5)).getAbstractConnection();
200         verify(abstractObjects, times(2)).getAbstractFlow();
201
202         when(abstractIntents.getAbstractOperations()).thenReturn(abstractOperations);
203         when(abstractOperations.getAbstractOperation())
204                 .thenReturn(operationList)
205                 .thenReturn(operationList)
206                 .thenReturn(null);
207         when(abstractOperation.getTargetObject()).thenReturn(objectId);
208         Assert.assertTrue(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition).equals("The target is not exist."));
209 //        System.out.println(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition) + "dfsfsdfsfds");
210         verify(abstractObjects, times(13)).getAbstractNode();
211         verify(abstractObjects, times(8)).getAbstractConnection();
212         verify(abstractObjects, times(5)).getAbstractFlow();
213
214
215     }
216
217     @Test
218     public void testCheckNodeTemplate() throws Exception{
219         method = class1.getDeclaredMethod("checkNodeTemplate",new Class[]{
220                 AbstractNode.class,
221                 Map.class
222         });
223         method.setAccessible(true);
224         field = class1.getDeclaredField("getDefinitions");
225         field.setAccessible(true);
226
227         GetDefinitions getDefinitions = mock(GetDefinitions.class);
228         AbstractNode node = mock(AbstractNode.class);
229         NodeType nodeType = mock(NodeType.class);
230         NodeDefinition nodeDefinition = mock(NodeDefinition.class);
231         PropertyName propertyName = mock(PropertyName.class);
232         Property property = mock(Property.class);
233         StringValue stringValue = mock(StringValue.class);
234         PropertyValues propertyValues = mock(PropertyValues.class);
235         PropertyDefinition.PropertyValueType type = PropertyDefinition.PropertyValueType.String;
236         PropertyDefinition propertyDefinition = mock(PropertyDefinition.class);
237         TemplateParameter.ParameterValueType valueType = TemplateParameter.ParameterValueType.Int;
238         List<Property> properties = new ArrayList<Property>();
239         List<StringValue> stringValues = new ArrayList<StringValue>();
240         List<PropertyDefinition> propertyDefinitions = new ArrayList<PropertyDefinition>();
241         Map<ParameterName, TemplateParameter.ParameterValueType> parameterValueTypeMap = new HashMap<ParameterName, TemplateParameter.ParameterValueType>();
242         Map<NodeType, NodeDefinition> nodeDefinitions = new HashMap<NodeType, NodeDefinition>();
243
244         parameterValueTypeMap.put(new ParameterName("ParameterName"),valueType);
245
246         field.set(updateTemplateDefinition, getDefinitions);
247         when(getDefinitions.getNodeDefinition()).thenReturn(nodeDefinitions);
248         when(node.getNodeType()).thenReturn(nodeType);
249         when(nodeType.getValue()).thenReturn("test");
250         //test null
251         Assert.assertTrue(method.invoke(updateTemplateDefinition,node,parameterValueTypeMap).equals("The node type " + node.getNodeType().getValue() + " is not defined."));
252         verify(node,times(3)).getNodeType();
253         //test not null
254         nodeDefinitions.put(nodeType, nodeDefinition);
255         propertyDefinitions.add(propertyDefinition);
256         properties.add(property);
257         stringValues.add(stringValue);
258         Assert.assertTrue(nodeDefinitions.get(nodeType) == nodeDefinition);
259         when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitions);
260         when(propertyDefinition.getPropertyName()).thenReturn(propertyName);
261         when(propertyName.getValue()).thenReturn("test");
262         when(node.getProperty())
263                 .thenReturn(null)
264                 .thenReturn(properties);
265         Assert.assertTrue(method.invoke(updateTemplateDefinition, node, parameterValueTypeMap) == null);
266         when(property.getPropertyName())
267                 .thenReturn(mock(PropertyName.class))
268                 .thenReturn(propertyName);
269         Assert.assertTrue(method.invoke(updateTemplateDefinition, node, parameterValueTypeMap).equals("The property name " + property.getPropertyName().getValue() + " is not defined."));
270         when(propertyDefinition.getPropertyValueType())
271                 .thenReturn(PropertyDefinition.PropertyValueType.Int)
272                 .thenReturn(type);
273 //        System.out.println("type" + type.getIntValue());
274         Assert.assertTrue(method.invoke(updateTemplateDefinition, node, parameterValueTypeMap) == null);
275         when(property.getPropertyValues()).thenReturn(propertyValues);
276         when(propertyValues.getStringValue()).thenReturn(stringValues);
277         when(stringValue.getValue()).thenReturn(new String("ParameterName"));
278         Assert.assertTrue(method.invoke(updateTemplateDefinition, node, parameterValueTypeMap).equals("The property " + property.getPropertyName().getValue() + " type is not right."));
279
280     }
281
282     @Test
283     public void testCheckConnectionTemplate() throws Exception{
284         method = class1.getDeclaredMethod("checkConnectionTemplate", new Class[]{
285                 AbstractConnection.class,
286                 Map.class
287         });
288         method.setAccessible(true);
289         field = class1.getDeclaredField("getDefinitions");
290         field.setAccessible(true);
291
292         GetDefinitions getDefinitions = mock(GetDefinitions.class);
293         AbstractConnection connection = mock(AbstractConnection.class);
294         ConnectionDefinition connectionDefinition = mock(ConnectionDefinition.class);
295         ConnectionType connectionType = mock(ConnectionType.class);
296         PropertyDefinition propertyDefinition = mock(PropertyDefinition.class);
297         PropertyName propertyName = mock(PropertyName.class);
298         PropertyValues propertyValues = mock(PropertyValues.class);
299         StringValue stringValue = mock(StringValue.class);
300         PropertyDefinition.PropertyValueType type = PropertyDefinition.PropertyValueType.String;
301         TemplateParameter.ParameterValueType valueType = TemplateParameter.ParameterValueType.Int;
302         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property
303                 property = mock(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property.class);
304         List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property>
305                 properties = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property>();
306         List<PropertyDefinition> propertyDefinitions = new ArrayList<PropertyDefinition>();
307         List<StringValue> stringValues = new ArrayList<StringValue>();
308         Map<ParameterName, TemplateParameter.ParameterValueType> parameterValueTypeMap = new HashMap<ParameterName, TemplateParameter.ParameterValueType>();
309         Map<ConnectionType, ConnectionDefinition> connDefinitions = new HashMap<ConnectionType, ConnectionDefinition>();
310
311         field.set(updateTemplateDefinition, getDefinitions);
312         propertyDefinitions.add(propertyDefinition);
313         properties.add(property);
314         stringValues.add(stringValue);
315         parameterValueTypeMap.put(new ParameterName("ParameterName"), valueType);
316
317         when(getDefinitions.getConnectionDefinition()).thenReturn(connDefinitions);
318         when(connection.getConnectionType()).thenReturn(connectionType);
319         when(connectionType.getValue()).thenReturn("test");
320         //test null
321         Assert.assertTrue(method.invoke(updateTemplateDefinition, connection, parameterValueTypeMap).equals("The connection type " + connection.getConnectionType().getValue() + " is not defined."));
322         //test not null
323         connDefinitions.put(connectionType, connectionDefinition);
324         when(connectionDefinition.getPropertyDefinition()).thenReturn(propertyDefinitions);
325         when(propertyDefinition.getPropertyName()).thenReturn(propertyName);
326         when(connection.getProperty())
327                 .thenReturn(null)
328                 .thenReturn(properties);
329         Assert.assertTrue(method.invoke(updateTemplateDefinition, connection, parameterValueTypeMap) == null);
330         when(property.getPropertyName())
331                 .thenReturn(mock(PropertyName.class))
332                 .thenReturn(propertyName);
333         when(propertyName.getValue()).thenReturn("test");
334         Assert.assertTrue(method.invoke(updateTemplateDefinition, connection, parameterValueTypeMap).equals("The property name " + property.getPropertyName().getValue() + " is not defined."));
335         Assert.assertTrue(type.getIntValue() == 0);
336         when(propertyDefinition.getPropertyValueType())
337                 .thenReturn(PropertyDefinition.PropertyValueType.Int)
338                 .thenReturn(type);
339         Assert.assertTrue(method.invoke(updateTemplateDefinition, connection, parameterValueTypeMap) == null);
340         verify(propertyDefinition).getPropertyValueType();
341         when(property.getPropertyValues()).thenReturn(propertyValues);
342         when(propertyValues.getStringValue()).thenReturn(stringValues);
343         when(stringValue.getValue()).thenReturn(new String("ParameterName"));
344         Assert.assertTrue(parameterValueTypeMap.get(new ParameterName("ParameterName")) == valueType);
345 //        System.out.println((String) method.invoke(updateTemplateDefinition, connection, parameterValueTypeMap));
346 //        System.out.println("valuetype" + valueType.getIntValue());
347         Assert.assertTrue(method.invoke(updateTemplateDefinition, connection, parameterValueTypeMap).equals("The property " + property.getPropertyName().getValue() + " type is not right."));
348         verify(stringValue).getValue();
349     }
350
351     @Test
352     public void testCheckFlowTemplate() throws Exception{
353         method = class1.getDeclaredMethod("checkFlowTemplate",new Class[]{
354                 AbstractFlow.class,
355                 Map.class
356         });
357         method.setAccessible(true);
358         field = class1.getDeclaredField("getDefinitions");
359         field.setAccessible(true);
360
361         GetDefinitions getDefinitions = mock(GetDefinitions.class);
362         AbstractFlow flow = mock(AbstractFlow.class);
363         MatchItem matchItem = mock(MatchItem.class);
364         List<MatchItem> matchItemList = new ArrayList<MatchItem>();
365         MatchItemValue matchItemValue = mock(MatchItemValue.class);
366         MatchItemName matchItemName = mock(MatchItemName.class);
367         MatchItemDefinition matchItemDefinition = mock(MatchItemDefinition.class);
368         MatchItemDefinition.MatchItemValueType type = MatchItemDefinition.MatchItemValueType.String;
369         TemplateParameter.ParameterValueType valueType = TemplateParameter.ParameterValueType.Int;
370         Map<MatchItemName, MatchItemDefinition> matchItemDefinitionMap = new HashMap<MatchItemName, MatchItemDefinition>();
371         Map<ParameterName, TemplateParameter.ParameterValueType> parameterValueTypeMap = new HashMap<ParameterName, TemplateParameter.ParameterValueType>();
372
373         field.set(updateTemplateDefinition, getDefinitions);
374         parameterValueTypeMap.put(new ParameterName("ParameterName"),valueType);
375
376         when(getDefinitions.getMatchItemDefinition()).thenReturn(matchItemDefinitionMap);
377         when(flow.getMatchItem()).thenReturn(matchItemList);
378         Assert.assertTrue(method.invoke(updateTemplateDefinition, flow, parameterValueTypeMap) == null);
379         matchItemList.add(matchItem);
380         when(matchItem.getMatchItemName()).thenReturn(matchItemName);
381         when(matchItemName.getValue()).thenReturn("test");
382         Assert.assertTrue(method.invoke(updateTemplateDefinition, flow, parameterValueTypeMap).equals("The match item " + matchItem.getMatchItemName().getValue() +" is not defined."));
383         //test add matchItem
384         matchItemDefinitionMap.put(matchItemName,matchItemDefinition);
385         Assert.assertTrue(matchItemDefinitionMap.containsKey(matchItemName));
386         when(matchItemDefinition.getMatchItemValueType())
387                 .thenReturn(MatchItemDefinition.MatchItemValueType.Int)
388                 .thenReturn(type);
389         Assert.assertTrue(method.invoke(updateTemplateDefinition, flow, parameterValueTypeMap) == null);
390         when(matchItem.getMatchItemValue()).thenReturn(matchItemValue);
391         when(matchItemValue.getStringValue()).thenReturn("ParameterName");
392         Assert.assertTrue(method.invoke(updateTemplateDefinition, flow, parameterValueTypeMap).equals("The match item " + "ParameterName" +" type is not right."));
393
394     }
395
396     @Test
397     public void testCheckOperationTemplate() throws Exception{
398         method = class1.getDeclaredMethod("checkOperationTemplate",new Class[]{
399                 AbstractOperation.class,
400                 Map.class
401         });
402         method.setAccessible(true);
403         field = class1.getDeclaredField("getDefinitions");
404         field.setAccessible(true);
405
406         GetDefinitions getDefinitions = mock(GetDefinitions.class);
407         Action action = mock(Action.class);
408         ActionName actionName = mock(ActionName.class);
409         ActionDefinition actionDefinition = mock(ActionDefinition.class);
410         AbstractOperation operation = mock(AbstractOperation.class);
411         ConditionSegment conditionSegment = mock(ConditionSegment.class);
412         ParameterName parameterName = mock(ParameterName.class);
413         ParameterValues parameterValues = mock(ParameterValues.class);
414         ConditionParameterName conditionParameterName = mock(ConditionParameterName.class);
415         ConditionParameterDefinition definition = mock(ConditionParameterDefinition.class);
416         ConditionParameterDefinition conditionParameterDefinition = mock(ConditionParameterDefinition.class);
417         ConditionParameterTargetValue conditionParameterTargetValue = mock(ConditionParameterTargetValue.class);
418         ConditionParameterDefinition.ParameterValueType type = ConditionParameterDefinition.ParameterValueType.String;
419         TemplateParameter.ParameterValueType valueType = TemplateParameter.ParameterValueType.Int;
420         ActionDefinition.ParameterValueType type1 = ActionDefinition.ParameterValueType.String;
421         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.action.instance.parameter.values.StringValue
422                 stringValue = mock(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.action.instance.parameter.values.StringValue.class);
423         List<Action> actionList = new ArrayList<Action>();
424         List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.action.instance.parameter.values.StringValue>
425                 stringValues = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.action.instance.parameter.values.StringValue>();
426         List<ConditionSegment> conditionSegmentList = new ArrayList<ConditionSegment>();
427         Map<ParameterName, TemplateParameter.ParameterValueType> parameterValueTypeMap = new HashMap<ParameterName, TemplateParameter.ParameterValueType>();
428         Map<ActionName, ActionDefinition> actionDefinitionMap = new HashMap<ActionName, ActionDefinition>();
429         Map<ParameterName, ConditionParameterDefinition> conditionParameterDefinitionMap = mock(Map.class);
430
431         field.set(updateTemplateDefinition, getDefinitions);
432         conditionSegmentList.add(conditionSegment);
433         parameterValueTypeMap.put(new ParameterName("ParameterName"), valueType);
434         conditionParameterDefinitionMap.put(parameterName, conditionParameterDefinition);
435
436         when(getDefinitions.getActionDefinition()).thenReturn(actionDefinitionMap);
437         when(getDefinitions.getConditionParameterDefinition()).thenReturn(conditionParameterDefinitionMap);
438         when(operation.getConditionSegment())
439                 .thenReturn(null)
440                 .thenReturn(conditionSegmentList);
441         when(operation.getAction())
442                 .thenReturn(null)
443                 .thenReturn(actionList);
444         Assert.assertTrue(method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap) == null);
445         //test condition
446         when(conditionSegment.getConditionParameterName())
447                 .thenReturn(conditionParameterName);
448         when(conditionParameterDefinitionMap.containsKey(conditionSegment.getConditionParameterName()))
449                 .thenReturn(false)
450                 .thenReturn(true);
451         when(parameterName.getValue()).thenReturn("test");
452         Assert.assertTrue(method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap).equals("The Condition " + conditionSegment.getConditionParameterName().getValue() + " is not defined."));
453         when(conditionSegment.getConditionParameterTargetValue())
454                 .thenReturn(null)
455                 .thenReturn(conditionParameterTargetValue);
456         Assert.assertTrue(method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap) == null);
457         when(conditionParameterDefinitionMap.get(conditionSegment.getConditionParameterName())).thenReturn(definition);
458         when(definition.getParameterValueType())
459                 .thenReturn(ConditionParameterDefinition.ParameterValueType.Int)
460                 .thenReturn(type);
461         Assert.assertTrue(method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap) == null);
462         when(conditionParameterTargetValue.getStringValue()).thenReturn("ParameterName");
463         Assert.assertTrue(method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap).equals("The condition " + conditionSegment.getConditionParameterName().getValue() + " type is not right."));
464
465         //test action
466         conditionSegmentList.clear();
467         actionList.add(action);
468         stringValues.add(stringValue);
469
470         when(action.getActionName()).thenReturn(actionName);
471         when(actionName.getValue()).thenReturn("test");
472         Assert.assertTrue(method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap).equals("The action " + action.getActionName().getValue() + " is not defined."));
473         actionDefinitionMap.put(actionName, actionDefinition);
474         when(action.getParameterValues())
475                 .thenReturn(null)
476                 .thenReturn(parameterValues);
477         Assert.assertTrue(method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap) == null);
478         when(actionDefinition.getParameterValueType()).thenReturn(type1);
479         when(parameterValues.getStringValue()).thenReturn(stringValues);
480         when(stringValue.getValue()).thenReturn("ParameterName");
481         Assert.assertTrue(method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap).equals("The action " + action.getActionName().getValue() +" type is not right."));
482     }
483 }