@Test public void testHandlePowers() { Action1 onSuccessMock = mock(Action1.class); Action0 onErrorMock = mock(Action0.class); List<OpenSession> openSessions = Lists.newArrayList( OpenSession.newSession(requestInstantaneousPower("1", Version.MODEL_F523)), OpenSession.newSession(requestDailyPower("1", Version.MODEL_F523_A)), OpenSession.newSession(requestMonthlyPower("255", Version.MODEL_F520)) ); openSessions.get(0).addAllResponse(Lists.newArrayList(() -> "*#18*51*113*888##", () -> OpenMessage.ACK)); openSessions.get(1).addAllResponse(Lists.newArrayList(() -> "*#18*71#0*54*1234##", () -> OpenMessage.ACK)); openSessions.get(2).addAllResponse(Lists.newArrayList(() -> "*#18*5255*53*999999##", () -> OpenMessage.ACK)); Observable.just(openSessions) .map(EnergyManagement.handlePowers(onSuccessMock, onErrorMock)) .subscribe(); verify(onSuccessMock).call(Lists.newArrayList("888", "1234", "999999")); verify(onErrorMock, never()).call(); }
public static Func1<List<OpenSession>, List<OpenSession>> handlePowers(Action1<List<String>> onSuccess, Action0 onError) { return openSessions -> { List<String> results = Lists.newArrayList(); for (OpenSession openSession: openSessions) { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_DIMENSION_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() >= 1 || response.size() <=3, "invalid response"); final String responseValue = response.get(0).getValue(); checkNotNull(responseValue, "response value is null"); if (response.size() != 3 && isValidPower(responseValue)) { results.add(getPower(openSession.getRequest(), responseValue)); } else { results.add(""); } } if (results.size() > 0 && results.size() == openSessions.size()) { onSuccess.call(results); } else { onError.call(); } return openSessions; }; }
EnergyManagement extends BaseOpenMessage { public static Func1<List<OpenSession>, List<OpenSession>> handlePowers(Action1<List<String>> onSuccess, Action0 onError) { return openSessions -> { List<String> results = Lists.newArrayList(); for (OpenSession openSession: openSessions) { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_DIMENSION_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() >= 1 || response.size() <=3, "invalid response"); final String responseValue = response.get(0).getValue(); checkNotNull(responseValue, "response value is null"); if (response.size() != 3 && isValidPower(responseValue)) { results.add(getPower(openSession.getRequest(), responseValue)); } else { results.add(""); } } if (results.size() > 0 && results.size() == openSessions.size()) { onSuccess.call(results); } else { onError.call(); } return openSessions; }; } }
EnergyManagement extends BaseOpenMessage { public static Func1<List<OpenSession>, List<OpenSession>> handlePowers(Action1<List<String>> onSuccess, Action0 onError) { return openSessions -> { List<String> results = Lists.newArrayList(); for (OpenSession openSession: openSessions) { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_DIMENSION_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() >= 1 || response.size() <=3, "invalid response"); final String responseValue = response.get(0).getValue(); checkNotNull(responseValue, "response value is null"); if (response.size() != 3 && isValidPower(responseValue)) { results.add(getPower(openSession.getRequest(), responseValue)); } else { results.add(""); } } if (results.size() > 0 && results.size() == openSessions.size()) { onSuccess.call(results); } else { onError.call(); } return openSessions; }; } private  EnergyManagement(String value); }
EnergyManagement extends BaseOpenMessage { public static Func1<List<OpenSession>, List<OpenSession>> handlePowers(Action1<List<String>> onSuccess, Action0 onError) { return openSessions -> { List<String> results = Lists.newArrayList(); for (OpenSession openSession: openSessions) { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_DIMENSION_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() >= 1 || response.size() <=3, "invalid response"); final String responseValue = response.get(0).getValue(); checkNotNull(responseValue, "response value is null"); if (response.size() != 3 && isValidPower(responseValue)) { results.add(getPower(openSession.getRequest(), responseValue)); } else { results.add(""); } } if (results.size() > 0 && results.size() == openSessions.size()) { onSuccess.call(results); } else { onError.call(); } return openSessions; }; } private  EnergyManagement(String value); static EnergyManagement requestInstantaneousPower(String where, Version version); static EnergyManagement requestDailyPower(String where, Version version); static EnergyManagement requestMonthlyPower(String where, Version version); static Func1<List<OpenSession>, List<OpenSession>> handlePowers(Action1<List<String>> onSuccess, Action0 onError); }
EnergyManagement extends BaseOpenMessage { public static Func1<List<OpenSession>, List<OpenSession>> handlePowers(Action1<List<String>> onSuccess, Action0 onError) { return openSessions -> { List<String> results = Lists.newArrayList(); for (OpenSession openSession: openSessions) { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_DIMENSION_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() >= 1 || response.size() <=3, "invalid response"); final String responseValue = response.get(0).getValue(); checkNotNull(responseValue, "response value is null"); if (response.size() != 3 && isValidPower(responseValue)) { results.add(getPower(openSession.getRequest(), responseValue)); } else { results.add(""); } } if (results.size() > 0 && results.size() == openSessions.size()) { onSuccess.call(results); } else { onError.call(); } return openSessions; }; } private  EnergyManagement(String value); static EnergyManagement requestInstantaneousPower(String where, Version version); static EnergyManagement requestDailyPower(String where, Version version); static EnergyManagement requestMonthlyPower(String where, Version version); static Func1<List<OpenSession>, List<OpenSession>> handlePowers(Action1<List<String>> onSuccess, Action0 onError);  }