Sho SHIMIZU
Committed by Gerrit Code Review

Add unit tests for Compiling phase

Change-Id: Ib28e48ad0f4e26f56bd2d416ee7f19f9c420a63d
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.TrafficSelector;
32 +import org.onosproject.net.flow.TrafficTreatment;
33 +import org.onosproject.net.intent.Intent;
34 +import org.onosproject.net.intent.IntentData;
35 +import org.onosproject.net.intent.MockIdGenerator;
36 +import org.onosproject.net.intent.PathIntent;
37 +import org.onosproject.net.intent.PointToPointIntent;
38 +import org.onosproject.net.intent.impl.IntentCompilationException;
39 +import org.onosproject.net.intent.impl.IntentProcessor;
40 +import org.onosproject.net.provider.ProviderId;
41 +import org.onosproject.store.Timestamp;
42 +
43 +import java.util.Arrays;
44 +import java.util.List;
45 +import java.util.Optional;
46 +
47 +import static org.easymock.EasyMock.createMock;
48 +import static org.easymock.EasyMock.expect;
49 +import static org.easymock.EasyMock.replay;
50 +import static org.easymock.EasyMock.verify;
51 +import static org.hamcrest.Matchers.instanceOf;
52 +import static org.hamcrest.Matchers.is;
53 +import static org.junit.Assert.assertThat;
54 +import static org.onosproject.net.DeviceId.deviceId;
55 +import static org.onosproject.net.Link.Type.DIRECT;
56 +import static org.onosproject.net.PortNumber.portNumber;
57 +import static org.onosproject.net.intent.IntentState.INSTALL_REQ;
58 +
59 +/**
60 + * Unit tests for Compiling phase.
61 + */
62 +public class CompilingTest {
63 +
64 + private final ApplicationId appId = new TestApplicationId("test");
65 + private final ProviderId pid = new ProviderId("of", "test");
66 + private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
67 + private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
68 + private final ConnectPoint cp1 = new ConnectPoint(deviceId("1"), portNumber(1));
69 + private final ConnectPoint cp2 = new ConnectPoint(deviceId("1"), portNumber(2));
70 + private final ConnectPoint cp3 = new ConnectPoint(deviceId("2"), portNumber(1));
71 + private final ConnectPoint cp4 = new ConnectPoint(deviceId("2"), portNumber(2));
72 +
73 + private final List<Link> links = Arrays.asList(new DefaultLink(pid, cp2, cp4, DIRECT));
74 + private final Path path = new DefaultPath(pid, links, 10);
75 +
76 + private PointToPointIntent input;
77 + private PathIntent compiled;
78 +
79 + private IdGenerator idGenerator;
80 + private IntentProcessor processor;
81 + private Timestamp version;
82 +
83 + @Before
84 + public void setUp() {
85 + processor = createMock(IntentProcessor.class);
86 + version = createMock(Timestamp.class);
87 +
88 + idGenerator = new MockIdGenerator();
89 +
90 + Intent.bindIdGenerator(idGenerator);
91 +
92 + // Intent creation should be placed after binding an ID generator
93 + input = new PointToPointIntent(appId, selector, treatment, cp1, cp3);
94 + compiled = new PathIntent(appId, selector, treatment, path);
95 + }
96 +
97 +
98 + @After
99 + public void tearDown() {
100 + Intent.unbindIdGenerator(idGenerator);
101 + }
102 +
103 + /**
104 + * Tests a next phase when no exception occurs.
105 + */
106 + @Test
107 + public void testMoveToNextPhaseWithoutError() {
108 + IntentData pending = new IntentData(input, INSTALL_REQ, version);
109 +
110 + expect(processor.compile(input, null)).andReturn(Arrays.asList(compiled));
111 + replay(processor);
112 +
113 + Compiling sut = new Compiling(processor, pending, null);
114 +
115 + Optional<IntentProcessPhase> output = sut.execute();
116 +
117 + verify(processor);
118 + assertThat(output.get(), is(instanceOf(InstallCoordinating.class)));
119 + }
120 +
121 + /**
122 + * Tests a next phase when IntentCompilationException occurs.
123 + */
124 + @Test
125 + public void testWhenIntentCompilationExceptionOccurs() {
126 + IntentData pending = new IntentData(input, INSTALL_REQ, version);
127 +
128 + expect(processor.compile(input, null)).andThrow(new IntentCompilationException());
129 + replay(processor);
130 +
131 + Compiling sut = new Compiling(processor, pending, null);
132 +
133 + Optional<IntentProcessPhase> output = sut.execute();
134 +
135 + verify(processor);
136 + assertThat(output.get(), is(instanceOf(CompilingFailed.class)));
137 + }
138 +}