@Test public void testHandleStatusOn() { Action0 onStatusMock = mock(Action0.class); Action0 offStatusMock = mock(Action0.class); OpenSession openSession = OpenSession.newSession(() -> "*#1*REQUEST"); openSession.addAllResponse(Lists.newArrayList(() -> "*1*1*21##", () -> "*#*1##")); Observable.just(openSession) .map(Lighting.handleStatus(onStatusMock, offStatusMock)) .subscribe(); verify(onStatusMock).call(); verify(offStatusMock, never()).call(); }
public static Func1<OpenSession, OpenSession> handleStatus(Action0 onStatus, Action0 offStatus) { return openSession -> { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_STATUS_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() == 1 || response.size() == 2, "invalid response"); checkNotNull(response.get(0).getValue(), "response value is null"); if (isOn(response.get(0).getValue())) { onStatus.call(); return openSession; } if (isOff(response.get(0).getValue())) { offStatus.call(); return openSession; } throw new IllegalStateException("unhandled lighting status"); }; }
Lighting extends BaseOpenMessage { public static Func1<OpenSession, OpenSession> handleStatus(Action0 onStatus, Action0 offStatus) { return openSession -> { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_STATUS_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() == 1 || response.size() == 2, "invalid response"); checkNotNull(response.get(0).getValue(), "response value is null"); if (isOn(response.get(0).getValue())) { onStatus.call(); return openSession; } if (isOff(response.get(0).getValue())) { offStatus.call(); return openSession; } throw new IllegalStateException("unhandled lighting status"); }; } }
Lighting extends BaseOpenMessage { public static Func1<OpenSession, OpenSession> handleStatus(Action0 onStatus, Action0 offStatus) { return openSession -> { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_STATUS_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() == 1 || response.size() == 2, "invalid response"); checkNotNull(response.get(0).getValue(), "response value is null"); if (isOn(response.get(0).getValue())) { onStatus.call(); return openSession; } if (isOff(response.get(0).getValue())) { offStatus.call(); return openSession; } throw new IllegalStateException("unhandled lighting status"); }; } private  Lighting(String value); }
Lighting extends BaseOpenMessage { public static Func1<OpenSession, OpenSession> handleStatus(Action0 onStatus, Action0 offStatus) { return openSession -> { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_STATUS_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() == 1 || response.size() == 2, "invalid response"); checkNotNull(response.get(0).getValue(), "response value is null"); if (isOn(response.get(0).getValue())) { onStatus.call(); return openSession; } if (isOff(response.get(0).getValue())) { offStatus.call(); return openSession; } throw new IllegalStateException("unhandled lighting status"); }; } private  Lighting(String value); static Lighting requestTurnOn(String where); static Lighting requestTurnOn(String where, Type type, String bus); static Lighting requestTurnOff(String where); static Lighting requestTurnOff(String where, Type type, String bus); static Func1<OpenSession, OpenSession> handleResponse(Action0 onSuccess, Action0 onFail); static Lighting requestStatus(String where); static Lighting requestStatus(String where, Type type, String bus); static Func1<OpenSession, OpenSession> handleStatus(Action0 onStatus, Action0 offStatus); static boolean isValidRangeType(String where, Type type, String bus); }
Lighting extends BaseOpenMessage { public static Func1<OpenSession, OpenSession> handleStatus(Action0 onStatus, Action0 offStatus) { return openSession -> { isValidPrefixType(openSession.getRequest(), FORMAT_PREFIX_STATUS_WHO, WHO); List<OpenMessage> response = openSession.getResponse(); checkNotNull(response, "response is null"); checkArgument(response.size() == 1 || response.size() == 2, "invalid response"); checkNotNull(response.get(0).getValue(), "response value is null"); if (isOn(response.get(0).getValue())) { onStatus.call(); return openSession; } if (isOff(response.get(0).getValue())) { offStatus.call(); return openSession; } throw new IllegalStateException("unhandled lighting status"); }; } private  Lighting(String value); static Lighting requestTurnOn(String where); static Lighting requestTurnOn(String where, Type type, String bus); static Lighting requestTurnOff(String where); static Lighting requestTurnOff(String where, Type type, String bus); static Func1<OpenSession, OpenSession> handleResponse(Action0 onSuccess, Action0 onFail); static Lighting requestStatus(String where); static Lighting requestStatus(String where, Type type, String bus); static Func1<OpenSession, OpenSession> handleStatus(Action0 onStatus, Action0 offStatus); static boolean isValidRangeType(String where, Type type, String bus);  }