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.onosproject.codec.JsonCodec; |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 24 | import org.onosproject.core.CoreService; |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 25 | import org.onosproject.core.DefaultApplicationId; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 27 | import org.onosproject.net.flow.TrafficTreatment; |
| 28 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flowobjective.DefaultNextTreatment; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flowobjective.NextObjective; |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flowobjective.NextTreatment; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 32 | |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | |
| 36 | import static org.easymock.EasyMock.createMock; |
| 37 | import static org.easymock.EasyMock.expect; |
| 38 | import static org.easymock.EasyMock.replay; |
| 39 | import static org.hamcrest.MatcherAssert.assertThat; |
| 40 | import static org.hamcrest.Matchers.is; |
| 41 | import static org.hamcrest.Matchers.notNullValue; |
| 42 | import static org.onosproject.codec.impl.NextObjectiveJsonMatcher.matchesNextObjective; |
| 43 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 44 | |
| 45 | /** |
| 46 | * Unit tests for NextObjective Codec. |
| 47 | */ |
| 48 | public class NextObjectiveCodecTest { |
| 49 | |
| 50 | MockCodecContext context; |
| 51 | JsonCodec<NextObjective> nextObjectiveCodec; |
| 52 | final CoreService mockCoreService = createMock(CoreService.class); |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 53 | static final String SAMPLE_APP_ID = "org.onosproject.sample"; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Sets up for each test. |
| 57 | * Creates a context and fetches the NextObjective codec. |
| 58 | */ |
| 59 | @Before |
| 60 | public void setUp() { |
| 61 | context = new MockCodecContext(); |
| 62 | nextObjectiveCodec = context.codec(NextObjective.class); |
| 63 | assertThat(nextObjectiveCodec, notNullValue()); |
| 64 | |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 65 | context.registerService(CoreService.class, mockCoreService); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Tests encoding of a NextObjective object. |
| 70 | */ |
| 71 | @Test |
| 72 | public void testNextObjectiveEncode() { |
| 73 | |
| 74 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 75 | NextTreatment nextTreatment = DefaultNextTreatment.of(treatment, 5); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 76 | |
| 77 | NextObjective nextObj = DefaultNextObjective.builder() |
| 78 | .makePermanent() |
| 79 | .withType(NextObjective.Type.HASHED) |
| 80 | .fromApp(APP_ID) |
| 81 | .withPriority(60) |
| 82 | .withId(5) |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 83 | .addTreatment(nextTreatment) |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 84 | .add(); |
| 85 | |
| 86 | ObjectNode nextObjJson = nextObjectiveCodec.encode(nextObj, context); |
| 87 | assertThat(nextObjJson, matchesNextObjective(nextObj)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Test decoding of a NextObjective object. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testNextObjectiveDecode() throws IOException { |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 95 | |
| 96 | ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID); |
| 97 | |
| 98 | expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes(); |
| 99 | replay(mockCoreService); |
| 100 | |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 101 | NextObjective nextObjective = getNextObjective("NextObjective.json"); |
| 102 | |
| 103 | assertThat(nextObjective.type(), is(NextObjective.Type.FAILOVER)); |
| 104 | assertThat(nextObjective.op(), is(NextObjective.Operation.ADD)); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Reads in a nextObjective from the given resource and decodes it. |
| 109 | * |
| 110 | * @param resourceName resource to use to read the JSON for the rule |
| 111 | * @return decoded nextObjective |
| 112 | * @throws IOException if processing the resource fails |
| 113 | */ |
| 114 | private NextObjective getNextObjective(String resourceName) throws IOException { |
| 115 | InputStream jsonStream = NextObjectiveCodecTest.class.getResourceAsStream(resourceName); |
| 116 | JsonNode json = context.mapper().readTree(jsonStream); |
| 117 | assertThat(json, notNullValue()); |
| 118 | NextObjective nextObjective = nextObjectiveCodec.decode((ObjectNode) json, context); |
| 119 | assertThat(nextObjective, notNullValue()); |
| 120 | return nextObjective; |
| 121 | } |
| 122 | } |