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