Jian Li | 4508352 | 2017-03-20 18:46:07 +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 | 4508352 | 2017-03-20 18:46:07 +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.onlab.packet.IpPrefix; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.codec.CodecContext; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.codec.impl.CodecManager; |
| 27 | import org.onosproject.mapping.addresses.MappingAddress; |
| 28 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 29 | import org.onosproject.mapping.web.MappingCodecRegistrator; |
| 30 | |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.notNullValue; |
| 33 | import static org.onosproject.mapping.web.codec.MappingAddressJsonMatcher.matchesMappingAddress; |
| 34 | |
| 35 | /** |
| 36 | * Unit tests for MappingAddressCodec. |
| 37 | */ |
| 38 | public class MappingAddressCodecTest { |
| 39 | |
| 40 | private CodecContext context; |
| 41 | private JsonCodec<MappingAddress> addressCodec; |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 42 | private MappingCodecRegistrator registrator; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 43 | private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24"); |
| 44 | private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64"); |
| 45 | private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01"); |
| 46 | private static final String DN = "onos"; |
| 47 | private static final String AS = "AS1000"; |
| 48 | |
| 49 | /** |
| 50 | * Sets up for each test. |
| 51 | * Creates a context and fetches the mapping address codec. |
| 52 | */ |
| 53 | @Before |
| 54 | public void setUp() { |
| 55 | CodecManager manager = new CodecManager(); |
| 56 | registrator = new MappingCodecRegistrator(); |
| 57 | registrator.codecService = manager; |
| 58 | registrator.activate(); |
| 59 | |
Jian Li | fa69be6 | 2017-04-05 16:00:10 +0900 | [diff] [blame] | 60 | context = new MappingCodecContextAdapter(registrator.codecService); |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 61 | |
| 62 | addressCodec = context.codec(MappingAddress.class); |
| 63 | assertThat(addressCodec, notNullValue()); |
| 64 | } |
| 65 | |
| 66 | @After |
| 67 | public void tearDown() { |
| 68 | registrator.deactivate(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Tests AS mapping address. |
| 73 | */ |
| 74 | @Test |
| 75 | public void asMappingAddressTest() { |
| 76 | MappingAddress address = MappingAddresses.asMappingAddress(AS); |
| 77 | ObjectNode result = addressCodec.encode(address, context); |
| 78 | assertThat(result, matchesMappingAddress(address)); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests DN mapping address. |
| 83 | */ |
| 84 | @Test |
| 85 | public void dnMappingAddressTest() { |
| 86 | MappingAddress address = MappingAddresses.dnMappingAddress(DN); |
| 87 | ObjectNode result = addressCodec.encode(address, context); |
| 88 | assertThat(result, matchesMappingAddress(address)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Tests IPv4 mapping address. |
| 93 | */ |
| 94 | @Test |
| 95 | public void ipv4MappingAddressTest() { |
| 96 | MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX); |
| 97 | ObjectNode result = addressCodec.encode(address, context); |
| 98 | assertThat(result, matchesMappingAddress(address)); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Tests IPv6 mapping address. |
| 103 | */ |
| 104 | @Test |
| 105 | public void ipv6MappingAddressTest() { |
| 106 | MappingAddress address = MappingAddresses.ipv6MappingAddress(IPV6_PREFIX); |
| 107 | ObjectNode result = addressCodec.encode(address, context); |
| 108 | assertThat(result, matchesMappingAddress(address)); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Tests Ethernet mapping address. |
| 113 | */ |
| 114 | @Test |
| 115 | public void ethMappingAddressTest() { |
| 116 | MappingAddress address = MappingAddresses.ethMappingAddress(MAC); |
| 117 | ObjectNode result = addressCodec.encode(address, context); |
| 118 | assertThat(result, matchesMappingAddress(address)); |
| 119 | } |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 120 | } |