Lighting extends BaseOpenMessage { protected static void checkRangeType(String where, Type type, String bus) { checkNotNull(where, "invalid null value: [where]"); checkNotNull(type, "invalid null value: [type]"); checkNotNull(bus, "invalid null value: [bus]"); checkArgument(where.length() >= 1 && where.length() <= 4, "invalid length [1-4]"); switch (type) { case GENERAL: checkArgument(WHERE_GENERAL_VALUE.equals(where), "allowed value [0]"); checkArgument(bus.isEmpty(), "invalid bus size"); break; case GENERAL_BUS: checkArgument(WHERE_GENERAL_VALUE.equals(where), "allowed value [0]"); checkBus(bus); break; case AREA: checkArgument( "00".equals(where) || (isInRange(WHERE_MIN_VALUE_AREA, WHERE_MAX_VALUE_AREA, checkIsInteger(where)) && where.length() == 1) || "100".equals(where), "allowed value [00, 1−9, 100]"); checkArgument(bus.isEmpty(), "invalid bus size"); break; case AREA_BUS: checkArgument( "00".equals(where) || (isInRange(WHERE_MIN_VALUE_AREA, WHERE_MAX_VALUE_AREA, checkIsInteger(where)) && where.length() == 1) || "100".equals(where), "allowed value [00, 1−9, 100]"); checkBus(bus); break; case GROUP: checkRange(WHERE_MIN_VALUE_GROUP, WHERE_MAX_VALUE_GROUP, checkIsInteger(where)); checkArgument(bus.isEmpty(), "invalid bus size"); break; case GROUP_BUS: checkRange(WHERE_MIN_VALUE_GROUP, WHERE_MAX_VALUE_GROUP, checkIsInteger(where)); checkBus(bus); break; case POINT_TO_POINT: checkRange(WHERE_MIN_VALUE, WHERE_MAX_VALUE, checkIsInteger(where)); checkArgument(bus.isEmpty(), "invalid bus size"); break; case POINT_TO_POINT_BUS: checkRange(WHERE_MIN_VALUE, WHERE_MAX_VALUE, checkIsInteger(where)); checkBus(bus); break; default: throw new IllegalArgumentException("invalid type"); } } 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);  }
@Test public void testCheckRangeType() { checkRangeType("0", Type.GENERAL, NO_BUS); checkRangeType("0", Type.GENERAL_BUS, "02"); checkRangeType("00", Type.AREA, NO_BUS); checkRangeType("1", Type.AREA_BUS, "02"); checkRangeType("9", Type.AREA, NO_BUS); checkRangeType("100", Type.AREA_BUS, "02"); checkRangeType("1", Type.GROUP, NO_BUS); checkRangeType("255", Type.GROUP_BUS, "02"); checkRangeType("0", Type.POINT_TO_POINT, NO_BUS); checkRangeType("9999", Type.POINT_TO_POINT_BUS, "02"); }
@Test public void testCheckRangeType_invalidArgument() { assertThat(captureThrowable(() -> checkRangeType(null, Type.POINT_TO_POINT, NO_BUS))) .isInstanceOf(NullPointerException.class) .hasMessage("invalid null value: [where]"); assertThat(captureThrowable(() -> checkRangeType("1", null, NO_BUS))) .isInstanceOf(NullPointerException.class) .hasMessage("invalid null value: [type]"); assertThat(captureThrowable(() -> checkRangeType("1", Type.POINT_TO_POINT, null))) .isInstanceOf(NullPointerException.class) .hasMessage("invalid null value: [bus]"); assertThat(captureThrowable(() -> checkRangeType("", Type.POINT_TO_POINT, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid length [1-4]"); assertThat(captureThrowable(() -> checkRangeType("XXXXX", Type.POINT_TO_POINT_BUS, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid length [1-4]"); }
@Test public void testCheckRangeType_invalidGeneral() { assertThat(captureThrowable(() -> checkRangeType("1", Type.GENERAL, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [0]"); assertThat(captureThrowable(() -> checkRangeType("-1", Type.GENERAL, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [0]"); assertThat(captureThrowable(() -> checkRangeType("x", Type.GENERAL, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [0]"); assertThat(captureThrowable(() -> checkRangeType("#", Type.GENERAL, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [0]"); assertThat(captureThrowable(() -> checkRangeType("0", Type.GENERAL, "x"))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid bus size"); }
@Test public void testCheckRangeType_invalidArea() { assertThat(captureThrowable(() -> checkRangeType("0", Type.AREA, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [00, 1−9, 100]"); assertThat(captureThrowable(() -> checkRangeType("01", Type.AREA, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [00, 1−9, 100]"); assertThat(captureThrowable(() -> checkRangeType("10", Type.AREA, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [00, 1−9, 100]"); assertThat(captureThrowable(() -> checkRangeType("99", Type.AREA, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [00, 1−9, 100]"); assertThat(captureThrowable(() -> checkRangeType("101", Type.AREA, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [00, 1−9, 100]"); assertThat(captureThrowable(() -> checkRangeType("-1", Type.AREA, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("allowed value [00, 1−9, 100]"); assertThat(captureThrowable(() -> checkRangeType("x", Type.AREA, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid integer format"); assertThat(captureThrowable(() -> checkRangeType("#", Type.AREA, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid integer format"); assertThat(captureThrowable(() -> checkRangeType("1", Type.AREA, "x"))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid bus size"); }
@Test public void testCheckRangeType_invalidGroup() { assertThat(captureThrowable(() -> checkRangeType("-1", Type.GROUP, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("value must be between 1 and 255"); assertThat(captureThrowable(() -> checkRangeType("x", Type.GROUP, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid integer format"); assertThat(captureThrowable(() -> checkRangeType("#", Type.GROUP, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid integer format"); assertThat(captureThrowable(() -> checkRangeType("0", Type.GROUP, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("value must be between 1 and 255"); assertThat(captureThrowable(() -> checkRangeType("256", Type.GROUP, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("value must be between 1 and 255"); }
@Test public void testCheckRangeType_invalidPointToPoint() { assertThat(captureThrowable(() -> checkRangeType("-1", Type.POINT_TO_POINT, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("value must be between 0 and 9999"); assertThat(captureThrowable(() -> checkRangeType("10000", Type.POINT_TO_POINT, NO_BUS))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid length [1-4]"); assertThat(captureThrowable(() -> checkRangeType("-1", Type.POINT_TO_POINT_BUS, "01"))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("value must be between 0 and 9999"); assertThat(captureThrowable(() -> checkRangeType("10000", Type.POINT_TO_POINT_BUS, "02"))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("invalid length [1-4]"); }