Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.net.config.basics; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | 3516f06 | 2018-03-19 10:12:25 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | |
Thomas Vachuska | 3516f06 | 2018-03-19 10:12:25 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.*; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 25 | import static org.onosproject.net.config.basics.BasicElementConfig.ZERO_THRESHOLD; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for {@link BasicElementConfig}. |
| 29 | */ |
| 30 | public class BasicElementConfigTest { |
| 31 | |
| 32 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 33 | |
| 34 | private static final String E1 = "e1"; |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 35 | private static final String GEO = BasicElementConfig.LOC_TYPE_GEO; |
| 36 | private static final String GRID = BasicElementConfig.LOC_TYPE_GRID; |
Thomas Vachuska | 3516f06 | 2018-03-19 10:12:25 -0700 | [diff] [blame] | 37 | public static final ImmutableSet<String> ROLES = ImmutableSet.of("spine", "primary"); |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 38 | |
| 39 | // concrete subclass of abstract class we are testing |
| 40 | private static class ElmCfg extends BasicElementConfig<String> { |
| 41 | ElmCfg() { |
Thomas Vachuska | 3516f06 | 2018-03-19 10:12:25 -0700 | [diff] [blame] | 42 | mapper = MAPPER; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 43 | 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 Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 101 | |
| 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 Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 110 | |
| 111 | @Test |
| 112 | public void defaultGridCoords() { |
| 113 | print(cfg); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 114 | assertFalse("grid not origin?", cfg.gridCoordsSet()); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 115 | 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 Vachuska | c67a991 | 2018-03-06 14:37:45 -0800 | [diff] [blame] | 121 | cfg.gridX(35.0).gridY(49.7).locType(GRID); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 122 | print(cfg); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 123 | assertTrue("grid at origin?", cfg.gridCoordsSet()); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 124 | 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 Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 131 | assertEquals("not none", BasicElementConfig.LOC_TYPE_NONE, cfg.locType()); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 132 | } |
| 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 Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 152 | assertEquals("not none", BasicElementConfig.LOC_TYPE_NONE, cfg.locType()); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 153 | } |
Thomas Vachuska | 3516f06 | 2018-03-19 10:12:25 -0700 | [diff] [blame] | 154 | |
| 155 | @Test |
| 156 | public void roles() { |
| 157 | cfg.roles(ROLES); |
| 158 | print(cfg); |
| 159 | assertEquals("not roles", ROLES, cfg.roles()); |
| 160 | } |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 161 | } |