Merge "Added two test files"
[nemo.git] / nemo-impl / src / test / java / org / opendaylight / nemo / user / vnspacemanager / structurestyle / updateintent / UpdateFlowTest.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
11
12 import static org.mockito.Mockito.doNothing;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
17
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.FutureCallback;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.SettableFuture;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
28 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
29 import org.opendaylight.nemo.user.vnspacemanager.languagestyle.NEMOConstants;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.MatchItemName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.Users;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.FlowBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.FlowKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.UserKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.MatchItemDefinitions;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.flow.instance.MatchItem;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.match.item.definitions.MatchItemDefinition;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.match.item.instance.MatchItemValue;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.FlowId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.FlowName;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.concurrent.ExecutionException;
53 import java.util.concurrent.TimeUnit;
54 import java.util.concurrent.TimeoutException;
55
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Map;
59 import java.util.LinkedList;
60 import org.junit.Assert;
61 import org.junit.Before;
62 import org.junit.Test;
63 import java.util.*;
64 import java.util.List;
65
66 import static org.mockito.Matchers.any;
67 import static org.mockito.Mockito.mock;
68 import static org.mockito.Mockito.times;
69 import static org.mockito.Mockito.verify;
70 import static org.mockito.Mockito.when;
71 import com.google.common.util.concurrent.CheckedFuture;
72
73 import java.lang.reflect.Field;
74 import java.lang.reflect.Method;
75 import java.util.ArrayList;
76 import java.util.List;
77 import java.lang.reflect.Field;
78 import java.lang.reflect.Method;
79 import java.util.HashMap;
80 import java.util.List;
81 import java.util.Map;
82 import java.util.concurrent.ExecutionException;
83 import java.util.concurrent.TimeUnit;
84 import java.util.concurrent.TimeoutException;
85 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
86
87
88 import static org.junit.Assert.*;
89
90 /**
91  * Created by ldzd11 on 2015/12/22.
92  */
93 public class UpdateFlowTest {
94
95     private  UpdateFlow updateFlow;
96     private DataBroker dataBroker;
97     private TenantManage tenantManage;
98     private MatchItemName matchItemName;
99     private MatchItemName matchItemName2;
100     private MatchItem matchItem;
101     private List<MatchItem> matchItemList;
102     private List<MatchItem> matchItemListnull;
103     private User user;
104     private UserId userId;
105     private Objects objects;
106     private List<Flow> flowList;
107     private FlowId flowId;
108     private FlowName flowName;
109     private FlowName flowName2;
110
111
112
113     private MatchItemValue matchItemValue;
114     private Flow flow;
115     private Flow flow2;
116
117     @Before
118     public void setUp() throws Exception {
119         dataBroker = mock(DataBroker.class);
120         tenantManage = mock(TenantManage.class);
121         matchItemName = mock(MatchItemName.class);
122         matchItemName2= mock(MatchItemName.class);
123         matchItem = mock(MatchItem.class);
124         matchItemList = new ArrayList<MatchItem>(1);
125         matchItemListnull = new ArrayList<MatchItem>();
126         userId = mock(UserId.class);
127         user = mock(User.class);
128         matchItemList.add(matchItem);
129         flow = mock(Flow.class);
130         flow2 = mock(Flow.class);
131         flowId = mock(FlowId.class);
132         flowName = mock(FlowName.class);
133         flowName2 = mock(FlowName.class);
134
135         matchItemValue = mock(MatchItemValue.class);
136         objects = mock(Objects.class);
137         flowList = new ArrayList<Flow>(1);
138         flowList.add(flow2);
139
140         updateFlow = new UpdateFlow(dataBroker,tenantManage);
141
142     }
143
144
145
146
147     @Test
148     public void testFlowHandling() throws Exception {
149
150         //into checkdefinition  for error = null
151         CheckedFuture matchitemdefinitionFuture = mock(CheckedFuture.class);
152         ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
153         when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
154         when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(matchitemdefinitionFuture);
155
156         List<MatchItemDefinition> matchItemDefinitions = new ArrayList<MatchItemDefinition>();
157         MatchItemDefinition matchItemDefinition = mock(MatchItemDefinition.class);
158         matchItemDefinitions.add(matchItemDefinition);
159         when(matchItemDefinition.getMatchItemName()).thenReturn(matchItemName);
160
161         Class<UpdateFlow> class1 = UpdateFlow.class;
162         Class<GetDefinitions> class2 = GetDefinitions.class;
163         Field field = class1.getDeclaredField("getDefinitions");
164         field.setAccessible(true);
165         Field field1 = class2.getDeclaredField("matchItemDefinitionList");
166         field1.setAccessible(true);
167
168         field1.set(field.get(updateFlow), matchItemDefinitions);
169         when(flow.getMatchItem()).thenReturn(null);
170         //into checkinstance for error = null
171         when(tenantManage.getFlow(userId)).thenReturn(null);
172         when(tenantManage.getFlowDataStore(userId)).thenReturn(null);
173         doNothing().when(tenantManage).setFlow(any(UserId.class),any(FlowId.class),any(Flow.class));
174
175
176         Assert.assertEquals(updateFlow.FlowHandling(userId, flow), null);
177
178
179
180
181     }
182
183     @Test
184     public void testcheckInstance() throws Exception {
185         Class<UpdateFlow> class1 = UpdateFlow.class;
186         Method method = class1.getDeclaredMethod("checkInstance",new Class[]{UserId.class, Flow.class});
187         method.setAccessible(true);
188         Map<FlowId,Flow> map1 = new HashMap<FlowId, Flow>();
189         map1.put(flowId,flow2);
190         when(tenantManage.getFlow(userId)).thenReturn(map1);
191         when(flow.getFlowId()).thenReturn(flowId);
192         when(flow2.getFlowName()).thenReturn(flowName2);
193         when(flow.getFlowName()).thenReturn(flowName);
194         Assert.assertEquals(method.invoke(updateFlow, userId, flow), "The flow name should not be changed.");
195
196         when(tenantManage.getFlow(userId)).thenReturn(null);
197         when(tenantManage.getFlowDataStore(userId)).thenReturn(map1);
198         when(flow.getFlowId()).thenReturn(flowId);
199         when(flow2.getFlowName()).thenReturn(flowName2);
200         when(flow.getFlowName()).thenReturn(flowName);
201         Assert.assertEquals(method.invoke(updateFlow, userId, flow), "The flow name should not be changed.");
202
203         when(tenantManage.getFlow(userId)).thenReturn(null);
204         when(tenantManage.getFlowDataStore(userId)).thenReturn(null);
205         Assert.assertEquals(method.invoke(updateFlow, userId, flow), null);
206     }
207
208     @Test
209     public void testcheckDefinition() throws Exception {
210         Class<UpdateFlow> class3 = UpdateFlow.class;
211         Method method = class3.getDeclaredMethod("checkDefinition",new Class[]{Flow.class});
212         method.setAccessible(true);
213
214         CheckedFuture matchitemdefinitionFuture = mock(CheckedFuture.class);
215         ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
216         when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
217         when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(matchitemdefinitionFuture);
218
219
220         List<MatchItemDefinition> matchItemDefinitions = new ArrayList<MatchItemDefinition>();
221         List<MatchItemDefinition> matchItemDefinitionsnull = new ArrayList<MatchItemDefinition>();
222         MatchItemDefinition matchItemDefinition = mock(MatchItemDefinition.class);
223         matchItemDefinitions.add(matchItemDefinition);
224         when(matchItemDefinition.getMatchItemName()).thenReturn(matchItemName);
225
226
227         Class<UpdateFlow> class1 = UpdateFlow.class;
228         Class<GetDefinitions> class2 = GetDefinitions.class;
229         Field field = class1.getDeclaredField("getDefinitions");
230         field.setAccessible(true);
231         Field field1 = class2.getDeclaredField("matchItemDefinitionList");
232         field1.setAccessible(true);
233
234         //the else
235         field1.set(field.get(updateFlow), matchItemDefinitions);
236         when(flow.getMatchItem()).thenReturn(matchItemList);
237         when(matchItem.getMatchItemName()).thenReturn(matchItemName2);
238         Assert.assertEquals(method.invoke(updateFlow, flow), "The match item has not been defined.");
239
240         //the upper
241         field1.set(field.get(updateFlow), matchItemDefinitionsnull);
242         Assert.assertEquals(method.invoke(updateFlow, flow), "The match item has not been defined.");
243
244         //the if errorInfo == null into checkPredefine
245         field1.set(field.get(updateFlow), matchItemDefinitions);
246         when(flow.getMatchItem()).thenReturn(matchItemListnull);
247         //into checkPredefine
248         Assert.assertEquals(method.invoke(updateFlow, flow), null);
249
250
251
252
253     }
254
255     @Test
256     public void testcheckPredefine() throws Exception {
257         Class<UpdateFlow> class1 = UpdateFlow.class;
258         Method method = class1.getDeclaredMethod("checkPredefine",new Class[]{List.class});
259         method.setAccessible(true);
260
261         when(matchItem.getMatchItemName()).thenReturn(matchItemName);
262         when(matchItem.getMatchItemName().getValue()).thenReturn(new String("src-ip"));
263         when(matchItem.getMatchItemValue()).thenReturn(matchItemValue);
264
265         //stringValues.contains("/")
266         when(matchItem.getMatchItemValue().getStringValue()).thenReturn(new String("110/"));
267         //into checkIpPrefix(stringvalues)  legalValue=false
268         Assert.assertEquals(method.invoke(updateFlow, matchItemList),"The " + NEMOConstants.ip_address + " is not legal.");
269
270         when(matchItem.getMatchItemValue().getStringValue()).thenReturn(new String("110\\."));
271         //into checkip address
272         Assert.assertEquals(method.invoke(updateFlow, matchItemList),"The " + NEMOConstants.ip_address + " is not legal.");
273
274
275         when(matchItem.getMatchItemName().getValue()).thenReturn(new String("src-mac"));
276         when(matchItem.getMatchItemValue()).thenReturn(matchItemValue);
277         when(matchItem.getMatchItemValue().getStringValue()).thenReturn(new String("110:"));
278         //into valuecheck.checkMac
279         Assert.assertEquals(method.invoke(updateFlow, matchItemList), "The " + NEMOConstants.mac_address + " is not legal.");
280
281
282
283
284
285
286
287
288
289
290
291     }
292 }