Skip to content

Commit

Permalink
CIRC-2111 Add validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkurash committed Jun 20, 2024
1 parent 3a87757 commit 5a9f605
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
23 changes: 18 additions & 5 deletions src/main/java/org/folio/circulation/domain/CirculationSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.folio.circulation.support.json.JsonPropertyFetcher.getProperty;

import java.lang.invoke.MethodHandles;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -19,6 +20,11 @@
public class CirculationSetting {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());

public static final String ID_FIELD = "id";
public static final String NAME_FIELD = "name";
public static final String VALUE_FIELD = "value";
public static final String METADATA_FIELD = "metadata";

@ToString.Include
@Getter
private final JsonObject representation;
Expand All @@ -33,14 +39,21 @@ public class CirculationSetting {
private final JsonObject value;

public static CirculationSetting from(JsonObject representation) {
if (getProperty(representation, "name") == null ||
getObjectProperty(representation, "value") == null) {
if (getProperty(representation, ID_FIELD) == null ||
getProperty(representation, NAME_FIELD) == null ||
getObjectProperty(representation, VALUE_FIELD) == null ||
!containsOnlyKnownFields(representation)) {

log.info("from:: Circulation setting JSON is malformed: {}", representation);
log.info("from:: Circulation setting JSON is invalid: {}", representation);
return null;
}

return new CirculationSetting(representation, getProperty(representation, "id"),
getProperty(representation, "name"), getObjectProperty(representation, "value"));
return new CirculationSetting(representation, getProperty(representation, ID_FIELD),
getProperty(representation, NAME_FIELD), getObjectProperty(representation, VALUE_FIELD));
}

private static boolean containsOnlyKnownFields(JsonObject representation) {
return Set.of(ID_FIELD, NAME_FIELD, VALUE_FIELD, METADATA_FIELD)
.containsAll(representation.fieldNames());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,27 +120,27 @@ void empty(RoutingContext routingContext) {
.thenAccept(context::writeResultToHttpResponse);
}

private void setRandomIdIfMissing(JsonObject representation) {
private static void setRandomIdIfMissing(JsonObject representation) {
final var providedId = getProperty(representation, "id");
if (providedId == null) {
representation.put("id", UUID.randomUUID().toString());
}
}

private Function<Result<CirculationSetting>, Result<CirculationSetting>>
private static Function<Result<CirculationSetting>, Result<CirculationSetting>>
refuseWhenCirculationSettingIsInvalid() {

return r -> r.failWhen(circulationSetting -> succeeded(circulationSetting == null),
circulationSetting -> singleValidationError("Circulation setting JSON is malformed", "", ""));
circulationSetting -> singleValidationError("Circulation setting JSON is invalid", "", ""));
}

private Function<Result<String>, Result<String>> refuseWhenIdIsInvalid() {
private static Function<Result<String>, Result<String>> refuseWhenIdIsInvalid() {
return r -> r.failWhen(id -> succeeded(!uuidIsValid(id)),
circulationSetting -> singleValidationError("Circulation setting ID is not a valid UUID",
"", ""));
}

private boolean uuidIsValid(String providedId) {
private static boolean uuidIsValid(String providedId) {
try {
return providedId != null && providedId.equals(UUID.fromString(providedId).toString());
} catch(IllegalArgumentException e) {
Expand Down
38 changes: 27 additions & 11 deletions src/test/java/api/settings/CirculationSettingsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

class CirculationSettingsTests extends APITests {

public static final String NAME = "name";
public static final String VALUE = "value";
public static final String ERRORS = "errors";
public static final String MESSAGE = "message";
public static final String INVALID_JSON_MESSAGE = "Circulation setting JSON is invalid";

@Test
void crudOperationsTest() {
// Testing POST method
Expand All @@ -23,8 +29,8 @@ void crudOperationsTest() {

// Testing GET (individual setting) method
final var settingById = circulationSettingsClient.get(settingId);
assertThat(settingById.getJson().getString("name"), is("initial-name"));
assertThat(settingById.getJson().getJsonObject("value").getString("initial-key"),
assertThat(settingById.getJson().getString(NAME), is("initial-name"));
assertThat(settingById.getJson().getJsonObject(VALUE).getString("initial-key"),
is("initial-value"));

// Testing GET (all) method
Expand All @@ -38,8 +44,8 @@ void crudOperationsTest() {
circulationSettingsClient.delete(anotherSetting.getId());
final var allSettingsAfterDeletion = circulationSettingsClient.getMany(CqlQuery.noQuery());
assertThat(allSettingsAfterDeletion.size(), is(1));
assertThat(allSettingsAfterDeletion.getFirst().getString("name"), is("initial-name"));
assertThat(allSettingsAfterDeletion.getFirst().getJsonObject("value").getString("initial-key"),
assertThat(allSettingsAfterDeletion.getFirst().getString(NAME), is("initial-name"));
assertThat(allSettingsAfterDeletion.getFirst().getJsonObject(VALUE).getString("initial-key"),
is("initial-value"));

// Testing PUT method
Expand All @@ -50,8 +56,8 @@ void crudOperationsTest() {

final var updatedSetting = circulationSettingsClient.get(settingId);

assertThat(updatedSetting.getJson().getString("name"), is("new-name"));
assertThat(updatedSetting.getJson().getJsonObject("value").getString("new-key"),
assertThat(updatedSetting.getJson().getString(NAME), is("new-name"));
assertThat(updatedSetting.getJson().getJsonObject(VALUE).getString("new-key"),
is("new-value"));
}

Expand All @@ -68,7 +74,7 @@ void invalidRequestsTest() {
// Testing GET with invalid ID (not a UUID)
var getErrors = restAssuredClient.get(circulationSettingsUrl("/not-a-uuid"), 422,
"get-circulation-setting");
assertThat(getErrors.getJson().getJsonArray("errors").getJsonObject(0).getString("message"),
assertThat(getErrors.getJson().getJsonArray(ERRORS).getJsonObject(0).getString(MESSAGE),
is("Circulation setting ID is not a valid UUID"));

// Testing DELETE with invalid ID
Expand All @@ -78,13 +84,23 @@ void invalidRequestsTest() {
// Testing PUT with malformed JSON
var putErrors = restAssuredClient.put("{\"invalid-field\": \"invalid-value\"}",
circulationSettingsUrl("/" + randomId()), 422, "put-circulation-setting");
assertThat(putErrors.getJson().getJsonArray("errors").getJsonObject(0).getString("message"),
is("Circulation setting JSON is malformed"));
assertThat(putErrors.getJson().getJsonArray(ERRORS).getJsonObject(0).getString(MESSAGE),
is(INVALID_JSON_MESSAGE));

var putErrorsNoValue = restAssuredClient.put("{\"name\": \"test-name\"}",
circulationSettingsUrl("/" + randomId()), 422, "put-circulation-setting");
assertThat(putErrorsNoValue.getJson().getJsonArray(ERRORS).getJsonObject(0).getString(MESSAGE),
is(INVALID_JSON_MESSAGE));

// Testing POST with malformed JSON
var postErrors = restAssuredClient.post("{\"invalid-field\": \"invalid-value\"}",
circulationSettingsUrl(""), 422, "put-circulation-setting");
assertThat(postErrors.getJson().getJsonArray("errors").getJsonObject(0).getString("message"),
is("Circulation setting JSON is malformed"));
assertThat(postErrors.getJson().getJsonArray(ERRORS).getJsonObject(0).getString(MESSAGE),
is(INVALID_JSON_MESSAGE));

var postErrorsNoValue = restAssuredClient.put("{\"name\": \"test-name\"}",
circulationSettingsUrl("/" + randomId()), 422, "put-circulation-setting");
assertThat(postErrorsNoValue.getJson().getJsonArray(ERRORS).getJsonObject(0).getString(MESSAGE),
is(INVALID_JSON_MESSAGE));
}
}

0 comments on commit 5a9f605

Please sign in to comment.