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