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;
12 import static org.mockito.Mockito.doNothing;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
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;
49 import java.util.HashMap;
50 import java.util.List;
52 import java.util.concurrent.ExecutionException;
53 import java.util.concurrent.TimeUnit;
54 import java.util.concurrent.TimeoutException;
56 import java.util.HashMap;
57 import java.util.List;
59 import java.util.LinkedList;
60 import org.junit.Assert;
61 import org.junit.Before;
62 import org.junit.Test;
64 import java.util.List;
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;
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;
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;
88 import static org.junit.Assert.*;
91 * Created by ldzd11 on 2015/12/22.
93 public class UpdateFlowTest {
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;
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;
113 private MatchItemValue matchItemValue;
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);
135 matchItemValue = mock(MatchItemValue.class);
136 objects = mock(Objects.class);
137 flowList = new ArrayList<Flow>(1);
140 updateFlow = new UpdateFlow(dataBroker,tenantManage);
148 public void testFlowHandling() throws Exception {
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);
156 List<MatchItemDefinition> matchItemDefinitions = new ArrayList<MatchItemDefinition>();
157 MatchItemDefinition matchItemDefinition = mock(MatchItemDefinition.class);
158 matchItemDefinitions.add(matchItemDefinition);
159 when(matchItemDefinition.getMatchItemName()).thenReturn(matchItemName);
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);
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));
176 Assert.assertEquals(updateFlow.FlowHandling(userId, flow), null);
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.");
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.");
203 when(tenantManage.getFlow(userId)).thenReturn(null);
204 when(tenantManage.getFlowDataStore(userId)).thenReturn(null);
205 Assert.assertEquals(method.invoke(updateFlow, userId, flow), null);
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);
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);
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);
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);
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.");
241 field1.set(field.get(updateFlow), matchItemDefinitionsnull);
242 Assert.assertEquals(method.invoke(updateFlow, flow), "The match item has not been defined.");
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);
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);
261 when(matchItem.getMatchItemName()).thenReturn(matchItemName);
262 when(matchItem.getMatchItemName().getValue()).thenReturn(new String("src-ip"));
263 when(matchItem.getMatchItemValue()).thenReturn(matchItemValue);
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.");
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.");
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.");