Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 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.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.codec.JsonCodec; |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 25 | import org.onosproject.core.CoreService; |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 26 | import org.onosproject.core.DefaultApplicationId; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 28 | import org.onosproject.net.flow.TrafficSelector; |
| 29 | import org.onosproject.net.flow.criteria.Criteria; |
| 30 | import org.onosproject.net.flow.criteria.Criterion; |
| 31 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 32 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 37 | import static org.easymock.EasyMock.*; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 38 | import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | import static org.hamcrest.Matchers.is; |
| 40 | import static org.hamcrest.Matchers.notNullValue; |
| 41 | import static org.onosproject.codec.impl.ForwardingObjectiveJsonMatcher.matchesForwardingObjective; |
| 42 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for ForwardingObjective Codec. |
| 46 | */ |
| 47 | public class ForwardingObjectiveCodecTest { |
| 48 | |
| 49 | MockCodecContext context; |
| 50 | JsonCodec<ForwardingObjective> forwardingObjectiveCodec; |
| 51 | final CoreService mockCoreService = createMock(CoreService.class); |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 52 | static final String SAMPLE_APP_ID = "org.onosproject.sample"; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Sets up for each test. |
| 56 | * Creates a context and fetches the ForwardingObjective codec. |
| 57 | */ |
| 58 | @Before |
| 59 | public void setUp() { |
| 60 | context = new MockCodecContext(); |
| 61 | forwardingObjectiveCodec = context.codec(ForwardingObjective.class); |
| 62 | assertThat(forwardingObjectiveCodec, notNullValue()); |
| 63 | |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 64 | context.registerService(CoreService.class, mockCoreService); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Tests encoding of a ForwardingObjective object. |
| 69 | */ |
| 70 | @Test |
| 71 | public void testForwardingObjectiveEncode() { |
| 72 | |
| 73 | Criterion criterion1 = Criteria.matchVlanId(VlanId.ANY); |
| 74 | Criterion criterion2 = Criteria.matchEthType((short) 0x8844); |
| 75 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 76 | .add(criterion1) |
| 77 | .add(criterion2) |
| 78 | .build(); |
| 79 | |
| 80 | ForwardingObjective forwardingObj = DefaultForwardingObjective.builder() |
| 81 | .makePermanent() |
| 82 | .fromApp(APP_ID) |
| 83 | .withPriority(60) |
| 84 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 85 | .nextStep(1) |
| 86 | .withSelector(selector) |
| 87 | .add(); |
| 88 | |
| 89 | ObjectNode forwardingObjJson = forwardingObjectiveCodec.encode(forwardingObj, context); |
| 90 | assertThat(forwardingObjJson, matchesForwardingObjective(forwardingObj)); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Test decoding of a ForwardingObjective object. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testForwardingObjectiveDecode() throws IOException { |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 98 | |
| 99 | ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID); |
| 100 | |
| 101 | expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes(); |
| 102 | replay(mockCoreService); |
| 103 | |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 104 | ForwardingObjective forwardingObjective = getForwardingObjective("ForwardingObjective.json"); |
| 105 | |
| 106 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 107 | assertThat(forwardingObjective.priority(), is(60)); |
| 108 | assertThat(forwardingObjective.timeout(), is(1)); |
| 109 | assertThat(forwardingObjective.op(), is(ForwardingObjective.Operation.ADD)); |
| 110 | assertThat(forwardingObjective.permanent(), is(false)); |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 111 | assertThat(forwardingObjective.appId().name(), is(SAMPLE_APP_ID)); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Reads in a forwardingObjectiveJsonCodec from the given resource and decodes it. |
| 116 | * |
| 117 | * @param resourceName resource to use to read the JSON for the rule |
| 118 | * @return decoded forwardingObjectiveJsonCodec |
| 119 | * @throws IOException if processing the resource fails |
| 120 | */ |
| 121 | private ForwardingObjective getForwardingObjective(String resourceName) throws IOException { |
| 122 | InputStream jsonStream = ForwardingObjectiveCodecTest.class.getResourceAsStream(resourceName); |
| 123 | JsonNode json = context.mapper().readTree(jsonStream); |
| 124 | assertThat(json, notNullValue()); |
| 125 | ForwardingObjective forwardingObjective = forwardingObjectiveCodec.decode((ObjectNode) json, context); |
| 126 | assertThat(forwardingObjective, notNullValue()); |
| 127 | return forwardingObjective; |
| 128 | } |
| 129 | } |