Andrea Campanella | 423962b | 2016-02-26 13:09:22 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
Ray Milkey | 28b15ae | 2018-10-18 11:36:10 -0700 | [diff] [blame] | 69 | private String canonicalXml(String s) { |
| 70 | String[] lines = s.split("\n"); |
| 71 | StringBuilder xml = new StringBuilder(); |
| 72 | for (String line : lines) { |
| 73 | if (line.contains("<")) { |
| 74 | xml.append(line); |
| 75 | xml.append("\n"); |
| 76 | } |
| 77 | } |
| 78 | return xml.toString().trim(); |
| 79 | } |
| 80 | |
Andrea Campanella | 423962b | 2016-02-26 13:09:22 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Tests getting a single object configuration via passing the path and the map of the desired values. |
| 83 | * |
| 84 | * @throws ConfigurationException if the testing xml file is not there. |
| 85 | */ |
| 86 | @Test |
| 87 | public void testGetXmlConfigurationFromMap() throws ConfigurationException { |
| 88 | Map<String, String> pathAndValues = new HashMap<>(); |
| 89 | pathAndValues.put("capable-switch.id", "openvswitch"); |
| 90 | pathAndValues.put("switch.id", "ofc-bridge"); |
| 91 | pathAndValues.put("controller.id", "tcp:1.1.1.1:1"); |
| 92 | pathAndValues.put("controller.ip-address", "1.1.1.1"); |
| 93 | XMLConfiguration cfg = utils.getXmlConfiguration(OF_CONFIG_XML_PATH, pathAndValues); |
| 94 | testCreateConfig.load(getClass().getResourceAsStream("/testCreateSingleYangConfig.xml")); |
| 95 | assertNotEquals("Null testConfiguration", new XMLConfiguration(), testCreateConfig); |
| 96 | |
| 97 | assertEquals("Wrong configuaration", IteratorUtils.toList(testCreateConfig.getKeys()), |
| 98 | IteratorUtils.toList(cfg.getKeys())); |
| 99 | |
Ray Milkey | 28b15ae | 2018-10-18 11:36:10 -0700 | [diff] [blame] | 100 | assertEquals("Wrong string configuaration", canonicalXml(utils.getString(testCreateConfig)), |
| 101 | canonicalXml(utils.getString(cfg))); |
Andrea Campanella | 423962b | 2016-02-26 13:09:22 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Tests getting a multiple object nested configuration via passing the path |
| 106 | * and a list of YangElements containing with the element and desired value. |
| 107 | * |
| 108 | * @throws ConfigurationException |
| 109 | */ |
| 110 | @Test |
| 111 | public void getXmlConfigurationFromYangElements() throws ConfigurationException { |
| 112 | |
| 113 | assertNotEquals("Null testConfiguration", new XMLConfiguration(), testCreateConfig); |
| 114 | testCreateConfig.load(getClass().getResourceAsStream("/testYangConfig.xml")); |
| 115 | List<YangElement> elements = new ArrayList<>(); |
| 116 | elements.add(new YangElement("capable-switch", ImmutableMap.of("id", "openvswitch"))); |
| 117 | elements.add(new YangElement("switch", ImmutableMap.of("id", "ofc-bridge"))); |
| 118 | List<ControllerInfo> controllers = |
| 119 | ImmutableList.of(new ControllerInfo(IpAddress.valueOf("1.1.1.1"), 1, "tcp"), |
| 120 | new ControllerInfo(IpAddress.valueOf("2.2.2.2"), 2, "tcp")); |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 121 | controllers.forEach(cInfo -> { |
Andrea Campanella | 423962b | 2016-02-26 13:09:22 -0800 | [diff] [blame] | 122 | elements.add(new YangElement("controller", ImmutableMap.of("id", cInfo.target(), |
| 123 | "ip-address", cInfo.ip().toString()))); |
| 124 | }); |
| 125 | XMLConfiguration cfg = |
| 126 | new XMLConfiguration(YangXmlUtils.getInstance() |
| 127 | .getXmlConfiguration(OF_CONFIG_XML_PATH, elements)); |
| 128 | assertEquals("Wrong configuaration", IteratorUtils.toList(testCreateConfig.getKeys()), |
| 129 | IteratorUtils.toList(cfg.getKeys())); |
Ray Milkey | 28b15ae | 2018-10-18 11:36:10 -0700 | [diff] [blame] | 130 | assertEquals("Wrong string configuaration", canonicalXml(utils.getString(testCreateConfig)), |
| 131 | canonicalXml(utils.getString(cfg))); |
Andrea Campanella | 423962b | 2016-02-26 13:09:22 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Test reading an XML configuration and retrieving the requested elements. |
| 136 | * |
| 137 | * @throws ConfigurationException |
| 138 | */ |
| 139 | @Test |
| 140 | public void testReadLastXmlConfiguration() throws ConfigurationException { |
| 141 | testCreateConfig.load(getClass().getResourceAsStream("/testYangConfig.xml")); |
| 142 | List<YangElement> elements = utils.readXmlConfiguration(testCreateConfig, |
| 143 | "controller"); |
| 144 | List<YangElement> expected = ImmutableList.of( |
| 145 | new YangElement("controller", ImmutableMap.of("id", "tcp:1.1.1.1:1", |
| 146 | "ip-address", "1.1.1.1")), |
| 147 | new YangElement("controller", ImmutableMap.of("id", "tcp:2.2.2.2:2", |
| 148 | "ip-address", "2.2.2.2"))); |
| 149 | assertEquals("Wrong elements collected", expected, elements); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Test reading an XML configuration and retrieving the requested elements. |
| 154 | * |
| 155 | * @throws ConfigurationException |
| 156 | */ |
| 157 | @Test |
| 158 | public void testReadNestedXmlConfiguration() throws ConfigurationException { |
| 159 | testCreateConfig.load(getClass().getResourceAsStream("/testYangConfig.xml")); |
| 160 | List<YangElement> elements = utils.readXmlConfiguration(testCreateConfig, "controllers"); |
| 161 | List<YangElement> expected = ImmutableList.of( |
| 162 | new YangElement("controllers", ImmutableMap.of("controller.id", "tcp:1.1.1.1:1", |
| 163 | "controller.ip-address", "1.1.1.1")), |
| 164 | new YangElement("controllers", ImmutableMap.of("controller.id", "tcp:2.2.2.2:2", |
| 165 | "controller.ip-address", "2.2.2.2"))); |
| 166 | assertEquals("Wrong elements collected", expected, elements); |
| 167 | } |
| 168 | |
| 169 | //enables to change the path to the resources directory. |
| 170 | private class YangXmlUtilsAdap extends YangXmlUtils { |
| 171 | |
| 172 | @Override |
| 173 | protected InputStream getCfgInputStream(String file) { |
| 174 | return YangXmlUtilsAdap.class.getResourceAsStream(file); |
| 175 | } |
| 176 | } |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 177 | } |