Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.config.basics; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertTrue; |
| 26 | import static org.onosproject.net.config.basics.BasicElementConfig.ZERO_THRESHOLD; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for {@link BasicElementConfig}. |
| 30 | */ |
| 31 | public class BasicElementConfigTest { |
| 32 | |
| 33 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 34 | |
| 35 | private static final String E1 = "e1"; |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 36 | private static final String GEO = "geo"; |
| 37 | private static final String GRID = "grid"; |
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() { |
| 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 Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 100 | |
| 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 Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 109 | |
| 110 | @Test |
| 111 | public void defaultGridCoords() { |
| 112 | print(cfg); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 113 | assertFalse("grid not origin?", cfg.gridCoordsSet()); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 114 | 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 Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 122 | assertTrue("grid at origin?", cfg.gridCoordsSet()); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 123 | 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 Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 153 | } |