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.criteria.Criteria; |
| 28 | import org.onosproject.net.flow.criteria.Criterion; |
| 29 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 30 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | |
| 35 | import static org.easymock.EasyMock.createMock; |
| 36 | import static org.easymock.EasyMock.expect; |
| 37 | import static org.easymock.EasyMock.replay; |
| 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.FilteringObjectiveJsonMatcher.matchesFilteringObjective; |
| 42 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for FilteringObjective Codec. |
| 46 | */ |
| 47 | public class FilteringObjectiveCodecTest { |
| 48 | |
| 49 | MockCodecContext context; |
| 50 | JsonCodec<FilteringObjective> filteringObjectiveCodec; |
| 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 FilteringObjective codec. |
| 57 | */ |
| 58 | @Before |
| 59 | public void setUp() { |
| 60 | context = new MockCodecContext(); |
| 61 | filteringObjectiveCodec = context.codec(FilteringObjective.class); |
| 62 | assertThat(filteringObjectiveCodec, 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 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 { |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 96 | |
| 97 | ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID); |
| 98 | |
| 99 | expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes(); |
| 100 | replay(mockCoreService); |
| 101 | |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 102 | FilteringObjective filteringObjective = getFilteringObjective("FilteringObjective.json"); |
| 103 | |
| 104 | assertThat(filteringObjective.type(), is(FilteringObjective.Type.PERMIT)); |
| 105 | assertThat(filteringObjective.priority(), is(60)); |
| 106 | assertThat(filteringObjective.timeout(), is(1)); |
| 107 | assertThat(filteringObjective.op(), is(FilteringObjective.Operation.ADD)); |
| 108 | assertThat(filteringObjective.permanent(), is(false)); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Reads in a filteringObjective from the given resource and decodes it. |
| 113 | * |
| 114 | * @param resourceName resource to use to read the JSON for the rule |
| 115 | * @return decoded filteringObjective |
| 116 | * @throws IOException if processing the resource fails |
| 117 | */ |
| 118 | private FilteringObjective getFilteringObjective(String resourceName) throws IOException { |
| 119 | InputStream jsonStream = FilteringObjectiveCodecTest.class.getResourceAsStream(resourceName); |
| 120 | JsonNode json = context.mapper().readTree(jsonStream); |
| 121 | assertThat(json, notNullValue()); |
| 122 | FilteringObjective filteringObjective = filteringObjectiveCodec.decode((ObjectNode) json, context); |
| 123 | assertThat(filteringObjective, notNullValue()); |
| 124 | return filteringObjective; |
| 125 | } |
| 126 | } |