Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | package org.onosproject.openstacktelemetry.impl; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | import org.onosproject.openstacktelemetry.api.TelemetryConfigProvider; |
| 20 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.util.Iterator; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.junit.Assert.assertTrue; |
| 28 | |
| 29 | /** |
| 30 | * Tests of the XML configuration loader implementation. |
| 31 | */ |
| 32 | public class XmlTelemetryConfigLoaderTest { |
| 33 | |
| 34 | @Test |
| 35 | public void basics() throws IOException { |
| 36 | XmlTelemetryConfigLoader loader = new XmlTelemetryConfigLoader(); |
| 37 | InputStream stream = getClass().getResourceAsStream("configs.singleInheritance.xml"); |
| 38 | TelemetryConfigProvider provider = loader.loadTelemetryConfigs(stream); |
| 39 | |
| 40 | assertEquals("incorrect config count", 2, provider.getTelemetryConfigs().size()); |
| 41 | |
| 42 | TelemetryConfig config = getConfig(provider, "foo.1"); |
| 43 | |
| 44 | assertEquals("incorrect config name", "foo.1", config.name()); |
| 45 | assertEquals("incorrect config type", "grpc", config.type().name().toLowerCase()); |
| 46 | assertEquals("incorrect config mfg", "Circus", config.manufacturer()); |
| 47 | assertEquals("incorrect config sw", "2.2", config.swVersion()); |
| 48 | |
| 49 | assertEquals("incorrect config properties", 2, config.properties().size()); |
| 50 | assertTrue("incorrect config property", config.properties().containsKey("p1")); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void multipleDrivers() throws IOException { |
| 55 | XmlTelemetryConfigLoader loader = new XmlTelemetryConfigLoader(); |
| 56 | InputStream stream = getClass().getResourceAsStream("configs.multipleInheritance.xml"); |
| 57 | TelemetryConfigProvider provider = loader.loadTelemetryConfigs(stream); |
| 58 | |
| 59 | TelemetryConfig config1 = getConfig(provider, "foo.1"); |
| 60 | |
| 61 | assertEquals("incorrect config mfg", "Circus", config1.manufacturer()); |
| 62 | assertEquals("incorrect config sw", "2.2", config1.swVersion()); |
| 63 | |
| 64 | assertEquals("incorrect config type", "grpc", config1.type().name().toLowerCase()); |
| 65 | assertEquals("incorrect config properties", 3, config1.properties().size()); |
| 66 | assertTrue("incorrect config property", config1.properties().containsKey("p0")); |
| 67 | assertTrue("incorrect config property", config1.properties().containsKey("p1")); |
| 68 | assertTrue("incorrect config property", config1.properties().containsKey("p2")); |
| 69 | |
| 70 | TelemetryConfig config2 = getConfig(provider, "foo.2"); |
| 71 | assertEquals("incorrect config type", "grpc", config2.type().name().toLowerCase()); |
| 72 | assertEquals("incorrect config mfg", "Big Top OEM", config2.manufacturer()); |
| 73 | assertEquals("incorrect config sw", "2.2", config2.swVersion()); |
| 74 | |
| 75 | assertEquals("incorrect config properties", 4, config2.properties().size()); |
| 76 | assertTrue("incorrect config property", config2.properties().containsKey("p0")); |
| 77 | assertTrue("incorrect config property", config2.properties().containsKey("p1")); |
| 78 | assertTrue("incorrect config property", config2.properties().containsKey("p2")); |
| 79 | assertTrue("incorrect config property", config2.properties().containsKey("p3")); |
| 80 | } |
| 81 | |
| 82 | private TelemetryConfig getConfig(TelemetryConfigProvider provider, String name) { |
| 83 | Iterator<TelemetryConfig> iterator = provider.getTelemetryConfigs().iterator(); |
| 84 | TelemetryConfig config; |
| 85 | do { |
| 86 | config = iterator.next(); |
| 87 | } while (!name.equals(config.name())); |
| 88 | return config; |
| 89 | } |
| 90 | } |