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 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 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; |
| 24 | import org.onosproject.codec.CodecService; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.codec.impl.CodecManager; |
| 27 | import org.onosproject.mapping.actions.MappingAction; |
| 28 | import org.onosproject.mapping.actions.MappingActions; |
| 29 | import org.onosproject.mapping.actions.NoMappingAction; |
| 30 | import org.onosproject.mapping.actions.DropMappingAction; |
| 31 | import org.onosproject.mapping.actions.ForwardMappingAction; |
| 32 | import org.onosproject.mapping.actions.NativeForwardMappingAction; |
| 33 | import org.onosproject.mapping.web.MappingCodecRegistrator; |
| 34 | |
| 35 | import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | import static org.hamcrest.Matchers.notNullValue; |
| 37 | import static org.onosproject.mapping.web.codec.MappingActionJsonMatcher.matchesAction; |
| 38 | |
| 39 | /** |
| 40 | * Unit tests for MappingActionCodec. |
| 41 | */ |
| 42 | public class MappingActionCodecTest { |
| 43 | |
| 44 | private CodecContext context; |
| 45 | private JsonCodec<MappingAction> actionCodec; |
| 46 | private MappingCodecRegistrator registrator; |
| 47 | |
| 48 | /** |
| 49 | * Sets up for each test. |
| 50 | * Creates a context and fetches the mapping action codec. |
| 51 | */ |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | CodecManager manager = new CodecManager(); |
| 55 | registrator = new MappingCodecRegistrator(); |
| 56 | registrator.codecService = manager; |
| 57 | registrator.activate(); |
| 58 | |
| 59 | context = new MappingActionCodecTest.MappingTestContext(registrator.codecService); |
| 60 | actionCodec = context.codec(MappingAction.class); |
| 61 | assertThat(actionCodec, notNullValue()); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Deactivates the codec registrator. |
| 66 | */ |
| 67 | @After |
| 68 | public void tearDown() { |
| 69 | registrator.deactivate(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Tests the encoding of no mapping action. |
| 74 | */ |
| 75 | @Test |
| 76 | public void noActionTest() { |
| 77 | final NoMappingAction action = MappingActions.noAction(); |
| 78 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 79 | assertThat(actionJson, matchesAction(action)); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests the encoding of drop mapping action. |
| 84 | */ |
| 85 | @Test |
| 86 | public void dropActionTest() { |
| 87 | final DropMappingAction action = MappingActions.drop(); |
| 88 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 89 | assertThat(actionJson, matchesAction(action)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests the encoding of forward mapping action. |
| 94 | */ |
| 95 | @Test |
| 96 | public void forwardActionTest() { |
| 97 | final ForwardMappingAction action = MappingActions.forward(); |
| 98 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 99 | assertThat(actionJson, matchesAction(action)); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Tests the encoding of native forwarding mapping action. |
| 104 | */ |
| 105 | @Test |
| 106 | public void nativeForwardActionTest() { |
| 107 | final NativeForwardMappingAction action = MappingActions.nativeForward(); |
| 108 | final ObjectNode actionJson = actionCodec.encode(action, context); |
| 109 | assertThat(actionJson, matchesAction(action)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Test mapping codec context. |
| 114 | */ |
| 115 | private class MappingTestContext implements CodecContext { |
| 116 | private final ObjectMapper mapper = new ObjectMapper(); |
| 117 | private final CodecService manager; |
| 118 | |
| 119 | /** |
| 120 | * Constructs a new mock codec context. |
| 121 | */ |
| 122 | public MappingTestContext(CodecService manager) { |
| 123 | this.manager = manager; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public ObjectMapper mapper() { |
| 128 | return mapper; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 133 | return manager.getCodec(entityClass); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public <T> T getService(Class<T> serviceClass) { |
| 138 | return null; |
| 139 | } |
| 140 | } |
| 141 | } |