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"; |
| 36 | |
| 37 | // concrete subclass of abstract class we are testing |
| 38 | private static class ElmCfg extends BasicElementConfig<String> { |
| 39 | ElmCfg() { |
| 40 | object = MAPPER.createObjectNode(); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public String toString() { |
| 45 | return object.toString(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private static void print(String fmt, Object... args) { |
| 50 | System.out.println(String.format(fmt, args)); |
| 51 | } |
| 52 | |
| 53 | private static void print(Object o) { |
| 54 | print("%s", o); |
| 55 | } |
| 56 | |
| 57 | private BasicElementConfig<?> cfg; |
| 58 | |
| 59 | @Before |
| 60 | public void setUp() { |
| 61 | cfg = new ElmCfg().name(E1); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void basicNoGeo() { |
| 66 | print(cfg); |
| 67 | assertFalse("geo set?", cfg.geoCoordsSet()); |
| 68 | assertEquals("lat", 0.0, cfg.latitude(), ZERO_THRESHOLD); |
| 69 | assertEquals("lon", 0.0, cfg.longitude(), ZERO_THRESHOLD); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void geoLatitudeOnly() { |
| 74 | cfg.latitude(0.1); |
| 75 | print(cfg); |
| 76 | assertTrue("geo NOT set", cfg.geoCoordsSet()); |
| 77 | assertEquals("lat", 0.1, cfg.latitude(), ZERO_THRESHOLD); |
| 78 | assertEquals("lon", 0.0, cfg.longitude(), ZERO_THRESHOLD); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void geoLongitudeOnly() { |
| 83 | cfg.longitude(-0.1); |
| 84 | print(cfg); |
| 85 | assertTrue("geo NOT set", cfg.geoCoordsSet()); |
| 86 | assertEquals("lat", 0.0, cfg.latitude(), ZERO_THRESHOLD); |
| 87 | assertEquals("lon", -0.1, cfg.longitude(), ZERO_THRESHOLD); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void geoLatLong() { |
| 92 | cfg.latitude(3.1415).longitude(2.71828); |
| 93 | print(cfg); |
| 94 | assertTrue("geo NOT set", cfg.geoCoordsSet()); |
| 95 | assertEquals("lat", 3.1415, cfg.latitude(), ZERO_THRESHOLD); |
| 96 | assertEquals("lon", 2.71828, cfg.longitude(), ZERO_THRESHOLD); |
| 97 | } |
| 98 | } |