Open Kilda Java Documentation
SwitchConfigurationTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.atdd;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 
28 
29 import cucumber.api.java.en.And;
30 import cucumber.api.java.en.Then;
31 import cucumber.api.java.en.When;
32 
33 import java.util.HashMap;
34 import java.util.List;
35 
37 
38  private final ControllerUtils controllerUtils;
39 
40  public SwitchConfigurationTest() throws Exception {
41  controllerUtils = new ControllerUtils();
42  }
43 
44  @When("^change port status to '(.*)' for switch \"([^\"]*)\" port \"([^\"]*)\"$")
45  public void changePortStatus(String portStatus, String switchName, int portNumber) {
46  PortDto portDto = SwitchesUtils.changeSwitchPortStatus(switchName, portNumber,
47  PortStatus.valueOf(portStatus.toUpperCase()));
48  assertNotNull(portDto);
49  }
50 
51  @Then("port status for switch \"(.*)\" port \"(\\d+)\" is '(.*)'")
52  public void checkPortStatus(String switchName, int portNumber, String portStatus) throws Exception {
53  assertEquals(getPortStatuses(switchName).get(portNumber), PortStatus.valueOf(portStatus.toUpperCase()));
54  }
55 
56  private HashMap<Integer, PortStatus> getPortStatuses(String switchName) throws FloodlightQueryException {
57  List<PortEntry> portEntries = controllerUtils.getSwitchPorts(switchName).getPortEntries();
58  HashMap<Integer, PortStatus> results = new HashMap<>();
59  portEntries.forEach(portEntry -> {
60  if (!portEntry.getPortNumber().equals("local")) {
61  Integer portNumber = Integer.parseInt(portEntry.getPortNumber());
62  PortStatus status = portEntry.getConfig().contains("PORT_DOWN") ? PortStatus.DOWN : PortStatus.UP;
63  results.put(portNumber, status);
64  }
65  });
66  return results;
67  }
68 
69  @And("^all port statuses for switch \"([^\"]*)\" are '(.*)'$")
70  public void allPortStatusesForSwitchAreUp(String switchName, String portStatus) throws FloodlightQueryException {
71  assertTrue(getPortStatuses(switchName).values().stream()
72  .allMatch(status -> status.equals(PortStatus.valueOf(portStatus.toUpperCase()))));
73  }
74 
75  @And("^all port statuses for switch \"([^\"]*)\" except for port \"([^\"]*)\" are '(.*)'$")
76  public void allPortStatusesForSwitchExceptForPortAreUp(String switchName, int portNumber, String portStatus)
77  throws Throwable {
78  assertTrue(getPortStatuses(switchName).entrySet().stream().filter(entry -> entry.getKey() != portNumber)
79  .allMatch(entry -> entry.getValue().equals(PortStatus.valueOf(portStatus.toUpperCase()))));
80  }
81 }
void allPortStatusesForSwitchAreUp(String switchName, String portStatus)
void changePortStatus(String portStatus, String switchName, int portNumber)
def status()
Definition: rest.py:593
Definition: PortEntry.java:28
static PortDto changeSwitchPortStatus(String switchName, int port, PortStatus status)
List< PortEntry > getPortEntries()
void allPortStatusesForSwitchExceptForPortAreUp(String switchName, int portNumber, String portStatus)
void checkPortStatus(String switchName, int portNumber, String portStatus)