Sho SHIMIZU
Committed by Ray Milkey

Add unit tests for WithdrawCoordinating phase

Change-Id: I13d44f71f0d9f3007da11924c5cebb9c5398bfe7
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.intent.impl.phase;
17 +
18 +import org.junit.After;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +import org.onosproject.TestApplicationId;
22 +import org.onosproject.core.ApplicationId;
23 +import org.onosproject.core.IdGenerator;
24 +import org.onosproject.net.ConnectPoint;
25 +import org.onosproject.net.DefaultLink;
26 +import org.onosproject.net.DefaultPath;
27 +import org.onosproject.net.Link;
28 +import org.onosproject.net.Path;
29 +import org.onosproject.net.flow.DefaultTrafficSelector;
30 +import org.onosproject.net.flow.DefaultTrafficTreatment;
31 +import org.onosproject.net.flow.FlowRuleOperations;
32 +import org.onosproject.net.flow.TrafficSelector;
33 +import org.onosproject.net.flow.TrafficTreatment;
34 +import org.onosproject.net.intent.Intent;
35 +import org.onosproject.net.intent.IntentData;
36 +import org.onosproject.net.intent.MockIdGenerator;
37 +import org.onosproject.net.intent.PathIntent;
38 +import org.onosproject.net.intent.PointToPointIntent;
39 +import org.onosproject.net.intent.impl.IntentProcessor;
40 +import org.onosproject.net.intent.impl.IntentRemovalException;
41 +import org.onosproject.net.provider.ProviderId;
42 +import org.onosproject.store.Timestamp;
43 +
44 +import java.util.Arrays;
45 +import java.util.List;
46 +import java.util.Optional;
47 +
48 +import static org.easymock.EasyMock.createMock;
49 +import static org.easymock.EasyMock.expect;
50 +import static org.easymock.EasyMock.replay;
51 +import static org.easymock.EasyMock.verify;
52 +import static org.hamcrest.Matchers.instanceOf;
53 +import static org.hamcrest.Matchers.is;
54 +import static org.junit.Assert.assertThat;
55 +import static org.onosproject.net.DeviceId.deviceId;
56 +import static org.onosproject.net.Link.Type.DIRECT;
57 +import static org.onosproject.net.PortNumber.portNumber;
58 +import static org.onosproject.net.intent.IntentState.INSTALLED;
59 +import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ;
60 +
61 +/**
62 + * Unit tests for WithdrawCoordinating phase.
63 + */
64 +public class WithdrawCoordinatingTest {
65 +
66 + private final ApplicationId appId = new TestApplicationId("test");
67 + private final ProviderId pid = new ProviderId("of", "test");
68 + private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
69 + private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
70 + private final ConnectPoint cp1 = new ConnectPoint(deviceId("1"), portNumber(1));
71 + private final ConnectPoint cp2 = new ConnectPoint(deviceId("1"), portNumber(2));
72 + private final ConnectPoint cp3 = new ConnectPoint(deviceId("2"), portNumber(1));
73 + private final ConnectPoint cp4 = new ConnectPoint(deviceId("2"), portNumber(2));
74 +
75 + private final List<Link> links = Arrays.asList(new DefaultLink(pid, cp2, cp4, DIRECT));
76 + private final Path path = new DefaultPath(pid, links, 10);
77 +
78 + private PointToPointIntent input;
79 + private PathIntent compiled;
80 +
81 + private IdGenerator idGenerator;
82 + private IntentProcessor processor;
83 + private Timestamp version;
84 +
85 + @Before
86 + public void setUp() {
87 + processor = createMock(IntentProcessor.class);
88 + version = createMock(Timestamp.class);
89 +
90 + idGenerator = new MockIdGenerator();
91 +
92 + Intent.bindIdGenerator(idGenerator);
93 +
94 + // Intent creation should be placed after binding an ID generator
95 + input = new PointToPointIntent(appId, selector, treatment, cp1, cp3);
96 + compiled = new PathIntent(appId, selector, treatment, path);
97 + }
98 +
99 +
100 + @After
101 + public void tearDown() {
102 + Intent.unbindIdGenerator(idGenerator);
103 + }
104 +
105 + /**
106 + * Tests a next phase when no exception occurs.
107 + */
108 + @Test
109 + public void testMoveToNextPhaseWithoutError() {
110 + IntentData pending = new IntentData(input, WITHDRAW_REQ, version);
111 + IntentData current = new IntentData(input, INSTALLED, version);
112 + current.setInstallables(Arrays.asList(compiled));
113 +
114 + FlowRuleOperations operations = createMock(FlowRuleOperations.class);
115 + expect(processor.uninstallCoordinate(current, pending)).andReturn(operations);
116 + replay(processor);
117 +
118 + WithdrawCoordinating sut = new WithdrawCoordinating(processor, pending, current);
119 +
120 + Optional<IntentProcessPhase> executed = sut.execute();
121 + verify(processor);
122 + assertThat(executed.get(), is(instanceOf(Withdrawing.class)));
123 + }
124 +
125 + /**
126 + * Tests a next phase when IntentRemovalExceptionOccurs.
127 + */
128 + @Test
129 + public void testWhenIntentRemovalExceptionOccurs() {
130 + IntentData pending = new IntentData(input, WITHDRAW_REQ, version);
131 + IntentData current = new IntentData(input, INSTALLED, version);
132 + current.setInstallables(Arrays.asList(compiled));
133 +
134 + expect(processor.uninstallCoordinate(current, pending)).andThrow(new IntentRemovalException());
135 + replay(processor);
136 +
137 + WithdrawCoordinating sut = new WithdrawCoordinating(processor, pending, current);
138 +
139 + Optional<IntentProcessPhase> executed = sut.execute();
140 + verify(processor);
141 + assertThat(executed.get(), is(instanceOf(WithdrawingFailed.class)));
142 + }
143 +
144 +}