blob: fe65c9c6fe47fbdf58a481484678b5ca3fd358c6 [file] [log] [blame]
Jian Lic4d03912017-02-06 00:09:17 +09001/*
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 */
16package org.onosproject.lisp.msg.types.lcaf;
17
18import com.google.common.testing.EqualsTester;
19import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onosproject.lisp.msg.exceptions.LispParseError;
25import org.onosproject.lisp.msg.exceptions.LispReaderException;
26import org.onosproject.lisp.msg.exceptions.LispWriterException;
27import org.onosproject.lisp.msg.types.LispIpv4Address;
28import org.onosproject.lisp.msg.types.lcaf.LispGeoCoordinateLcafAddress.GeoCoordinateAddressBuilder;
29import org.onosproject.lisp.msg.types.lcaf.LispGeoCoordinateLcafAddress.GeoCoordinateLcafAddressReader;
30import org.onosproject.lisp.msg.types.lcaf.LispGeoCoordinateLcafAddress.GeoCoordinateLcafAddressWriter;
31
32import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.Matchers.is;
34
35/**
36 * Unit tests for LispGeoCoordinateLcafAddress class.
37 */
38public class LispGeoCoordinateLcafAddressTest {
39
40 private static final String IP_ADDRESS_1 = "192.168.1.1";
41 private static final String IP_ADDRESS_2 = "192.168.1.2";
42
43 private LispGeoCoordinateLcafAddress address1;
44 private LispGeoCoordinateLcafAddress sameAsAddress1;
45 private LispGeoCoordinateLcafAddress address2;
46
47 @Before
48 public void setup() {
49
50 GeoCoordinateAddressBuilder builder1 = new GeoCoordinateAddressBuilder();
51
52 LispIpv4Address ipv4Address1 =
53 new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1));
54
55 address1 = builder1
56 .withIsNorth(true)
57 .withLatitudeDegree((short) 1)
58 .withLatitudeMinute((byte) 1)
59 .withLatitudeSecond((byte) 1)
60 .withIsEast(false)
61 .withLongitudeDegree((short) 1)
62 .withLongitudeMinute((byte) 1)
63 .withLongitudeSecond((byte) 1)
64 .withAltitude(1)
65 .withAddress(ipv4Address1)
66 .build();
67
68 GeoCoordinateAddressBuilder builder2 = new GeoCoordinateAddressBuilder();
69
70 sameAsAddress1 = builder2
71 .withIsNorth(true)
72 .withLatitudeDegree((short) 1)
73 .withLatitudeMinute((byte) 1)
74 .withLatitudeSecond((byte) 1)
75 .withIsEast(false)
76 .withLongitudeDegree((short) 1)
77 .withLongitudeMinute((byte) 1)
78 .withLongitudeSecond((byte) 1)
79 .withAltitude(1)
80 .withAddress(ipv4Address1)
81 .build();
82
83 GeoCoordinateAddressBuilder builder3 = new GeoCoordinateAddressBuilder();
84
85 LispIpv4Address ipv4Address2 =
86 new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_2));
87
88 address2 = builder3
89 .withIsNorth(false)
90 .withLatitudeDegree((short) 2)
91 .withLatitudeMinute((byte) 2)
92 .withLatitudeSecond((byte) 2)
93 .withIsEast(true)
94 .withLongitudeDegree((short) 2)
95 .withLongitudeMinute((byte) 2)
96 .withLongitudeSecond((byte) 2)
97 .withAltitude(2)
98 .withAddress(ipv4Address2)
99 .build();
100 }
101
102 @Test
103 public void testEquality() {
104 new EqualsTester()
105 .addEqualityGroup(address1, sameAsAddress1)
106 .addEqualityGroup(address2).testEquals();
107 }
108
109 @Test
110 public void testConstruction() {
111 LispGeoCoordinateLcafAddress address = address1;
112
113 LispIpv4Address ipv4Address =
114 new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1));
115
116 assertThat(address.isNorth(), is(true));
117 assertThat(address.getLatitudeDegree(), is((short) 1));
118 assertThat(address.getLatitudeMinute(), is((byte) 1));
119 assertThat(address.getLatitudeSecond(), is((byte) 1));
120 assertThat(address.isEast(), is(false));
121 assertThat(address.getLongitudeDegree(), is((short) 1));
122 assertThat(address.getLongitudeMinute(), is((byte) 1));
123 assertThat(address.getLongitudeSecond(), is((byte) 1));
124 assertThat(address.getAltitude(), is(1));
125 assertThat(address.getAddress(), is(ipv4Address));
126 }
127
128 @Test
129 public void testSerialization() throws LispWriterException,
130 LispParseError, LispReaderException {
131
132 ByteBuf byteBuf = Unpooled.buffer();
133
134 GeoCoordinateLcafAddressWriter writer = new GeoCoordinateLcafAddressWriter();
135 writer.writeTo(byteBuf, address1);
136
137 GeoCoordinateLcafAddressReader reader = new GeoCoordinateLcafAddressReader();
138 LispGeoCoordinateLcafAddress deserialized = reader.readFrom(byteBuf);
139
140 new EqualsTester().addEqualityGroup(address1, deserialized).testEquals();
141 }
142}