Skip to content

Commit

Permalink
Merge pull request #280 from pranavrd/fix/validation-service
Browse files Browse the repository at this point in the history
fix: duplicate create of items
  • Loading branch information
pranavrd authored Sep 24, 2023
2 parents b397d66 + c5ed7f5 commit 0189e09
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ private void handleItemCreation(RoutingContext routingContext,
.getResponse());
} else {
LOGGER.debug("Success: JWT Auth successful");
requestBody.put(HTTP_METHOD, routingContext.request().method().toString());
/* Link Validating the request to ensure item correctness */
validatorService.validateItem(requestBody, valhandler -> {
if (valhandler.failed()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ String getReturnTypeForValidation(JsonObject result) {
.toString();
}

/**
* {@inheritDoc}
* */
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public ValidatorService validateSchema(
JsonObject request, Handler<AsyncResult<JsonObject>> handler) {
Expand Down Expand Up @@ -157,38 +155,38 @@ public ValidatorService validateSchema(
return this;
}

/**
* {@inheritDoc}
* */
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public ValidatorService validateItem(
JsonObject request, Handler<AsyncResult<JsonObject>> handler) {

request.put(CONTEXT, vocContext);
String method = (String) request.remove(HTTP_METHOD);

String itemType = getItemType(request, handler);
LOGGER.debug("Info: itemType: " + itemType);

// Validate if Resource
if (itemType.equalsIgnoreCase(ITEM_TYPE_RESOURCE)) {
validateResource(request, handler);
validateResource(request, method, handler);
} else if (itemType.equalsIgnoreCase(ITEM_TYPE_RESOURCE_SERVER)) {
// Validate if Resource Server TODO: More checks and auth rules
validateResourceServer(request, handler);
validateResourceServer(request, method, handler);
} else if (itemType.equalsIgnoreCase(ITEM_TYPE_PROVIDER)) {
validateProvider(request, handler);
validateProvider(request, method, handler);
} else if (itemType.equalsIgnoreCase(ITEM_TYPE_RESOURCE_GROUP)) {
validateResourceGroup(request, handler);
validateResourceGroup(request, method, handler);
} else if (itemType.equalsIgnoreCase(ITEM_TYPE_COS)) {
validateCosItem(request, handler);
validateCosItem(request, method, handler);
} else if (itemType.equalsIgnoreCase(ITEM_TYPE_OWNER)) {
validateOwnerItem(request, handler);
validateOwnerItem(request, method, handler);
}
return this;
}

private void validateResourceGroup(JsonObject request, Handler<AsyncResult<JsonObject>> handler) {
private void validateResourceGroup(
JsonObject request, String method, Handler<AsyncResult<JsonObject>> handler) {
validateId(request, handler, isUacInstance);
if (!isUacInstance && !request.containsKey(ID)) {
UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -216,13 +214,18 @@ private void validateResourceGroup(JsonObject request, Handler<AsyncResult<JsonO
if (res.result().getInteger(TOTAL_HITS) < 1 || !returnType.contains(ITEM_TYPE_PROVIDER)) {
LOGGER.debug("Provider does not exist");
handler.handle(Future.failedFuture("Fail: Provider item doesn't exist"));
} else if (method.equalsIgnoreCase(REQUEST_POST)
&& returnType.contains(ITEM_TYPE_RESOURCE_GROUP)) {
LOGGER.debug("RG already exists");
handler.handle(Future.failedFuture("Fail: Resource Group item already exists"));
} else {
handler.handle(Future.succeededFuture(request));
}
});
}

private void validateProvider(JsonObject request, Handler<AsyncResult<JsonObject>> handler) {
private void validateProvider(
JsonObject request, String method, Handler<AsyncResult<JsonObject>> handler) {
// Validate if Provider
validateId(request, handler, isUacInstance);
if (!isUacInstance && !request.containsKey(ID)) {
Expand Down Expand Up @@ -253,14 +256,18 @@ private void validateProvider(JsonObject request, Handler<AsyncResult<JsonObject
|| !returnType.contains(ITEM_TYPE_RESOURCE_SERVER)) {
LOGGER.debug("RS does not exist");
handler.handle(Future.failedFuture("Fail: Resource Server item doesn't exist"));
} else if (method.equalsIgnoreCase(REQUEST_POST)
&& returnType.contains(ITEM_TYPE_PROVIDER)) {
LOGGER.debug("Provider already exists");
handler.handle(Future.failedFuture("Fail: Provider item already exists"));
} else {
handler.handle(Future.succeededFuture(request));
}
});
}

private void validateResourceServer(
JsonObject request, Handler<AsyncResult<JsonObject>> handler) {
JsonObject request, String method, Handler<AsyncResult<JsonObject>> handler) {
validateId(request, handler, isUacInstance);
if (!isUacInstance && !request.containsKey(ID)) {
UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -290,13 +297,18 @@ private void validateResourceServer(
if (res.result().getInteger(TOTAL_HITS) < 1 || !returnType.contains(ITEM_TYPE_COS)) {
LOGGER.debug("Cos does not exist");
handler.handle(Future.failedFuture("Fail: Cos item doesn't exist"));
} else if (method.equalsIgnoreCase(REQUEST_POST)
&& returnType.contains(ITEM_TYPE_RESOURCE_SERVER)) {
LOGGER.debug("RS already exists");
handler.handle(Future.failedFuture("Fail: Resource Server item already exists"));
} else {
handler.handle(Future.succeededFuture(request));
}
});
}

private void validateResource(JsonObject request, Handler<AsyncResult<JsonObject>> handler) {
private void validateResource(
JsonObject request, String method, Handler<AsyncResult<JsonObject>> handler) {
validateId(request, handler, isUacInstance);
if (!isUacInstance && !request.containsKey("id")) {
UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -340,13 +352,18 @@ private void validateResource(JsonObject request, Handler<AsyncResult<JsonObject
&& !returnType.contains(ITEM_TYPE_RESOURCE_GROUP)) {
LOGGER.debug("RG does not exist");
handler.handle(Future.failedFuture("Fail: Resource Group item doesn't exist"));
} else if (method.equalsIgnoreCase(REQUEST_POST)
&& res.result().getInteger(TOTAL_HITS) > 3) {
LOGGER.debug("RI already exists");
handler.handle(Future.failedFuture("Fail: Resource item already exists"));
} else {
handler.handle(Future.succeededFuture(request));
}
});
}

private void validateCosItem(JsonObject request, Handler<AsyncResult<JsonObject>> handler) {
private void validateCosItem(
JsonObject request, String method, Handler<AsyncResult<JsonObject>> handler) {
validateId(request, handler, isUacInstance);
if (!isUacInstance && !request.containsKey(ID)) {
UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -375,13 +392,18 @@ private void validateCosItem(JsonObject request, Handler<AsyncResult<JsonObject>
if (res.result().getInteger(TOTAL_HITS) < 1 || !returnType.contains(ITEM_TYPE_OWNER)) {
LOGGER.debug("Owner does not exist");
handler.handle(Future.failedFuture("Fail: Owner item doesn't exist"));
} else if (method.equalsIgnoreCase(REQUEST_POST)
&& returnType.contains(ITEM_TYPE_COS)) {
LOGGER.debug("COS already exists");
handler.handle(Future.failedFuture("Fail: COS item already exists"));
} else {
handler.handle(Future.succeededFuture(request));
}
});
}

private void validateOwnerItem(JsonObject request, Handler<AsyncResult<JsonObject>> handler) {
private void validateOwnerItem(
JsonObject request, String method, Handler<AsyncResult<JsonObject>> handler) {
validateId(request, handler, isUacInstance);
if (!isUacInstance && !request.containsKey(ID)) {
UUID uuid = UUID.randomUUID();
Expand All @@ -399,7 +421,13 @@ private void validateOwnerItem(JsonObject request, Handler<AsyncResult<JsonObjec
handler.handle(Future.failedFuture(VALIDATION_FAILURE_MSG));
return;
}
handler.handle(Future.succeededFuture(request));
if (method.equalsIgnoreCase(REQUEST_POST)
&& res.result().getInteger(TOTAL_HITS) > 0) {
LOGGER.debug("Owner item already exists");
handler.handle(Future.failedFuture("Fail: Owner item already exists"));
} else {
handler.handle(Future.succeededFuture(request));
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void testValidateSchema(String itemType, VertxTestContext vertxTestContex
@DisplayName("test validate item")
public void testValidateItem(
String item, String parent, int invocations, VertxTestContext testContext) {
JsonObject request = requestBody().put(TYPE, new JsonArray().add(item));
JsonObject request = requestBody().put(TYPE, new JsonArray().add(item)).put(HTTP_METHOD, REQUEST_POST);

when(asyncResult.failed()).thenReturn(false);
when(asyncResult.result())
Expand Down Expand Up @@ -209,6 +209,7 @@ public void testValidateItemParentNotExists(
public void testValidateOwnerItem(VertxTestContext testContext) {
JsonObject request = requestBody();
request.put(TYPE, new JsonArray().add(ITEM_TYPE_OWNER));
request.put(HTTP_METHOD, REQUEST_POST);

doAnswer(
new Answer<AsyncResult<JsonObject>>() {
Expand Down

0 comments on commit 0189e09

Please sign in to comment.