From c5ed7f508391e4fa3f103292c7b57a70faf91940 Mon Sep 17 00:00:00 2001 From: pranavrd Date: Sun, 24 Sep 2023 20:41:21 +0530 Subject: [PATCH] fix: duplicate create of items --- .../catalogue/server/apiserver/CrudApis.java | 1 + .../validator/ValidatorServiceImpl.java | 66 +++++++++++++------ .../validator/ValidatorServiceImplTest.java | 3 +- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/main/java/iudx/catalogue/server/apiserver/CrudApis.java b/src/main/java/iudx/catalogue/server/apiserver/CrudApis.java index c5a8bd0e..a72639f1 100644 --- a/src/main/java/iudx/catalogue/server/apiserver/CrudApis.java +++ b/src/main/java/iudx/catalogue/server/apiserver/CrudApis.java @@ -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()) { diff --git a/src/main/java/iudx/catalogue/server/validator/ValidatorServiceImpl.java b/src/main/java/iudx/catalogue/server/validator/ValidatorServiceImpl.java index afe6f76e..f84276e1 100644 --- a/src/main/java/iudx/catalogue/server/validator/ValidatorServiceImpl.java +++ b/src/main/java/iudx/catalogue/server/validator/ValidatorServiceImpl.java @@ -112,9 +112,7 @@ String getReturnTypeForValidation(JsonObject result) { .toString(); } - /** - * {@inheritDoc} - * */ + /** {@inheritDoc} */ @SuppressWarnings("unchecked") public ValidatorService validateSchema( JsonObject request, Handler> handler) { @@ -157,38 +155,38 @@ public ValidatorService validateSchema( return this; } - /** - * {@inheritDoc} - * */ + /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public ValidatorService validateItem( JsonObject request, Handler> 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> handler) { + private void validateResourceGroup( + JsonObject request, String method, Handler> handler) { validateId(request, handler, isUacInstance); if (!isUacInstance && !request.containsKey(ID)) { UUID uuid = UUID.randomUUID(); @@ -216,13 +214,18 @@ private void validateResourceGroup(JsonObject request, Handler> handler) { + private void validateProvider( + JsonObject request, String method, Handler> handler) { // Validate if Provider validateId(request, handler, isUacInstance); if (!isUacInstance && !request.containsKey(ID)) { @@ -253,6 +256,10 @@ private void validateProvider(JsonObject request, Handler> handler) { + JsonObject request, String method, Handler> handler) { validateId(request, handler, isUacInstance); if (!isUacInstance && !request.containsKey(ID)) { UUID uuid = UUID.randomUUID(); @@ -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> handler) { + private void validateResource( + JsonObject request, String method, Handler> handler) { validateId(request, handler, isUacInstance); if (!isUacInstance && !request.containsKey("id")) { UUID uuid = UUID.randomUUID(); @@ -340,13 +352,18 @@ private void validateResource(JsonObject request, Handler 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> handler) { + private void validateCosItem( + JsonObject request, String method, Handler> handler) { validateId(request, handler, isUacInstance); if (!isUacInstance && !request.containsKey(ID)) { UUID uuid = UUID.randomUUID(); @@ -375,13 +392,18 @@ private void validateCosItem(JsonObject request, Handler 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> handler) { + private void validateOwnerItem( + JsonObject request, String method, Handler> handler) { validateId(request, handler, isUacInstance); if (!isUacInstance && !request.containsKey(ID)) { UUID uuid = UUID.randomUUID(); @@ -399,7 +421,13 @@ private void validateOwnerItem(JsonObject request, Handler 0) { + LOGGER.debug("Owner item already exists"); + handler.handle(Future.failedFuture("Fail: Owner item already exists")); + } else { + handler.handle(Future.succeededFuture(request)); + } }); } diff --git a/src/test/java/iudx/catalogue/server/validator/ValidatorServiceImplTest.java b/src/test/java/iudx/catalogue/server/validator/ValidatorServiceImplTest.java index e965b469..ac9e309d 100644 --- a/src/test/java/iudx/catalogue/server/validator/ValidatorServiceImplTest.java +++ b/src/test/java/iudx/catalogue/server/validator/ValidatorServiceImplTest.java @@ -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()) @@ -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>() {