Jian Li | b5d8221 | 2017-04-25 01:45:18 +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 | b5d8221 | 2017-04-25 01:45:18 +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 | */ |
| 16 | package org.onosproject.mapping.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.mapping.DefaultMapping; |
| 30 | import org.onosproject.mapping.DefaultMappingEntry; |
| 31 | import org.onosproject.mapping.DefaultMappingKey; |
| 32 | import org.onosproject.mapping.DefaultMappingTreatment; |
| 33 | import org.onosproject.mapping.DefaultMappingValue; |
| 34 | import org.onosproject.mapping.Mapping; |
| 35 | import org.onosproject.mapping.MappingCodecRegistrator; |
| 36 | import org.onosproject.mapping.MappingEntry; |
| 37 | import org.onosproject.mapping.MappingEntry.MappingEntryState; |
| 38 | import org.onosproject.mapping.MappingKey; |
| 39 | import org.onosproject.mapping.MappingTreatment; |
| 40 | import org.onosproject.mapping.MappingValue; |
| 41 | import org.onosproject.mapping.actions.MappingAction; |
| 42 | import org.onosproject.mapping.actions.MappingActions; |
| 43 | import org.onosproject.mapping.addresses.MappingAddress; |
| 44 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 45 | import org.onosproject.mapping.codec.MappingKeyCodecTest.MappingKeyJsonMatcher; |
| 46 | import org.onosproject.mapping.codec.MappingValueCodecTest.MappingValueJsonMatcher; |
| 47 | import org.onosproject.mapping.instructions.MappingInstruction; |
| 48 | import org.onosproject.mapping.instructions.MappingInstructions; |
| 49 | import org.onosproject.net.DeviceId; |
| 50 | |
| 51 | import static org.hamcrest.MatcherAssert.assertThat; |
| 52 | import static org.hamcrest.Matchers.notNullValue; |
| 53 | |
| 54 | /** |
| 55 | * Unit tests for MappingEntryCodec. |
| 56 | */ |
| 57 | public class MappingEntryCodecTest { |
| 58 | |
| 59 | private static final String IPV4_STRING = "1.2.3.4"; |
| 60 | private static final String PORT_STRING = "32"; |
| 61 | private static final IpPrefix IPV4_PREFIX = |
| 62 | IpPrefix.valueOf(IPV4_STRING + "/" + PORT_STRING); |
| 63 | |
| 64 | private static final int UNICAST_WEIGHT = 1; |
| 65 | private static final int UNICAST_PRIORITY = 1; |
| 66 | private static final int MULTICAST_WEIGHT = 2; |
| 67 | private static final int MULTICAST_PRIORITY = 2; |
| 68 | |
| 69 | private static final long ID = 1L; |
| 70 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("lisp:5.6.7.8"); |
| 71 | private static final MappingEntryState STATE = MappingEntryState.ADDED; |
| 72 | |
| 73 | private CodecContext context; |
| 74 | private JsonCodec<MappingEntry> entryCodec; |
| 75 | private MappingCodecRegistrator registrator; |
| 76 | |
| 77 | /** |
| 78 | * Sets up for each test. |
| 79 | * Creates a context and fetches the mapping entry codec. |
| 80 | */ |
| 81 | @Before |
| 82 | public void setUp() { |
| 83 | CodecManager manager = new CodecManager(); |
| 84 | registrator = new MappingCodecRegistrator(); |
| 85 | registrator.codecService = manager; |
| 86 | registrator.activate(); |
| 87 | |
| 88 | context = new MappingCodecContextAdapter(registrator.codecService); |
| 89 | entryCodec = context.codec(MappingEntry.class); |
| 90 | assertThat(entryCodec, notNullValue()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Deactivates the codec registrator. |
| 95 | */ |
| 96 | @After |
| 97 | public void tearDown() { |
| 98 | registrator.deactivate(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Tests encoding of a mapping entry object. |
| 103 | */ |
| 104 | @Test |
| 105 | public void testMappingEntryEncode() { |
| 106 | MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX); |
| 107 | |
| 108 | MappingInstruction unicastWeight = MappingInstructions.unicastWeight(UNICAST_WEIGHT); |
| 109 | MappingInstruction unicastPriority = MappingInstructions.unicastPriority(UNICAST_PRIORITY); |
| 110 | MappingInstruction multicastWeight = MappingInstructions.multicastWeight(MULTICAST_WEIGHT); |
| 111 | MappingInstruction multicastPriority = MappingInstructions.multicastPriority(MULTICAST_PRIORITY); |
| 112 | |
| 113 | MappingKey key = DefaultMappingKey.builder() |
| 114 | .withAddress(address) |
| 115 | .build(); |
| 116 | |
| 117 | MappingTreatment treatment = DefaultMappingTreatment.builder() |
| 118 | .add(unicastWeight) |
| 119 | .add(unicastPriority) |
| 120 | .add(multicastWeight) |
| 121 | .add(multicastPriority) |
| 122 | .withAddress(address) |
| 123 | .build(); |
| 124 | |
| 125 | MappingAction action = MappingActions.noAction(); |
| 126 | |
| 127 | MappingValue value = DefaultMappingValue.builder() |
| 128 | .add(treatment) |
| 129 | .withAction(action) |
| 130 | .build(); |
| 131 | |
| 132 | Mapping mapping = DefaultMapping.builder() |
| 133 | .withId(ID) |
| 134 | .forDevice(DEVICE_ID) |
| 135 | .withKey(key) |
| 136 | .withValue(value) |
| 137 | .build(); |
| 138 | |
| 139 | MappingEntry entry = new DefaultMappingEntry(mapping, STATE); |
| 140 | |
| 141 | ObjectNode entryJson = entryCodec.encode(entry, context); |
| 142 | assertThat(entryJson, MappingEntryJsonMatcher.matchesMappingEntry(entry)); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Hamcrest matcher for mapping entry. |
| 147 | */ |
| 148 | public static final class MappingEntryJsonMatcher |
| 149 | extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 150 | |
| 151 | private final MappingEntry mappingEntry; |
| 152 | |
| 153 | /** |
| 154 | * A default constructor. |
| 155 | * |
| 156 | * @param mappingEntry mapping entry |
| 157 | */ |
| 158 | private MappingEntryJsonMatcher(MappingEntry mappingEntry) { |
| 159 | this.mappingEntry = mappingEntry; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 164 | |
| 165 | // check mapping id |
| 166 | final JsonNode jsonIdNode = jsonNode.get(MappingEntryCodec.ID); |
| 167 | final long jsonId = jsonIdNode.asLong(); |
| 168 | final long id = mappingEntry.id().value(); |
| 169 | if (jsonId != id) { |
| 170 | description.appendText("mapping id was " + id); |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | // check device id |
| 175 | final JsonNode jsonDeviceIdNode = jsonNode.get(MappingEntryCodec.DEVICE_ID); |
| 176 | final String jsonDeviceId = jsonDeviceIdNode.textValue(); |
| 177 | final String deviceId = mappingEntry.deviceId().toString(); |
| 178 | if (!jsonDeviceId.equals(deviceId)) { |
| 179 | description.appendText("device id was " + deviceId); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // check state |
| 184 | final JsonNode jsonStateNode = jsonNode.get(MappingEntryCodec.STATE); |
| 185 | final String jsonState = jsonStateNode.textValue(); |
| 186 | final String state = mappingEntry.state().name(); |
| 187 | if (!jsonState.equals(state)) { |
| 188 | description.appendText("state was " + state); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | // check mapping key |
| 193 | final JsonNode jsonKeyNode = jsonNode.get(MappingEntryCodec.KEY); |
| 194 | assertThat(jsonKeyNode, MappingKeyJsonMatcher.matchesMappingKey(mappingEntry.key())); |
| 195 | |
| 196 | // check mapping value |
| 197 | final JsonNode jsonValueNode = jsonNode.get(MappingEntryCodec.VALUE); |
| 198 | assertThat(jsonValueNode, MappingValueJsonMatcher.matchesMappingValue(mappingEntry.value())); |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void describeTo(Description description) { |
| 205 | description.appendText(mappingEntry.toString()); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Factory to allocate a mapping entry matcher. |
| 210 | * |
| 211 | * @param entry mapping entry object we are looking for |
| 212 | * @return matcher |
| 213 | */ |
| 214 | public static MappingEntryJsonMatcher matchesMappingEntry(MappingEntry entry) { |
| 215 | return new MappingEntryJsonMatcher(entry); |
| 216 | } |
| 217 | } |
| 218 | } |