Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 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; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | |
| 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertTrue; |
| 26 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
| 27 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 28 | |
| 29 | /** |
| 30 | * Test of the base network config class. |
| 31 | */ |
| 32 | public class ConfigTest { |
| 33 | |
| 34 | private static final String SUBJECT = "subject"; |
| 35 | private static final String KEY = "key"; |
| 36 | |
| 37 | private static final String TEXT = "text"; |
| 38 | private static final String LONG = "long"; |
| 39 | private static final String DOUBLE = "double"; |
| 40 | private static final String MAC = "mac"; |
| 41 | private static final String IP = "ip"; |
| 42 | |
| 43 | private final ObjectMapper mapper = new ObjectMapper(); |
| 44 | private final ConfigApplyDelegate delegate = new TestDelegate(); |
| 45 | |
| 46 | private Config<String> cfg; |
| 47 | private JsonNode json; |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() { |
| 51 | json = new ObjectMapper().createObjectNode() |
| 52 | .put(TEXT, "foo").put(LONG, 5).put(DOUBLE, 0.5) |
| 53 | .put(MAC, "ab:cd:ef:ca:fe:ed").put(IP, "12.34.56.78"); |
| 54 | cfg = new TestConfig(); |
| 55 | cfg.init(SUBJECT, KEY, json, mapper, delegate); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void hasOnlyFields() { |
| 60 | assertTrue("has unexpected fields", cfg.hasOnlyFields(TEXT, LONG, DOUBLE, MAC, IP)); |
| 61 | assertFalse("did not detect unexpected fields", cfg.hasOnlyFields(TEXT, LONG, DOUBLE, MAC)); |
| 62 | assertTrue("is not proper text", cfg.isString(TEXT, MANDATORY)); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void isString() { |
| 67 | assertTrue("is not proper text", cfg.isString(TEXT, MANDATORY)); |
| 68 | assertTrue("is not proper text", cfg.isString(TEXT, MANDATORY, "^f.*")); |
| 69 | assertTrue("is not proper text", cfg.isString(TEXT, OPTIONAL, "^f.*")); |
| 70 | assertTrue("is not proper text", cfg.isString(TEXT, OPTIONAL)); |
| 71 | assertTrue("is not proper text", cfg.isString("none", OPTIONAL)); |
| 72 | assertFalse("did not detect missing field", cfg.isString("none", MANDATORY)); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void isNumber() { |
| 77 | assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY)); |
| 78 | assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY, 0)); |
| 79 | assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY, 0, 10)); |
| 80 | assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY, 5, 6)); |
| 81 | assertFalse("is not in range", cfg.isNumber(LONG, MANDATORY, 6, 10)); |
| 82 | assertFalse("is not in range", cfg.isNumber(LONG, MANDATORY, 4, 5)); |
| 83 | assertTrue("is not proper number", cfg.isNumber(LONG, OPTIONAL, 0, 10)); |
| 84 | assertTrue("is not proper number", cfg.isNumber(LONG, OPTIONAL)); |
| 85 | assertTrue("is not proper number", cfg.isNumber("none", OPTIONAL)); |
| 86 | assertFalse("did not detect missing field", cfg.isNumber("none", MANDATORY)); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void isDecimal() { |
| 91 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY)); |
| 92 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.0)); |
| 93 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.0, 1.0)); |
| 94 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.5, 0.6)); |
| 95 | assertFalse("is not in range", cfg.isDecimal(DOUBLE, MANDATORY, 0.6, 1.0)); |
| 96 | assertFalse("is not in range", cfg.isDecimal(DOUBLE, MANDATORY, 0.4, 0.5)); |
| 97 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, OPTIONAL, 0.0, 1.0)); |
| 98 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, OPTIONAL)); |
| 99 | assertTrue("is not proper decimal", cfg.isDecimal("none", OPTIONAL)); |
| 100 | assertFalse("did not detect missing field", cfg.isDecimal("none", MANDATORY)); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void isMacAddress() { |
| 105 | assertTrue("is not proper mac", cfg.isMacAddress(MAC, MANDATORY)); |
| 106 | assertTrue("is not proper mac", cfg.isMacAddress(MAC, OPTIONAL)); |
| 107 | assertTrue("is not proper mac", cfg.isMacAddress("none", OPTIONAL)); |
| 108 | assertFalse("did not detect missing field", cfg.isMacAddress("none", MANDATORY)); |
| 109 | } |
| 110 | |
| 111 | @Test(expected = IllegalArgumentException.class) |
| 112 | public void badMacAddress() { |
| 113 | assertTrue("is not proper mac", cfg.isMacAddress(TEXT, MANDATORY)); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | @Test |
| 118 | public void isIpAddress() { |
| 119 | assertTrue("is not proper ip", cfg.isIpAddress(IP, MANDATORY)); |
| 120 | assertTrue("is not proper ip", cfg.isIpAddress(IP, OPTIONAL)); |
| 121 | assertTrue("is not proper ip", cfg.isIpAddress("none", OPTIONAL)); |
| 122 | assertFalse("did not detect missing field", cfg.isMacAddress("none", MANDATORY)); |
| 123 | } |
| 124 | |
| 125 | @Test(expected = IllegalArgumentException.class) |
| 126 | public void badIpAddress() { |
| 127 | assertTrue("is not proper ip", cfg.isIpAddress(TEXT, MANDATORY)); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | // TODO: Add tests for other helper methods |
| 132 | |
| 133 | private class TestConfig extends Config<String> { |
| 134 | } |
| 135 | |
| 136 | private class TestDelegate implements ConfigApplyDelegate { |
| 137 | @Override |
| 138 | public void onApply(Config config) { |
| 139 | } |
| 140 | } |
| 141 | } |