Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.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; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.flow.criteria.Criteria; |
| 26 | import org.onosproject.net.flow.criteria.Criterion; |
| 27 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 28 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.io.InputStream; |
| 32 | |
| 33 | import static org.easymock.EasyMock.createMock; |
| 34 | import static org.easymock.EasyMock.expect; |
| 35 | import static org.easymock.EasyMock.replay; |
| 36 | import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | import static org.hamcrest.Matchers.is; |
| 38 | import static org.hamcrest.Matchers.notNullValue; |
| 39 | import static org.onosproject.codec.impl.FilteringObjectiveJsonMatcher.matchesFilteringObjective; |
| 40 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 41 | |
| 42 | /** |
| 43 | * Unit tests for FilteringObjective Codec. |
| 44 | */ |
| 45 | public class FilteringObjectiveCodecTest { |
| 46 | |
| 47 | MockCodecContext context; |
| 48 | JsonCodec<FilteringObjective> filteringObjectiveCodec; |
| 49 | final CoreService mockCoreService = createMock(CoreService.class); |
| 50 | |
| 51 | /** |
| 52 | * Sets up for each test. |
| 53 | * Creates a context and fetches the FilteringObjective codec. |
| 54 | */ |
| 55 | @Before |
| 56 | public void setUp() { |
| 57 | context = new MockCodecContext(); |
| 58 | filteringObjectiveCodec = context.codec(FilteringObjective.class); |
| 59 | assertThat(filteringObjectiveCodec, notNullValue()); |
| 60 | |
| 61 | expect(mockCoreService.registerApplication(FilteringObjectiveCodec.REST_APP_ID)) |
| 62 | .andReturn(APP_ID).anyTimes(); |
| 63 | replay(mockCoreService); |
| 64 | context.registerService(CoreService.class, mockCoreService); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Tests encoding of a FilteringObjective object. |
| 69 | */ |
| 70 | @Test |
| 71 | public void testFilteringObjectiveEncode() { |
| 72 | |
| 73 | Criterion condition1 = Criteria.matchVlanId(VlanId.ANY); |
| 74 | Criterion condition2 = Criteria.matchEthType((short) 0x8844); |
| 75 | |
| 76 | FilteringObjective filteringObj = DefaultFilteringObjective.builder() |
| 77 | .makePermanent() |
| 78 | .permit() |
| 79 | .fromApp(APP_ID) |
| 80 | .withPriority(60) |
| 81 | .addCondition(condition1) |
| 82 | .addCondition(condition2) |
| 83 | .add(); |
| 84 | |
| 85 | // TODO: need to add test case for TrafficTreatment (META in filteringObj) |
| 86 | |
| 87 | ObjectNode filteringObjJson = filteringObjectiveCodec.encode(filteringObj, context); |
| 88 | assertThat(filteringObjJson, matchesFilteringObjective(filteringObj)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Test decoding of a FilteringObjective object. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testFilteringObjectiveDecode() throws IOException { |
| 96 | FilteringObjective filteringObjective = getFilteringObjective("FilteringObjective.json"); |
| 97 | |
| 98 | assertThat(filteringObjective.type(), is(FilteringObjective.Type.PERMIT)); |
| 99 | assertThat(filteringObjective.priority(), is(60)); |
| 100 | assertThat(filteringObjective.timeout(), is(1)); |
| 101 | assertThat(filteringObjective.op(), is(FilteringObjective.Operation.ADD)); |
| 102 | assertThat(filteringObjective.permanent(), is(false)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Reads in a filteringObjective from the given resource and decodes it. |
| 107 | * |
| 108 | * @param resourceName resource to use to read the JSON for the rule |
| 109 | * @return decoded filteringObjective |
| 110 | * @throws IOException if processing the resource fails |
| 111 | */ |
| 112 | private FilteringObjective getFilteringObjective(String resourceName) throws IOException { |
| 113 | InputStream jsonStream = FilteringObjectiveCodecTest.class.getResourceAsStream(resourceName); |
| 114 | JsonNode json = context.mapper().readTree(jsonStream); |
| 115 | assertThat(json, notNullValue()); |
| 116 | FilteringObjective filteringObjective = filteringObjectiveCodec.decode((ObjectNode) json, context); |
| 117 | assertThat(filteringObjective, notNullValue()); |
| 118 | return filteringObjective; |
| 119 | } |
| 120 | } |