Skip to content

Commit

Permalink
Issue #559: Fixed static factory method of `RetrieveFeatureDesiredPro…
Browse files Browse the repository at this point in the history
…pertiesResponse`.

When passing null for a `JsonObject` it threw a NPE. However, the parameter is annotated with `@Nullable`.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Oct 15, 2021
1 parent b1b6050 commit 1cdda55
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,19 @@ public static RetrieveFeatureDesiredPropertiesResponse of(final ThingId thingId,
* @param jsonObject the retrieved desired properties JSON.
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
* @throws NullPointerException if any argument but {@code jsonObject} is {@code null}.
*/
public static RetrieveFeatureDesiredPropertiesResponse of(final ThingId thingId,
final CharSequence featureId,
@Nullable final JsonObject jsonObject,
final DittoHeaders dittoHeaders) {

checkNotNull(jsonObject, "jsonObject");
final FeatureProperties desiredProperties = ThingsModelFactory.newFeatureProperties(jsonObject);
final FeatureProperties desiredProperties;
if (null == jsonObject) {
desiredProperties = ThingsModelFactory.newFeatureProperties(JsonFactory.nullObject());
} else {
desiredProperties = ThingsModelFactory.newFeatureProperties(jsonObject);
}

return of(thingId, featureId, desiredProperties, dittoHeaders);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,30 @@
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;

import org.assertj.core.api.Assertions;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.things.model.FeatureProperties;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.TestConstants;
import org.eclipse.ditto.things.model.signals.commands.ThingCommandResponse;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import nl.jqno.equalsverifier.EqualsVerifier;

/**
* Unit test for {@link RetrieveFeatureDesiredPropertiesResponse}.
*/
public class RetrieveFeatureDesiredPropertiesResponseTest {
public final class RetrieveFeatureDesiredPropertiesResponseTest {

@Rule
public final TestName testName = new TestName();

private static final JsonSchemaVersion KNOWN_SCHEMA_VERSION = JsonSchemaVersion.V_2;

Expand All @@ -47,46 +53,57 @@ public class RetrieveFeatureDesiredPropertiesResponseTest {
TestConstants.Feature.HOVER_BOARD_DESIRED_PROPERTIES.toJson(KNOWN_SCHEMA_VERSION))
.build();


@Test
public void assertImmutability() {
assertInstancesOf(RetrieveFeatureDesiredPropertiesResponse.class, areImmutable(),
provided(FeatureProperties.class, ThingId.class).isAlsoImmutable());
}


@Test
public void testHashCodeAndEquals() {
EqualsVerifier.forClass(RetrieveFeatureDesiredPropertiesResponse.class)
.withRedefinedSuperclass()
.verify();
}


@Test(expected = NullPointerException.class)
public void tryToCreateInstanceWithNullFeatureDesiredProperties() {
RetrieveFeatureDesiredPropertiesResponse.of(TestConstants.Thing.THING_ID, TestConstants.Feature.HOVER_BOARD_ID,
RetrieveFeatureDesiredPropertiesResponse.of(TestConstants.Thing.THING_ID,
TestConstants.Feature.HOVER_BOARD_ID,
null,
TestConstants.EMPTY_DITTO_HEADERS);
}


@Test
public void toJsonReturnsExpected() {
public void getInstanceWithNullFeaturePropertiesJsonObject() {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().correlationId(testName.getMethodName()).build();
final RetrieveFeatureDesiredPropertiesResponse underTest =
RetrieveFeatureDesiredPropertiesResponse.of(TestConstants.Thing.THING_ID,
TestConstants.Feature.HOVER_BOARD_ID,
TestConstants.Feature.HOVER_BOARD_DESIRED_PROPERTIES, TestConstants.EMPTY_DITTO_HEADERS);
final String actualJsonString = underTest.toJson(FieldType.regularOrSpecial()).toString();
(JsonObject) null,
dittoHeaders);

Assertions.assertThat(actualJsonString).isEqualTo(KNOWN_JSON.toString());
assertThat(underTest.getDesiredProperties()).isEqualTo(ThingsModelFactory.emptyFeatureProperties());
}

@Test
public void toJsonReturnsExpected() {
final RetrieveFeatureDesiredPropertiesResponse underTest = RetrieveFeatureDesiredPropertiesResponse.of(
TestConstants.Thing.THING_ID,
TestConstants.Feature.HOVER_BOARD_ID,
TestConstants.Feature.HOVER_BOARD_DESIRED_PROPERTIES,
TestConstants.EMPTY_DITTO_HEADERS
);
final String actualJsonString = underTest.toJson(FieldType.regularOrSpecial()).toString();

assertThat(actualJsonString).isEqualTo(KNOWN_JSON.toString());
}

@Test
public void createInstanceFromValidJson() {
final RetrieveFeatureDesiredPropertiesResponse underTest =
RetrieveFeatureDesiredPropertiesResponse.fromJson(KNOWN_JSON.toString(), TestConstants.EMPTY_DITTO_HEADERS);
RetrieveFeatureDesiredPropertiesResponse.fromJson(KNOWN_JSON.toString(),
TestConstants.EMPTY_DITTO_HEADERS);

assertThat(underTest).isNotNull();
assertThat(underTest.getDesiredProperties()).isEqualTo(TestConstants.Feature.HOVER_BOARD_DESIRED_PROPERTIES);
Expand Down

0 comments on commit 1cdda55

Please sign in to comment.