Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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.vtnweb.resources; |
| 17 | |
| 18 | import static org.easymock.EasyMock.anyObject; |
| 19 | import static org.easymock.EasyMock.createMock; |
| 20 | import static org.easymock.EasyMock.expect; |
| 21 | import static org.easymock.EasyMock.replay; |
| 22 | import static org.hamcrest.Matchers.is; |
| 23 | import static org.hamcrest.Matchers.notNullValue; |
| 24 | import static org.junit.Assert.assertThat; |
| 25 | |
| 26 | import java.util.HashSet; |
| 27 | import java.util.List; |
| 28 | import java.util.Objects; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import javax.ws.rs.client.WebTarget; |
| 32 | |
| 33 | import org.junit.After; |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.onlab.osgi.ServiceDirectory; |
| 37 | import org.onlab.osgi.TestServiceDirectory; |
| 38 | import org.onlab.rest.BaseResource; |
| 39 | import org.onosproject.codec.CodecService; |
| 40 | import org.onosproject.net.DeviceId; |
| 41 | import org.onosproject.vtnrsc.FiveTuple; |
| 42 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 43 | import org.onosproject.vtnrsc.LoadBalanceId; |
| 44 | import org.onosproject.vtnrsc.PortChain; |
| 45 | import org.onosproject.vtnrsc.PortChainId; |
| 46 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 47 | import org.onosproject.vtnrsc.PortPairId; |
| 48 | import org.onosproject.vtnrsc.TenantId; |
| 49 | import org.onosproject.vtnrsc.portchain.PortChainService; |
| 50 | import org.onosproject.vtnweb.web.SfcCodecContext; |
| 51 | |
| 52 | import com.eclipsesource.json.Json; |
| 53 | import com.eclipsesource.json.JsonObject; |
| 54 | import com.google.common.collect.ImmutableList; |
| 55 | import com.google.common.collect.Lists; |
| 56 | |
| 57 | /** |
| 58 | * Unit tests for port chain device map REST APIs. |
| 59 | */ |
| 60 | public class PortChainDeviceMapResourceTest extends VtnResourceTest { |
| 61 | |
| 62 | final PortChainService portChainService = createMock(PortChainService.class); |
| 63 | |
| 64 | PortChainId portChainId1 = PortChainId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 65 | TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); |
| 66 | private final List<PortPairGroupId> portPairGroupList1 = Lists.newArrayList(); |
| 67 | private final List<FlowClassifierId> flowClassifierList1 = Lists.newArrayList(); |
| 68 | |
| 69 | final MockPortChain portChain1 = new MockPortChain(portChainId1, tenantId1, "portChain1", |
| 70 | "Mock port chain", portPairGroupList1, |
| 71 | flowClassifierList1); |
| 72 | |
| 73 | /** |
| 74 | * Mock class for a port chain. |
| 75 | */ |
| 76 | private static class MockPortChain implements PortChain { |
| 77 | |
| 78 | private final PortChainId portChainId; |
| 79 | private final TenantId tenantId; |
| 80 | private final String name; |
| 81 | private final String description; |
| 82 | private final List<PortPairGroupId> portPairGroupList; |
| 83 | private final List<FlowClassifierId> flowClassifierList; |
| 84 | |
| 85 | public MockPortChain(PortChainId portChainId, TenantId tenantId, |
| 86 | String name, String description, |
| 87 | List<PortPairGroupId> portPairGroupList, |
| 88 | List<FlowClassifierId> flowClassifierList) { |
| 89 | |
| 90 | this.portChainId = portChainId; |
| 91 | this.tenantId = tenantId; |
| 92 | this.name = name; |
| 93 | this.description = description; |
| 94 | this.portPairGroupList = portPairGroupList; |
| 95 | this.flowClassifierList = flowClassifierList; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public PortChainId portChainId() { |
| 100 | return portChainId; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public TenantId tenantId() { |
| 105 | return tenantId; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public String name() { |
| 110 | return name; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String description() { |
| 115 | return description; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public List<PortPairGroupId> portPairGroups() { |
| 120 | return ImmutableList.copyOf(portPairGroupList); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public List<FlowClassifierId> flowClassifiers() { |
| 125 | return ImmutableList.copyOf(flowClassifierList); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean exactMatch(PortChain portChain) { |
| 130 | return this.equals(portChain) && |
| 131 | Objects.equals(this.portChainId, portChain.portChainId()) && |
| 132 | Objects.equals(this.tenantId, portChain.tenantId()); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void addLoadBalancePath(FiveTuple fiveTuple, LoadBalanceId id, List<PortPairId> path) { |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public LoadBalanceId getLoadBalanceId(FiveTuple fiveTuple) { |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public Set<FiveTuple> getLoadBalanceIdMapKeys() { |
| 146 | return null; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public List<PortPairId> getLoadBalancePath(LoadBalanceId id) { |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public List<PortPairId> getLoadBalancePath(FiveTuple fiveTuple) { |
| 156 | return null; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public LoadBalanceId matchPath(List<PortPairId> path) { |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public int getLoadBalancePathSize() { |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void addSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) { |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void addSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) { |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void removeSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) { |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void removeSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) { |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public List<DeviceId> getSfcClassifiers(LoadBalanceId id) { |
| 187 | DeviceId deviceId1 = DeviceId.deviceId("of:000000000000001"); |
| 188 | List<DeviceId> classifierList = Lists.newArrayList(); |
| 189 | classifierList.add(deviceId1); |
| 190 | return classifierList; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public List<DeviceId> getSfcForwarders(LoadBalanceId id) { |
| 195 | DeviceId deviceId1 = DeviceId.deviceId("of:000000000000002"); |
| 196 | DeviceId deviceId2 = DeviceId.deviceId("of:000000000000003"); |
| 197 | List<DeviceId> forwarderList = Lists.newArrayList(); |
| 198 | forwarderList.add(deviceId1); |
| 199 | forwarderList.add(deviceId2); |
| 200 | return forwarderList; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Set<LoadBalanceId> getLoadBalancePathMapKeys() { |
| 205 | LoadBalanceId id = LoadBalanceId.of((byte) 1); |
| 206 | Set<LoadBalanceId> set = new HashSet<LoadBalanceId>(); |
| 207 | set.add(id); |
| 208 | return set; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public PortChain oldPortChain() { |
| 213 | return null; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Sets up the global values for all the tests. |
| 219 | */ |
| 220 | @Before |
| 221 | public void setUpTest() { |
| 222 | SfcCodecContext context = new SfcCodecContext(); |
| 223 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 224 | .add(PortChainService.class, portChainService) |
| 225 | .add(CodecService.class, context.codecManager()); |
| 226 | BaseResource.setServiceDirectory(testDirectory); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Cleans up. |
| 231 | */ |
| 232 | @After |
| 233 | public void tearDownTest() { |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Tests the result of a rest api GET for port chain id. |
| 238 | */ |
| 239 | @Test |
| 240 | public void testGetPortChainDeviceMap() { |
| 241 | |
| 242 | expect(portChainService.getPortChain(anyObject())).andReturn(portChain1).anyTimes(); |
| 243 | replay(portChainService); |
| 244 | |
| 245 | final WebTarget wt = target(); |
| 246 | final String response = wt.path("portChainDeviceMap/1278dcd4-459f-62ed-754b-87fc5e4a6751").request() |
| 247 | .get(String.class); |
| 248 | final JsonObject result = Json.parse(response).asObject(); |
| 249 | assertThat(result, notNullValue()); |
| 250 | assertThat(result.names().get(0), is("portChainDeviceMap")); |
| 251 | |
| 252 | } |
| 253 | } |