blob: f81f462fa0175b780da67cd56ec8e223620529d8 [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);
113 assertEquals("gridx", 0.0, cfg.gridX(), ZERO_THRESHOLD);
114 assertEquals("gridy", 0.0, cfg.gridY(), ZERO_THRESHOLD);
115 }
116
117 @Test
118 public void someGridCoords() {
119 cfg.gridX(35.0).gridY(49.7);
120 print(cfg);
121 assertEquals("gridx", 35.0, cfg.gridX(), ZERO_THRESHOLD);
122 assertEquals("gridy", 49.7, cfg.gridY(), ZERO_THRESHOLD);
123 }
124
125 @Test
126 public void defaultLocationType() {
127 print(cfg);
128 assertEquals("not geo", GEO, cfg.locType());
129 }
130
131 @Test
132 public void geoLocationType() {
133 cfg.locType(GEO);
134 print(cfg);
135 assertEquals("not geo", GEO, cfg.locType());
136 }
137
138 @Test
139 public void gridLocationType() {
140 cfg.locType(GRID);
141 print(cfg);
142 assertEquals("not grid", GRID, cfg.locType());
143 }
144
145 @Test
146 public void otherLocationType() {
147 cfg.locType("foobar");
148 print(cfg);
149 assertEquals("not geo", GEO, cfg.locType());
150 }
Simon Huntf4fd2a22016-08-10 15:41:09 -0700151}