Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [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 | */ |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 16 | package org.onosproject.mapping.codec; |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 17 | |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.codec.CodecContext; |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.codec.impl.CodecManager; |
| 26 | import org.onosproject.mapping.actions.MappingAction; |
| 27 | import org.onosproject.mapping.actions.MappingActions; |
| 28 | import org.onosproject.mapping.actions.NoMappingAction; |
| 29 | import org.onosproject.mapping.actions.DropMappingAction; |
| 30 | import org.onosproject.mapping.actions.ForwardMappingAction; |
| 31 | import org.onosproject.mapping.actions.NativeForwardMappingAction; |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 32 | import org.onosproject.mapping.MappingCodecRegistrator; |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 33 | |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 37 | import static org.hamcrest.MatcherAssert.assertThat; |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 38 | import static org.hamcrest.Matchers.is; |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 39 | import static org.hamcrest.Matchers.notNullValue; |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 40 | import static org.onosproject.mapping.codec.MappingActionJsonMatcher.matchesAction; |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Unit tests for MappingActionCodec. |
| 44 | */ |
| 45 | public class MappingActionCodecTest { |
| 46 | |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 47 | private static final String NO_ACTION_STRING = "NO_ACTION"; |
| 48 | |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 49 | private CodecContext context; |
| 50 | private JsonCodec<MappingAction> actionCodec; |
| 51 | private MappingCodecRegistrator registrator; |
| 52 | |
| 53 | /** |
| 54 | * Sets up for each test. |
| 55 | * Creates a context and fetches the mapping action codec. |
| 56 | */ |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | CodecManager manager = new CodecManager(); |
| 60 | registrator = new MappingCodecRegistrator(); |
| 61 | registrator.codecService = manager; |
| 62 | registrator.activate(); |
| 63 | |
Jian Li | fa69be6 | 2017-04-05 16:00:10 +0900 | [diff] [blame] | 64 | context = new MappingCodecContextAdapter(registrator.codecService); |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 65 | actionCodec = context.codec(MappingAction.class); |
| 66 | assertThat(actionCodec, notNullValue()); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Deactivates the codec registrator. |
| 71 | */ |
| 72 | @After |
| 73 | public void tearDown() { |
| 74 | registrator.deactivate(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Tests the encoding of no mapping action. |
| 79 | */ |
| 80 | @Test |
| 81 | public void noActionTest() { |
| 82 | final NoMappingAction action = MappingActions.noAction(); |
| 83 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 84 | assertThat(actionJson, matchesAction(action)); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests the encoding of drop mapping action. |
| 89 | */ |
| 90 | @Test |
| 91 | public void dropActionTest() { |
| 92 | final DropMappingAction action = MappingActions.drop(); |
| 93 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 94 | assertThat(actionJson, matchesAction(action)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Tests the encoding of forward mapping action. |
| 99 | */ |
| 100 | @Test |
| 101 | public void forwardActionTest() { |
| 102 | final ForwardMappingAction action = MappingActions.forward(); |
| 103 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 104 | assertThat(actionJson, matchesAction(action)); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Tests the encoding of native forwarding mapping action. |
| 109 | */ |
| 110 | @Test |
| 111 | public void nativeForwardActionTest() { |
| 112 | final NativeForwardMappingAction action = MappingActions.nativeForward(); |
| 113 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 114 | assertThat(actionJson, matchesAction(action)); |
| 115 | } |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Tests decoding of a mapping key JSON object. |
| 119 | * |
| 120 | * @throws IOException if processing the resource fails |
| 121 | */ |
| 122 | @Test |
| 123 | public void testMappingActionDecode() throws IOException { |
| 124 | MappingAction action = getAction("MappingAction.json"); |
| 125 | assertThat(action.toString(), is(NO_ACTION_STRING)); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Reads in a mapping action from the given resource and decodes it. |
| 130 | * |
| 131 | * @param resourceName resource to use to read the JSON for the rule |
| 132 | * @return decoded mappingAction |
| 133 | * @throws IOException if processing the resource fails |
| 134 | */ |
| 135 | private MappingAction getAction(String resourceName) throws IOException { |
| 136 | InputStream jsonStream = MappingActionCodecTest.class.getResourceAsStream(resourceName); |
| 137 | JsonNode json = context.mapper().readTree(jsonStream); |
| 138 | assertThat(json, notNullValue()); |
| 139 | MappingAction action = actionCodec.decode((ObjectNode) json, context); |
| 140 | assertThat(action, notNullValue()); |
| 141 | return action; |
| 142 | } |
| 143 | } |