Skip to content

Commit 7478829

Browse files
committed
Refactore test drivers
1 parent 24b3514 commit 7478829

7 files changed

Lines changed: 188 additions & 141 deletions

File tree

src/main/java/com/github/kaklakariada/fritzbox/AbstractTestHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/**
2+
* A Java API for managing FritzBox HomeAutomation
3+
* Copyright (C) 2017 Christoph Pirkl <christoph at users.sourceforge.net>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
118
package com.github.kaklakariada.fritzbox;
219

320
public abstract class AbstractTestHelper {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* A Java API for managing FritzBox HomeAutomation
3+
* Copyright (C) 2017 Christoph Pirkl <christoph at users.sourceforge.net>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.github.kaklakariada.fritzbox;
19+
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.UncheckedIOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
26+
import java.util.Optional;
27+
import java.util.Properties;
28+
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
32+
public class Config {
33+
private static final Logger LOG = LoggerFactory.getLogger(Config.class);
34+
private static final Path DEFAULT_CONFIG = Paths.get("application.properties");
35+
private final Properties properties;
36+
37+
private Config(final Properties properties) {
38+
this.properties = properties;
39+
}
40+
41+
public static Config read() {
42+
return read(DEFAULT_CONFIG);
43+
}
44+
45+
private static Config read(final Path configFile) {
46+
final Path file = configFile.normalize();
47+
return new Config(loadProperties(file));
48+
}
49+
50+
private static Properties loadProperties(final Path configFile) {
51+
if (!Files.exists(configFile)) {
52+
throw new IllegalStateException("Config file not found at '" + configFile + "'");
53+
}
54+
LOG.info("Reading config file from {}", configFile);
55+
try (InputStream stream = Files.newInputStream(configFile)) {
56+
final Properties props = new Properties();
57+
props.load(stream);
58+
return props;
59+
} catch (final IOException e) {
60+
throw new UncheckedIOException("Error reading config file " + configFile, e);
61+
}
62+
}
63+
64+
public String getUrl() {
65+
return getMandatoryValue("fritzbox.url");
66+
}
67+
68+
public String getUsername() {
69+
return getMandatoryValue("fritzbox.username");
70+
}
71+
72+
public String getPassword() {
73+
return getMandatoryValue("fritzbox.password");
74+
}
75+
76+
private String getMandatoryValue(final String param) {
77+
return getOptionalValue(param).orElseThrow(
78+
() -> new IllegalStateException("Property '" + param + "' not found in config file"));
79+
}
80+
81+
private Optional<String> getOptionalValue(final String param) {
82+
return Optional.ofNullable(this.properties.getProperty(param));
83+
}
84+
}

src/main/java/com/github/kaklakariada/fritzbox/HomeAutomation.java

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,26 @@ public class HomeAutomation {
3636

3737
private final FritzBoxSession session;
3838

39-
private enum ParamName {
39+
private enum Param {
4040
PARAM("param"), LEVEL("level"), TARGET("target");
4141

42-
private String paramName;
42+
private final String name;
4343

44-
ParamName(String paramName) {
45-
this.paramName = paramName;
44+
Param(final String name) {
45+
this.name = name;
4646
}
4747
}
4848

49-
private HomeAutomation(FritzBoxSession fritzbox) {
49+
private HomeAutomation(final FritzBoxSession fritzbox) {
5050
this.session = fritzbox;
5151
}
5252

53-
public static HomeAutomation connect(String baseUrl, String username, String password) {
53+
public static HomeAutomation connect(final Config config) {
54+
return connect(config.getUrl(), config.getUsername(), config.getPassword());
55+
}
56+
57+
public static HomeAutomation connect(final String baseUrl, final String username, final String password) {
58+
LOG.info("Logging in to '{}' with username '{}'", baseUrl, username);
5459
final FritzBoxSession session = new FritzBoxSession(baseUrl);
5560
session.login(username, password);
5661
return new HomeAutomation(session);
@@ -62,33 +67,37 @@ public DeviceList getDeviceListInfos() {
6267
deviceList.getApiVersion());
6368
return deviceList;
6469
}
65-
66-
public Device getDeviceInfos(String deviceAin) {
67-
return executeDeviceCommand(deviceAin, "getdeviceinfos", null, Device.class);
70+
71+
public Device getDeviceInfos(final String deviceAin) {
72+
return executeDeviceCommand(deviceAin, "getdeviceinfos", Param.PARAM, null, Device.class);
6873
}
6974

70-
private <T> T executeCommand(String command, Class<T> resultType) {
75+
private <T> T executeCommand(final String command, final Class<T> resultType) {
7176
final QueryParameters parameters = QueryParameters.builder().add("switchcmd", command).build();
7277
return session.getAutenticated(HOME_AUTOMATION_PATH, parameters, resultType);
7378
}
7479

75-
private <T> T executeParamCommand(String deviceAin, String command, String parameter, Class<T> responseType) {
76-
return executeDeviceCommand(deviceAin, command, ParamName.PARAM, parameter, responseType);
80+
private <T> T executeParamCommand(final String deviceAin, final String command, final String parameter,
81+
final Class<T> responseType) {
82+
return executeDeviceCommand(deviceAin, command, Param.PARAM, parameter, responseType);
7783
}
7884

79-
private <T> T executeLevelCommand(String deviceAin, String command, String parameter, Class<T> responseType) {
80-
return executeDeviceCommand(deviceAin, command, ParamName.LEVEL, parameter, responseType);
85+
private <T> T executeLevelCommand(final String deviceAin, final String command, final String parameter,
86+
final Class<T> responseType) {
87+
return executeDeviceCommand(deviceAin, command, Param.LEVEL, parameter, responseType);
8188
}
8289

83-
private <T> T executeTargetCommand(String deviceAin, String command, String parameter, Class<T> responseType) {
84-
return executeDeviceCommand(deviceAin, command, ParamName.TARGET, parameter, responseType);
90+
private <T> T executeTargetCommand(final String deviceAin, final String command, final String parameter,
91+
final Class<T> responseType) {
92+
return executeDeviceCommand(deviceAin, command, Param.TARGET, parameter, responseType);
8593
}
8694

87-
private <T> T executeDeviceCommand(String deviceAin, String command, ParamName paramName, String parameter,
88-
Class<T> responseType) {
95+
private <T> T executeDeviceCommand(final String deviceAin, final String command, final Param paramName,
96+
final String parameter,
97+
final Class<T> responseType) {
8998
final Builder paramBuilder = QueryParameters.builder().add("ain", deviceAin).add("switchcmd", command);
9099
if (parameter != null) {
91-
paramBuilder.add(paramName.paramName, parameter);
100+
paramBuilder.add(paramName.name, parameter);
92101
}
93102
return session.getAutenticated(HOME_AUTOMATION_PATH, paramBuilder.build(), responseType);
94103
}
@@ -100,58 +109,58 @@ public List<String> getSwitchList() {
100109
return idList;
101110
}
102111

103-
public void switchPowerState(String deviceAin, boolean on) {
112+
public void switchPowerState(final String deviceAin, final boolean on) {
104113
final String command = on ? "setswitchon" : "setswitchoff";
105114
executeParamCommand(deviceAin, command, null, Boolean.class);
106115
}
107116

108-
public void togglePowerState(String deviceAin) {
117+
public void togglePowerState(final String deviceAin) {
109118
executeParamCommand(deviceAin, "setswitchtoggle", null, Boolean.class);
110119
}
111120

112-
public void setHkrTsoll(String deviceAin, String tsoll) {
121+
public void setHkrTsoll(final String deviceAin, final String tsoll) {
113122
executeParamCommand(deviceAin, "sethkrtsoll", tsoll, Boolean.class);
114123
}
115124

116-
public void setBlind(String deviceAin, String target) {
125+
public void setBlind(final String deviceAin, final String target) {
117126
executeTargetCommand(deviceAin, "setblind", target, Boolean.class);
118127
}
119128

120-
public void setLevel(String deviceAin, String level) {
129+
public void setLevel(final String deviceAin, final String level) {
121130
executeLevelCommand(deviceAin, "setlevel", level, Boolean.class);
122131
}
123132

124-
public void setLevelPercentage(String deviceAin, String level) {
133+
public void setLevelPercentage(final String deviceAin, final String level) {
125134
executeLevelCommand(deviceAin, "setlevelpercentage", level, Boolean.class);
126135
}
127136

128-
public boolean getSwitchState(String deviceAin) {
137+
public boolean getSwitchState(final String deviceAin) {
129138
return executeParamCommand(deviceAin, "getswitchstate", null, Boolean.class);
130139
}
131140

132-
public boolean getSwitchPresent(String deviceAin) {
141+
public boolean getSwitchPresent(final String deviceAin) {
133142
return executeParamCommand(deviceAin, "getswitchpresent", null, Boolean.class);
134143
}
135144

136-
public String getSwitchName(String deviceAin) {
145+
public String getSwitchName(final String deviceAin) {
137146
return executeParamCommand(deviceAin, "getswitchname", null, String.class);
138147
}
139148

140-
public Float getTemperature(String deviceAin) {
149+
public Float getTemperature(final String deviceAin) {
141150
final Integer centiDegree = executeParamCommand(deviceAin, "gettemperature", null, Integer.class);
142151
return centiDegree == null ? null : centiDegree / 10F;
143152
}
144153

145-
public DeviceStats getBasicStatistics(String deviceAin) {
154+
public DeviceStats getBasicStatistics(final String deviceAin) {
146155
return executeParamCommand(deviceAin, "getbasicdevicestats", null, DeviceStats.class);
147156
}
148157

149-
public Float getSwitchPowerWatt(String deviceAin) {
158+
public Float getSwitchPowerWatt(final String deviceAin) {
150159
final Integer powerMilliWatt = executeParamCommand(deviceAin, "getswitchpower", null, Integer.class);
151160
return powerMilliWatt == null ? null : powerMilliWatt / 1000F;
152161
}
153162

154-
public Integer getSwitchEnergyWattHour(String deviceAin) {
163+
public Integer getSwitchEnergyWattHour(final String deviceAin) {
155164
return executeParamCommand(deviceAin, "getswitchenergy", null, Integer.class);
156165
}
157166

src/main/java/com/github/kaklakariada/fritzbox/TestBlind.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@
3535
public class TestBlind extends AbstractTestHelper {
3636

3737
private static final Logger LOG = LoggerFactory.getLogger(TestBlind.class);
38+
private static final int WAIT_SECONDS = 20;
3839

3940
private final HomeAutomation homeAutomation;
4041

41-
final int waitSeconds = 20;
42-
4342
public TestBlind() throws InterruptedException {
44-
this.homeAutomation = TestLogin.login();
43+
this.homeAutomation = HomeAutomation.connect(Config.read());
4544

4645
// make sure to set back blind to original state
4746
final List<Device> blindDevices = getBlindDevices();
47+
if (blindDevices.isEmpty()) {
48+
LOG.warn("No blind devices found");
49+
return;
50+
}
4851
final int wasPercenClosed = blindDevices.get(0).getLevelControl().getLevelpercentage();
4952

5053
toggleBlindOpenClose();
@@ -66,8 +69,7 @@ private void toggleBlindOpenClose() throws InterruptedException {
6669
toggleBlindOpenClose(blindDevices.get(0));
6770

6871
// Start move back
69-
LOG.info("Wait {} seconds", waitSeconds);
70-
Thread.sleep(waitSeconds * 1000);
72+
sleep();
7173
LOG.info("");
7274
LOG.info("Status after change");
7375
blindDevices = getBlindDevices();
@@ -76,12 +78,16 @@ private void toggleBlindOpenClose() throws InterruptedException {
7678

7779
// Show status at end of test
7880
LOG.info("");
79-
LOG.info("Wait {} seconds", waitSeconds);
80-
Thread.sleep(waitSeconds * 1000);
81+
sleep();
8182
blindDevices = getBlindDevices();
8283
showStatus(blindDevices);
8384
}
8485

86+
private void sleep() throws InterruptedException {
87+
LOG.info("Wait {} seconds", WAIT_SECONDS);
88+
Thread.sleep(WAIT_SECONDS * 1000L);
89+
}
90+
8591
private void toggleBlindOpenClose(final Device blind) {
8692
final String ain = getAin(blind.getIdentifier());
8793
final boolean wasOpen = blind.getLevelControl().getLevel() == 0;
@@ -107,9 +113,7 @@ private void togglePercentOpen() throws InterruptedException {
107113

108114
setPercentOpen(blindDevices.get(0), newPercenClosed);
109115

110-
// Start move back
111-
LOG.info("Wait {} seconds", waitSeconds);
112-
Thread.sleep(waitSeconds * 1000);
116+
sleep();
113117
LOG.info("");
114118
LOG.info("Status after change");
115119
blindDevices = getBlindDevices();
@@ -118,13 +122,12 @@ private void togglePercentOpen() throws InterruptedException {
118122

119123
// Show status at end of test
120124
LOG.info("");
121-
LOG.info("Wait {} seconds", waitSeconds);
122-
Thread.sleep(waitSeconds * 1000);
125+
sleep();
123126
blindDevices = getBlindDevices();
124127
showStatus(blindDevices);
125128
}
126129

127-
private void setPercentOpen(final Device blind, int percent) {
130+
private void setPercentOpen(final Device blind, final int percent) {
128131
final String ain = getAin(blind.getIdentifier());
129132
final String newLevel = String.valueOf(percent);
130133

@@ -136,11 +139,10 @@ private void setPercentOpen(final Device blind, int percent) {
136139

137140
private List<Device> getBlindDevices() {
138141
final DeviceList devices = homeAutomation.getDeviceListInfos();
139-
final List<Device> hkrDevices = devices.getDevices()
142+
return devices.getDevices()
140143
.stream()
141144
.filter(device -> device.getBlind() != null)
142145
.collect(Collectors.toList());
143-
return hkrDevices;
144146
}
145147

146148
private void showStatus(final List<Device> blindDevices) {
@@ -153,8 +155,7 @@ private void showStatus(final List<Device> blindDevices) {
153155
});
154156
}
155157

156-
public static void main(String[] args) throws InterruptedException {
158+
public static void main(final String[] args) throws InterruptedException {
157159
new TestBlind();
158160
}
159-
160161
}

0 commit comments

Comments
 (0)