Open Kilda Java Documentation
BatchRecordTest.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.service.batch;
17 
18 import static org.easymock.EasyMock.capture;
19 import static org.easymock.EasyMock.createMock;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.newCapture;
22 import static org.easymock.EasyMock.replay;
23 import static org.easymock.EasyMock.verify;
24 
27 
28 import com.google.common.collect.ImmutableList;
29 import net.floodlightcontroller.core.IOFSwitch;
30 import org.easymock.Capture;
31 import org.easymock.CaptureType;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.projectfloodlight.openflow.protocol.OFFactory;
36 import org.projectfloodlight.openflow.protocol.OFMessage;
37 import org.projectfloodlight.openflow.protocol.OFType;
38 import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
39 import org.projectfloodlight.openflow.types.DatapathId;
40 
41 import java.util.Iterator;
42 import java.util.List;
43 
44 public class BatchRecordTest {
45  private OFMessage payload;
46  private IOFSwitch iofSwitch = createMock(IOFSwitch.class);
47  private SwitchUtils switchUtils = createMock(SwitchUtils.class);
48  protected final DatapathId switchId = DatapathId.of(0x00ff000001L);
49 
50  @Before
51  public void setUp() throws Exception {
52  OFFactory ofFactory = new OFFactoryVer13();
53  payload = ofFactory.buildFlowAdd()
54  .setPriority(0)
55  .build();
56  expect(iofSwitch.getOFFactory()).andReturn(ofFactory).anyTimes();
57  }
58 
59  @Test
60  public void write() {
61  OfPendingMessage batchRecord = new OfPendingMessage(switchId, payload);
62  BatchRecord batch = new BatchRecord(switchUtils, ImmutableList.of(batchRecord));
63 
64  expect(switchUtils.lookupSwitch(switchId)).andReturn(iofSwitch).anyTimes();
65  replay(switchUtils);
66 
67  Capture<OFMessage> captureSwitchWrite = newCapture(CaptureType.ALL);
68 
69  expect(iofSwitch.getId()).andReturn(switchId).anyTimes();
70  expect(iofSwitch.write(capture(captureSwitchWrite))).andReturn(true).times(2);
71  replay(iofSwitch);
72 
73  try {
74  batch.write();
75  } catch (OFInstallException e) {
76  throw new AssertionError("Batch write operation failed", e);
77  }
78 
79  verify(switchUtils, iofSwitch);
80 
81  List<OFMessage> switchWriteRecords = captureSwitchWrite.getValues();
82 
83  Iterator<OFMessage> iter = switchWriteRecords.iterator();
84  Assert.assertEquals(payload, iter.next());
85  Assert.assertEquals(OFType.BARRIER_REQUEST, iter.next().getType());
86  }
87 }
IOFSwitch lookupSwitch(DatapathId dpId)
net
Definition: plan-b.py:46