Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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.mapping.web.codec; |
| 17 | |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.codec.CodecContext; |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.codec.impl.CodecManager; |
| 25 | import org.onosproject.mapping.actions.MappingAction; |
| 26 | import org.onosproject.mapping.actions.MappingActions; |
| 27 | import org.onosproject.mapping.actions.NoMappingAction; |
| 28 | import org.onosproject.mapping.actions.DropMappingAction; |
| 29 | import org.onosproject.mapping.actions.ForwardMappingAction; |
| 30 | import org.onosproject.mapping.actions.NativeForwardMappingAction; |
| 31 | import org.onosproject.mapping.web.MappingCodecRegistrator; |
| 32 | |
| 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.Matchers.notNullValue; |
| 35 | import static org.onosproject.mapping.web.codec.MappingActionJsonMatcher.matchesAction; |
| 36 | |
| 37 | /** |
| 38 | * Unit tests for MappingActionCodec. |
| 39 | */ |
| 40 | public class MappingActionCodecTest { |
| 41 | |
| 42 | private CodecContext context; |
| 43 | private JsonCodec<MappingAction> actionCodec; |
| 44 | private MappingCodecRegistrator registrator; |
| 45 | |
| 46 | /** |
| 47 | * Sets up for each test. |
| 48 | * Creates a context and fetches the mapping action codec. |
| 49 | */ |
| 50 | @Before |
| 51 | public void setUp() { |
| 52 | CodecManager manager = new CodecManager(); |
| 53 | registrator = new MappingCodecRegistrator(); |
| 54 | registrator.codecService = manager; |
| 55 | registrator.activate(); |
| 56 | |
Jian Li | fa69be6 | 2017-04-05 16:00:10 +0900 | [diff] [blame] | 57 | context = new MappingCodecContextAdapter(registrator.codecService); |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 58 | actionCodec = context.codec(MappingAction.class); |
| 59 | assertThat(actionCodec, notNullValue()); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Deactivates the codec registrator. |
| 64 | */ |
| 65 | @After |
| 66 | public void tearDown() { |
| 67 | registrator.deactivate(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests the encoding of no mapping action. |
| 72 | */ |
| 73 | @Test |
| 74 | public void noActionTest() { |
| 75 | final NoMappingAction action = MappingActions.noAction(); |
| 76 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 77 | assertThat(actionJson, matchesAction(action)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Tests the encoding of drop mapping action. |
| 82 | */ |
| 83 | @Test |
| 84 | public void dropActionTest() { |
| 85 | final DropMappingAction action = MappingActions.drop(); |
| 86 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 87 | assertThat(actionJson, matchesAction(action)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Tests the encoding of forward mapping action. |
| 92 | */ |
| 93 | @Test |
| 94 | public void forwardActionTest() { |
| 95 | final ForwardMappingAction action = MappingActions.forward(); |
| 96 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 97 | assertThat(actionJson, matchesAction(action)); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests the encoding of native forwarding mapping action. |
| 102 | */ |
| 103 | @Test |
| 104 | public void nativeForwardActionTest() { |
| 105 | final NativeForwardMappingAction action = MappingActions.nativeForward(); |
| 106 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 107 | assertThat(actionJson, matchesAction(action)); |
| 108 | } |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 109 | } |