blob: f2e4b993bfb0ffc35de52119c04b1b2192e340ec [file] [log] [blame]
Simon Huntf4fd2a22016-08-10 15:41:09 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Simon Huntf4fd2a22016-08-10 15:41:09 -07003 *
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;
Thomas Vachuska3516f062018-03-19 10:12:25 -070020import com.google.common.collect.ImmutableSet;
Simon Huntf4fd2a22016-08-10 15:41:09 -070021import org.junit.Before;
22import org.junit.Test;
23
Thomas Vachuska3516f062018-03-19 10:12:25 -070024import static org.junit.Assert.*;
Simon Huntf4fd2a22016-08-10 15:41:09 -070025import static org.onosproject.net.config.basics.BasicElementConfig.ZERO_THRESHOLD;
26
27/**
28 * Unit tests for {@link BasicElementConfig}.
29 */
30public class BasicElementConfigTest {
31
32 private static final ObjectMapper MAPPER = new ObjectMapper();
33
34 private static final String E1 = "e1";
Thomas Vachuskac616e172018-04-17 16:57:12 -070035 private static final String GEO = BasicElementConfig.LOC_TYPE_GEO;
36 private static final String GRID = BasicElementConfig.LOC_TYPE_GRID;
Thomas Vachuska3516f062018-03-19 10:12:25 -070037 public static final ImmutableSet<String> ROLES = ImmutableSet.of("spine", "primary");
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() {
Thomas Vachuska3516f062018-03-19 10:12:25 -070042 mapper = MAPPER;
Simon Huntf4fd2a22016-08-10 15:41:09 -070043 object = MAPPER.createObjectNode();
44 }
45
46 @Override
47 public String toString() {
48 return object.toString();
49 }
50 }
51
52 private static void print(String fmt, Object... args) {
53 System.out.println(String.format(fmt, args));
54 }
55
56 private static void print(Object o) {
57 print("%s", o);
58 }
59
60 private BasicElementConfig<?> cfg;
61
62 @Before
63 public void setUp() {
64 cfg = new ElmCfg().name(E1);
65 }
66
67 @Test
68 public void basicNoGeo() {
69 print(cfg);
70 assertFalse("geo set?", cfg.geoCoordsSet());
71 assertEquals("lat", 0.0, cfg.latitude(), ZERO_THRESHOLD);
72 assertEquals("lon", 0.0, cfg.longitude(), ZERO_THRESHOLD);
73 }
74
75 @Test
76 public void geoLatitudeOnly() {
77 cfg.latitude(0.1);
78 print(cfg);
79 assertTrue("geo NOT set", cfg.geoCoordsSet());
80 assertEquals("lat", 0.1, cfg.latitude(), ZERO_THRESHOLD);
81 assertEquals("lon", 0.0, cfg.longitude(), ZERO_THRESHOLD);
82 }
83
84 @Test
85 public void geoLongitudeOnly() {
86 cfg.longitude(-0.1);
87 print(cfg);
88 assertTrue("geo NOT set", cfg.geoCoordsSet());
89 assertEquals("lat", 0.0, cfg.latitude(), ZERO_THRESHOLD);
90 assertEquals("lon", -0.1, cfg.longitude(), ZERO_THRESHOLD);
91 }
92
93 @Test
94 public void geoLatLong() {
95 cfg.latitude(3.1415).longitude(2.71828);
96 print(cfg);
97 assertTrue("geo NOT set", cfg.geoCoordsSet());
98 assertEquals("lat", 3.1415, cfg.latitude(), ZERO_THRESHOLD);
99 assertEquals("lon", 2.71828, cfg.longitude(), ZERO_THRESHOLD);
100 }
Simon Hunt1e20dae2016-10-28 11:26:26 -0700101
102 @Test
103 public void uiType() {
104 print(cfg);
105 assertEquals("not default type", null, cfg.uiType());
106 cfg.uiType("someOtherType");
107 print(cfg);
108 assertEquals("not other type", "someOtherType", cfg.uiType());
109 }
Simon Hunteb3cf542017-02-10 13:18:41 -0800110
111 @Test
112 public void defaultGridCoords() {
113 print(cfg);
Simon Huntbc30e682017-02-15 18:39:23 -0800114 assertFalse("grid not origin?", cfg.gridCoordsSet());
Simon Hunteb3cf542017-02-10 13:18:41 -0800115 assertEquals("gridx", 0.0, cfg.gridX(), ZERO_THRESHOLD);
116 assertEquals("gridy", 0.0, cfg.gridY(), ZERO_THRESHOLD);
117 }
118
119 @Test
120 public void someGridCoords() {
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800121 cfg.gridX(35.0).gridY(49.7).locType(GRID);
Simon Hunteb3cf542017-02-10 13:18:41 -0800122 print(cfg);
Simon Huntbc30e682017-02-15 18:39:23 -0800123 assertTrue("grid at origin?", cfg.gridCoordsSet());
Simon Hunteb3cf542017-02-10 13:18:41 -0800124 assertEquals("gridx", 35.0, cfg.gridX(), ZERO_THRESHOLD);
125 assertEquals("gridy", 49.7, cfg.gridY(), ZERO_THRESHOLD);
126 }
127
128 @Test
129 public void defaultLocationType() {
130 print(cfg);
Thomas Vachuskac616e172018-04-17 16:57:12 -0700131 assertEquals("not none", BasicElementConfig.LOC_TYPE_NONE, cfg.locType());
Simon Hunteb3cf542017-02-10 13:18:41 -0800132 }
133
134 @Test
135 public void geoLocationType() {
136 cfg.locType(GEO);
137 print(cfg);
138 assertEquals("not geo", GEO, cfg.locType());
139 }
140
141 @Test
142 public void gridLocationType() {
143 cfg.locType(GRID);
144 print(cfg);
145 assertEquals("not grid", GRID, cfg.locType());
146 }
147
148 @Test
149 public void otherLocationType() {
150 cfg.locType("foobar");
151 print(cfg);
Thomas Vachuskac616e172018-04-17 16:57:12 -0700152 assertEquals("not none", BasicElementConfig.LOC_TYPE_NONE, cfg.locType());
Simon Hunteb3cf542017-02-10 13:18:41 -0800153 }
Thomas Vachuska3516f062018-03-19 10:12:25 -0700154
155 @Test
156 public void roles() {
157 cfg.roles(ROLES);
158 print(cfg);
159 assertEquals("not roles", ROLES, cfg.roles());
160 }
Simon Huntf4fd2a22016-08-10 15:41:09 -0700161}