CommandResponse { public static CommandResponse from( final String requestId, final String tenantId, final String deviceId, final Buffer payload, final String contentType, final Integer status) { Objects.requireNonNull(tenantId); Objects.requireNonNull(deviceId); if (requestId == null) { LOG.debug("cannot create CommandResponse: request id is null"); return null; } else if (INVALID_STATUS_CODE.test(status)) { LOG.debug("cannot create CommandResponse: status is invalid: {}", status); return null; } else if (requestId.length() < 3) { LOG.debug("cannot create CommandResponse: request id invalid: {}", requestId); return null; } else { try { final String replyToOptionsBitFlag = requestId.substring(0, 1); final boolean addDeviceIdToReply = Command.isReplyToContainedDeviceIdOptionSet(replyToOptionsBitFlag); final int lengthStringOne = Integer.parseInt(requestId.substring(1, 3), 16); final String replyId = requestId.substring(3 + lengthStringOne); return new CommandResponse( tenantId, deviceId, payload, contentType, status, requestId.substring(3, 3 + lengthStringOne), addDeviceIdToReply ? deviceId + "/" + replyId : replyId); } catch (NumberFormatException | StringIndexOutOfBoundsException se) { LOG.debug("error creating CommandResponse", se); return null; } } } private  CommandResponse(final String tenantId, final String deviceId, final Buffer payload,
            final String contentType, final int status, final String correlationId, final String replyToId); private  CommandResponse(final Message message, final String replyToId); static CommandResponse from(
            final String requestId,
            final String tenantId,
            final String deviceId,
            final Buffer payload,
            final String contentType,
            final Integer status); static CommandResponse from(final Message message); String getReplyToId(); String getCorrelationId(); int getStatus(); Message toMessage();  }
@Test public void testFromMessageFailsForMissingCorrelationId() { final boolean replyToContainedDeviceId = true; final String replyToOptionsBitFlag = Command.encodeReplyToOptions(replyToContainedDeviceId); final Message message = ProtonHelper.message(); message.setAddress(ResourceIdentifier .from(getCommandResponseEndpoint(), TENANT_ID, String.format("%s/%srid-1", DEVICE_ID, replyToOptionsBitFlag)).toString()); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK); final CommandResponse response = CommandResponse.from(message); assertThat(response).isNull(); }
@Test public void testFromMessageFailsForMissingAddress() { final Message message = ProtonHelper.message(); message.setCorrelationId(CORRELATION_ID); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK); final CommandResponse response = CommandResponse.from(message); assertThat(response).isNull(); }
@Test public void testFromMessageFailsForMissingStatus() { final boolean replyToContainedDeviceId = true; final String replyToOptionsBitFlag = Command.encodeReplyToOptions(replyToContainedDeviceId); final Message message = ProtonHelper.message(); message.setAddress(ResourceIdentifier .from(getCommandResponseEndpoint(), TENANT_ID, String.format("%s/%srid-1", DEVICE_ID, replyToOptionsBitFlag)).toString()); message.setCorrelationId(CORRELATION_ID); final CommandResponse response = CommandResponse.from(message); assertThat(response).isNull(); }
@Test public void testFromMessageFailsForInvalidStatus() { final boolean replyToContainedDeviceId = true; final String replyToOptionsBitFlag = Command.encodeReplyToOptions(replyToContainedDeviceId); final Message message = ProtonHelper.message(); message.setAddress(ResourceIdentifier .from(getCommandResponseEndpoint(), TENANT_ID, String.format("%s/%srid-1", DEVICE_ID, replyToOptionsBitFlag)).toString()); message.setCorrelationId(CORRELATION_ID); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_STATUS, 777); final CommandResponse response = CommandResponse.from(message); assertThat(response).isNull(); }
@Test public void testFromMessageFailsForInvalidAddressWithNothingBehindDeviceId() { final Message message = ProtonHelper.message(); message.setAddress(ResourceIdentifier.from(getCommandResponseEndpoint(), TENANT_ID, DEVICE_ID).toString()); message.setCorrelationId(CORRELATION_ID); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK); final CommandResponse response = CommandResponse.from(message); assertThat(response).isNull(); }
@Test public void testFromMessageFailsForInvalidAddressWithOnlyReplyToOptionsBit() { final boolean replyToContainedDeviceId = true; final String replyToOptionsBitFlag = Command.encodeReplyToOptions(replyToContainedDeviceId); final Message message = ProtonHelper.message(); message.setAddress(ResourceIdentifier .from(getCommandResponseEndpoint(), TENANT_ID, String.format("%s/%s", DEVICE_ID, replyToOptionsBitFlag)).toString()); message.setCorrelationId(CORRELATION_ID); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK); final CommandResponse response = CommandResponse.from(message); assertThat(response).isNull(); }
@Test public void testFromMessageFailsForInvalidAddressWithWrongReplyToOptionsBit() { final String replyToOptionsBitFlag = "X"; final Message message = ProtonHelper.message(); message.setAddress(ResourceIdentifier .from(getCommandResponseEndpoint(), TENANT_ID, String.format("%s/%srid-1", DEVICE_ID, replyToOptionsBitFlag)).toString()); message.setCorrelationId(CORRELATION_ID); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK); final CommandResponse response = CommandResponse.from(message); assertThat(response).isNull(); }
@Test public void testFromMessageFailsForInvalidAddressWithEmptyReplyId() { final boolean replyToContainedDeviceId = false; final String replyToOptionsBitFlag = Command.encodeReplyToOptions(replyToContainedDeviceId); final Message message = ProtonHelper.message(); message.setAddress(ResourceIdentifier .from(getCommandResponseEndpoint(), TENANT_ID, String.format("%s/%s%s", DEVICE_ID, replyToOptionsBitFlag, "")).toString()); message.setCorrelationId(CORRELATION_ID); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK); final CommandResponse response = CommandResponse.from(message); assertThat(response).isNull(); }