Andrea Campanella | 423962b | 2016-02-26 13:09:22 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andrea Campanella | 423962b | 2016-02-26 13:09:22 -0800 | [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.drivers.utilities; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import org.apache.commons.collections.IteratorUtils; |
| 22 | import org.apache.commons.configuration.ConfigurationException; |
| 23 | import org.apache.commons.configuration.XMLConfiguration; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onosproject.net.behaviour.ControllerInfo; |
| 28 | |
| 29 | import java.io.InputStream; |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.HashMap; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | |
| 35 | import static org.junit.Assert.*; |
| 36 | |
| 37 | /** |
| 38 | * Tests for the XMLYangUtils. |
| 39 | */ |
| 40 | public class YangXmlUtilsTest { |
| 41 | public static final String OF_CONFIG_XML_PATH = "/of-config/of-config.xml"; |
| 42 | private YangXmlUtilsAdap utils; |
| 43 | private XMLConfiguration testCreateConfig; |
| 44 | |
| 45 | @Before |
| 46 | public void setUp() throws Exception { |
| 47 | assertTrue("No resource for test", YangXmlUtilsTest.class. |
| 48 | getResourceAsStream("/of-config/of-config.xml") != null); |
| 49 | utils = new YangXmlUtilsAdap(); |
| 50 | |
| 51 | testCreateConfig = new XMLConfiguration(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests getting a single object configuration via passing the path and the map of the desired values. |
| 56 | * |
| 57 | * @throws ConfigurationException if the testing xml file is not there. |
| 58 | */ |
| 59 | @Test |
| 60 | public void testGetXmlUtilsInstance() throws ConfigurationException { |
| 61 | |
| 62 | YangXmlUtils instance1 = YangXmlUtils.getInstance(); |
| 63 | YangXmlUtils instance2 = YangXmlUtils.getInstance(); |
| 64 | |
| 65 | assertEquals("Duplicate instance", instance1, instance2); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests getting a single object configuration via passing the path and the map of the desired values. |
| 71 | * |
| 72 | * @throws ConfigurationException if the testing xml file is not there. |
| 73 | */ |
| 74 | @Test |
| 75 | public void testGetXmlConfigurationFromMap() throws ConfigurationException { |
| 76 | Map<String, String> pathAndValues = new HashMap<>(); |
| 77 | pathAndValues.put("capable-switch.id", "openvswitch"); |
| 78 | pathAndValues.put("switch.id", "ofc-bridge"); |
| 79 | pathAndValues.put("controller.id", "tcp:1.1.1.1:1"); |
| 80 | pathAndValues.put("controller.ip-address", "1.1.1.1"); |
| 81 | XMLConfiguration cfg = utils.getXmlConfiguration(OF_CONFIG_XML_PATH, pathAndValues); |
| 82 | testCreateConfig.load(getClass().getResourceAsStream("/testCreateSingleYangConfig.xml")); |
| 83 | assertNotEquals("Null testConfiguration", new XMLConfiguration(), testCreateConfig); |
| 84 | |
| 85 | assertEquals("Wrong configuaration", IteratorUtils.toList(testCreateConfig.getKeys()), |
| 86 | IteratorUtils.toList(cfg.getKeys())); |
| 87 | |
| 88 | assertEquals("Wrong string configuaration", utils.getString(testCreateConfig), |
| 89 | utils.getString(cfg)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests getting a multiple object nested configuration via passing the path |
| 94 | * and a list of YangElements containing with the element and desired value. |
| 95 | * |
| 96 | * @throws ConfigurationException |
| 97 | */ |
| 98 | @Test |
| 99 | public void getXmlConfigurationFromYangElements() throws ConfigurationException { |
| 100 | |
| 101 | assertNotEquals("Null testConfiguration", new XMLConfiguration(), testCreateConfig); |
| 102 | testCreateConfig.load(getClass().getResourceAsStream("/testYangConfig.xml")); |
| 103 | List<YangElement> elements = new ArrayList<>(); |
| 104 | elements.add(new YangElement("capable-switch", ImmutableMap.of("id", "openvswitch"))); |
| 105 | elements.add(new YangElement("switch", ImmutableMap.of("id", "ofc-bridge"))); |
| 106 | List<ControllerInfo> controllers = |
| 107 | ImmutableList.of(new ControllerInfo(IpAddress.valueOf("1.1.1.1"), 1, "tcp"), |
| 108 | new ControllerInfo(IpAddress.valueOf("2.2.2.2"), 2, "tcp")); |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame^] | 109 | controllers.forEach(cInfo -> { |
Andrea Campanella | 423962b | 2016-02-26 13:09:22 -0800 | [diff] [blame] | 110 | elements.add(new YangElement("controller", ImmutableMap.of("id", cInfo.target(), |
| 111 | "ip-address", cInfo.ip().toString()))); |
| 112 | }); |
| 113 | XMLConfiguration cfg = |
| 114 | new XMLConfiguration(YangXmlUtils.getInstance() |
| 115 | .getXmlConfiguration(OF_CONFIG_XML_PATH, elements)); |
| 116 | assertEquals("Wrong configuaration", IteratorUtils.toList(testCreateConfig.getKeys()), |
| 117 | IteratorUtils.toList(cfg.getKeys())); |
| 118 | assertEquals("Wrong string configuaration", utils.getString(testCreateConfig), |
| 119 | utils.getString(cfg)); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Test reading an XML configuration and retrieving the requested elements. |
| 124 | * |
| 125 | * @throws ConfigurationException |
| 126 | */ |
| 127 | @Test |
| 128 | public void testReadLastXmlConfiguration() throws ConfigurationException { |
| 129 | testCreateConfig.load(getClass().getResourceAsStream("/testYangConfig.xml")); |
| 130 | List<YangElement> elements = utils.readXmlConfiguration(testCreateConfig, |
| 131 | "controller"); |
| 132 | List<YangElement> expected = ImmutableList.of( |
| 133 | new YangElement("controller", ImmutableMap.of("id", "tcp:1.1.1.1:1", |
| 134 | "ip-address", "1.1.1.1")), |
| 135 | new YangElement("controller", ImmutableMap.of("id", "tcp:2.2.2.2:2", |
| 136 | "ip-address", "2.2.2.2"))); |
| 137 | assertEquals("Wrong elements collected", expected, elements); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Test reading an XML configuration and retrieving the requested elements. |
| 142 | * |
| 143 | * @throws ConfigurationException |
| 144 | */ |
| 145 | @Test |
| 146 | public void testReadNestedXmlConfiguration() throws ConfigurationException { |
| 147 | testCreateConfig.load(getClass().getResourceAsStream("/testYangConfig.xml")); |
| 148 | List<YangElement> elements = utils.readXmlConfiguration(testCreateConfig, "controllers"); |
| 149 | List<YangElement> expected = ImmutableList.of( |
| 150 | new YangElement("controllers", ImmutableMap.of("controller.id", "tcp:1.1.1.1:1", |
| 151 | "controller.ip-address", "1.1.1.1")), |
| 152 | new YangElement("controllers", ImmutableMap.of("controller.id", "tcp:2.2.2.2:2", |
| 153 | "controller.ip-address", "2.2.2.2"))); |
| 154 | assertEquals("Wrong elements collected", expected, elements); |
| 155 | } |
| 156 | |
| 157 | //enables to change the path to the resources directory. |
| 158 | private class YangXmlUtilsAdap extends YangXmlUtils { |
| 159 | |
| 160 | @Override |
| 161 | protected InputStream getCfgInputStream(String file) { |
| 162 | return YangXmlUtilsAdap.class.getResourceAsStream(file); |
| 163 | } |
| 164 | } |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame^] | 165 | } |