blob: 1c41843d85fcd7c339c2be3d7e75578a18db3d26 [file] [log] [blame]
Simon Huntf4fd2a22016-08-10 15:41:09 -07001/*
2 * Copyright 2016-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
17package org.onosproject.net.config.basics;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import org.junit.Before;
21import org.junit.Test;
22
23import static org.junit.Assert.assertEquals;
24import static org.junit.Assert.assertFalse;
25import static org.junit.Assert.assertTrue;
26import static org.onosproject.net.config.basics.BasicElementConfig.ZERO_THRESHOLD;
27
28/**
29 * Unit tests for {@link BasicElementConfig}.
30 */
31public class BasicElementConfigTest {
32
33 private static final ObjectMapper MAPPER = new ObjectMapper();
34
35 private static final String E1 = "e1";
Simon Hunteb3cf542017-02-10 13:18:41 -080036 private static final String GEO = "geo";
37 private static final String GRID = "grid";
Simon Huntf4fd2a22016-08-10 15:41:09 -070038
39 // concrete subclass of abstract class we are testing
40 private static class ElmCfg extends BasicElementConfig<String> {
41 ElmCfg() {
42 object = MAPPER.createObjectNode();
43 }
44
45 @Override
46 public String toString() {
47 return object.toString();
48 }
49 }
50
51 private static void print(String fmt, Object... args) {
52 System.out.println(String.format(fmt, args));
53 }
54
55 private static void print(Object o) {
56 print("%s", o);
57 }
58
59 private BasicElementConfig<?> cfg;
60
61 @Before
62 public void setUp() {
63 cfg = new ElmCfg().name(E1);
64 }
65
66 @Test
67 public void basicNoGeo() {
68 print(cfg);
69 assertFalse("geo set?", cfg.geoCoordsSet());
70 assertEquals("lat", 0.0, cfg.latitude(), ZERO_THRESHOLD);
71 assertEquals("lon", 0.0, cfg.longitude(), ZERO_THRESHOLD);
72 }
73
74 @Test
75 public void geoLatitudeOnly() {
76 cfg.latitude(0.1);
77 print(cfg);
78 assertTrue("geo NOT set", cfg.geoCoordsSet());
79 assertEquals("lat", 0.1, cfg.latitude(), ZERO_THRESHOLD);
80 assertEquals("lon", 0.0, cfg.longitude(), ZERO_THRESHOLD);
81 }
82
83 @Test
84 public void geoLongitudeOnly() {
85 cfg.longitude(-0.1);
86 print(cfg);
87 assertTrue("geo NOT set", cfg.geoCoordsSet());
88 assertEquals("lat", 0.0, cfg.latitude(), ZERO_THRESHOLD);
89 assertEquals("lon", -0.1, cfg.longitude(), ZERO_THRESHOLD);
90 }
91
92 @Test
93 public void geoLatLong() {
94 cfg.latitude(3.1415).longitude(2.71828);
95 print(cfg);
96 assertTrue("geo NOT set", cfg.geoCoordsSet());
97 assertEquals("lat", 3.1415, cfg.latitude(), ZERO_THRESHOLD);
98 assertEquals("lon", 2.71828, cfg.longitude(), ZERO_THRESHOLD);
99 }
Simon Hunt1e20dae2016-10-28 11:26:26 -0700100
101 @Test
102 public void uiType() {
103 print(cfg);
104 assertEquals("not default type", null, cfg.uiType());
105 cfg.uiType("someOtherType");
106 print(cfg);
107 assertEquals("not other type", "someOtherType", cfg.uiType());
108 }
Simon Hunteb3cf542017-02-10 13:18:41 -0800109
110 @Test
111 public void defaultGridCoords() {
112 print(cfg);
Simon Huntbc30e682017-02-15 18:39:23 -0800113 assertFalse("grid not origin?", cfg.gridCoordsSet());
Simon Hunteb3cf542017-02-10 13:18:41 -0800114 assertEquals("gridx", 0.0, cfg.gridX(), ZERO_THRESHOLD);
115 assertEquals("gridy", 0.0, cfg.gridY(), ZERO_THRESHOLD);
116 }
117
118 @Test
119 public void someGridCoords() {
120 cfg.gridX(35.0).gridY(49.7);
121 print(cfg);
Simon Huntbc30e682017-02-15 18:39:23 -0800122 assertTrue("grid at origin?", cfg.gridCoordsSet());
Simon Hunteb3cf542017-02-10 13:18:41 -0800123 assertEquals("gridx", 35.0, cfg.gridX(), ZERO_THRESHOLD);
124 assertEquals("gridy", 49.7, cfg.gridY(), ZERO_THRESHOLD);
125 }
126
127 @Test
128 public void defaultLocationType() {
129 print(cfg);
130 assertEquals("not geo", GEO, cfg.locType());
131 }
132
133 @Test
134 public void geoLocationType() {
135 cfg.locType(GEO);
136 print(cfg);
137 assertEquals("not geo", GEO, cfg.locType());
138 }
139
140 @Test
141 public void gridLocationType() {
142 cfg.locType(GRID);
143 print(cfg);
144 assertEquals("not grid", GRID, cfg.locType());
145 }
146
147 @Test
148 public void otherLocationType() {
149 cfg.locType("foobar");
150 print(cfg);
151 assertEquals("not geo", GEO, cfg.locType());
152 }
Simon Huntf4fd2a22016-08-10 15:41:09 -0700153}