Skip to content

Commit

Permalink
Merge pull request #272 from pranavrd/chore/update-list-api
Browse files Browse the repository at this point in the history
Chore: update list api
  • Loading branch information
pranavrd authored Sep 20, 2023
2 parents 1d86937 + 2e78987 commit a3045b6
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 42 deletions.
43 changes: 42 additions & 1 deletion docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/listTypesSuccessResponse'
oneOf:
- $ref: '#/components/schemas/listTypesSuccessResponse'
- $ref: '#/components/schemas/listTypesOwner'
'400':
description: Bad Request
content:
Expand Down Expand Up @@ -2119,6 +2121,45 @@ components:
type: 'urn:dx:cat:InvalidSyntax'
title: Invalid Syntax
detail: Wrong Item Type
listTypesOwner:
type: object
title: Response for list owner
description: API response when item type is owner
properties:
type:
type: string
description: |
URN type of the response indicating if the query was successful or if any errors have
been triggered.
title:
description: A human readable title to the message response.
totalHits:
type: integer
description: Total number of documents which matched the query.
format: int32
results:
type: array
description: Results which matched the query. This is an array of objects.
items:
type: object
required:
- type
example:
type: urn:dx:cat:Success
title: Success
totalHits: 1
results:
- '@context': https://voc.iudx.org.in/
type:
- iudx:Owner
name: IudxOwner
description: owner item for IUDX entities
id: cc18c925-8990-3a07-9cac-0bb2bd8166f6
itemStatus: ACTIVE
itemCreatedAt: 2023-09-08T10:00:02+0530



invalidIdErrorResponse:
type: object
title: Invalid ID
Expand Down
104 changes: 63 additions & 41 deletions src/main/java/iudx/catalogue/server/apiserver/ListApis.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/**
*
*
* <h1>SearchApis.java</h1>
* Callback handlers for List APIS
*
* <p>Callback handlers for List APIS
*/

package iudx.catalogue.server.apiserver;

import static iudx.catalogue.server.apiserver.util.Constants.*;
import static iudx.catalogue.server.util.Constants.*;

import io.vertx.core.AsyncResult;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
Expand All @@ -16,18 +20,13 @@
import iudx.catalogue.server.apiserver.util.QueryMapper;
import iudx.catalogue.server.apiserver.util.RespBuilder;
import iudx.catalogue.server.database.DatabaseService;
import iudx.catalogue.server.util.Api;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public final class ListApis {


private DatabaseService dbService;

private static final Logger LOGGER = LogManager.getLogger(ListApis.class);

private DatabaseService dbService;

public void setDbService(DatabaseService dbService) {
this.dbService = dbService;
Expand Down Expand Up @@ -91,49 +90,72 @@ public void listItemsHandler(RoutingContext routingContext) {
break;
default:
LOGGER.error("Fail: Invalid itemType:" + itemType);
response.setStatusCode(400)
.end(new RespBuilder()
.withType(TYPE_INVALID_SYNTAX)
.withTitle(TITLE_INVALID_SYNTAX)
.withDetail(DETAIL_WRONG_ITEM_TYPE)
.getResponse());
response
.setStatusCode(400)
.end(
new RespBuilder()
.withType(TYPE_INVALID_SYNTAX)
.withTitle(TITLE_INVALID_SYNTAX)
.withDetail(DETAIL_WRONG_ITEM_TYPE)
.getResponse());
return;
}
requestBody.put(TYPE, type);

/* Request database service with requestBody for listing items */
dbService.listItems(requestBody, dbhandler -> {
if (dbhandler.succeeded()) {
LOGGER.info("Success: Item listing");
response.setStatusCode(200).end(dbhandler.result().toString());
} else if (dbhandler.failed()) {
LOGGER.error(
"Fail: Issue in listing " + itemType + ": " + dbhandler.cause().getMessage());
response.setStatusCode(400)
.end(new RespBuilder()
.withType(TYPE_INVALID_SYNTAX)
.withTitle(TITLE_INVALID_SYNTAX)
.withDetail(DETAIL_WRONG_ITEM_TYPE)
.getResponse());
}
});
if (type.equalsIgnoreCase(ITEM_TYPE_OWNER) || type.equalsIgnoreCase(ITEM_TYPE_COS)) {
dbService.listOwnerOrCos(
requestBody,
dbHandler -> {
handleResponseFromDatabase(response, itemType, dbHandler);
});
} else {

/* Request database service with requestBody for listing items */
dbService.listItems(
requestBody,
dbhandler -> {
handleResponseFromDatabase(response, itemType, dbhandler);
});
}
} else {
LOGGER.error("Fail: Search/Count; Invalid request query parameters");
response.setStatusCode(400)
.end(new RespBuilder()
.withType(TYPE_INVALID_SYNTAX)
.withTitle(TITLE_INVALID_SYNTAX)
.withDetail(DETAIL_WRONG_ITEM_TYPE)
.getResponse());
response
.setStatusCode(400)
.end(
new RespBuilder()
.withType(TYPE_INVALID_SYNTAX)
.withTitle(TITLE_INVALID_SYNTAX)
.withDetail(DETAIL_WRONG_ITEM_TYPE)
.getResponse());
}
} else {
LOGGER.error("Fail: Search/Count; Invalid request query parameters");
response.setStatusCode(400)
.end(new RespBuilder()
.withType(TYPE_INVALID_SYNTAX)
.withTitle(TITLE_INVALID_SYNTAX)
.withDetail(DETAIL_WRONG_ITEM_TYPE)
.getResponse());
response
.setStatusCode(400)
.end(
new RespBuilder()
.withType(TYPE_INVALID_SYNTAX)
.withTitle(TITLE_INVALID_SYNTAX)
.withDetail(DETAIL_WRONG_ITEM_TYPE)
.getResponse());
}
}

void handleResponseFromDatabase(
HttpServerResponse response, String itemType, AsyncResult<JsonObject> dbhandler) {
if (dbhandler.succeeded()) {
LOGGER.info("Success: Item listing");
response.setStatusCode(200).end(dbhandler.result().toString());
} else if (dbhandler.failed()) {
LOGGER.error("Fail: Issue in listing " + itemType + ": " + dbhandler.cause().getMessage());
response
.setStatusCode(400)
.end(
new RespBuilder()
.withType(TYPE_INVALID_SYNTAX)
.withTitle(TITLE_INVALID_SYNTAX)
.withDetail(DETAIL_WRONG_ITEM_TYPE)
.getResponse());
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/iudx/catalogue/server/database/DatabaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ DatabaseService nlpSearchLocationQuery(JsonArray request, JsonObject queryParams
@Fluent
DatabaseService listItems(JsonObject request, Handler<AsyncResult<JsonObject>> handler);

/**
* The listOwnerOrCos implements the fetch of entire owner or cos item from the database.
*
* @param request which is a JsonObject
* @param handler which is a Request Handler
* @return DatabaseService which is a service
*/
@Fluent
DatabaseService listOwnerOrCos(JsonObject request, Handler<AsyncResult<JsonObject>> handler);

/**
* The listRelationship implements the list resource, resourceGroup, provider, resourceServer,
* type relationships operation with the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,39 @@ public DatabaseService listItems(JsonObject request, Handler<AsyncResult<JsonObj
return this;
}

/**
* {@inheritDoc}
* */
@Override
public DatabaseService listOwnerOrCos(
JsonObject request, Handler<AsyncResult<JsonObject>> handler) {
RespBuilder respBuilder = new RespBuilder();
String elasticQuery = queryDecoder.listItemQuery(request);

LOGGER.debug("Info: Listing items;" + elasticQuery);

client.searchAsync(
elasticQuery,
docIndex,
clientHandler -> {
if (clientHandler.succeeded()) {
LOGGER.debug("Success: Successful DB request");
JsonObject responseJson = clientHandler.result();
handler.handle(Future.succeededFuture(responseJson));
} else {
LOGGER.error("Fail: DB request has failed;" + clientHandler.cause());
/* Handle request error */
handler.handle(
Future.failedFuture(
respBuilder
.withType(TYPE_INTERNAL_SERVER_ERROR)
.withTitle(TITLE_INTERNAL_SERVER_ERROR)
.getResponse()));
}
});
return this;
}

/**
* {@inheritDoc}
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,64 @@ public AsyncResult<JsonObject> answer(InvocationOnMock arg0) throws Throwable {
});
}

@Test
@Description("successful test listOwnerOrCos")
public void testListOwnerOrCos(VertxTestContext vertxTestContext) {
JsonObject request = new JsonObject();
request.put(ITEM_TYPE, ITEM_TYPE_OWNER).put(TYPE_KEY, OWNER);
DatabaseServiceImpl.client = mock(ElasticClient.class);
when(asyncResult.succeeded()).thenReturn(true);
doAnswer(
new Answer<AsyncResult<JsonObject>>() {
@Override
public AsyncResult<JsonObject> answer(InvocationOnMock arg0) throws Throwable {
((Handler<AsyncResult<JsonObject>>) arg0.getArgument(2)).handle(asyncResult);
return null;
}
})
.when(DatabaseServiceImpl.client)
.searchAsync(anyString(), anyString(), any());
dbService.listOwnerOrCos(
request,
handler -> {
if (handler.succeeded()) {
verify(DatabaseServiceImpl.client, times(1)).searchAsync(anyString(),anyString(), any());
vertxTestContext.completeNow();
} else {
vertxTestContext.failNow("fail");
}
});
}

@Test
@Description("test listOwnerOrCos on failure")
public void failureTestListOwnerOrCos(VertxTestContext vertxTestContext) {
JsonObject request = new JsonObject();
request.put(ITEM_TYPE, ITEM_TYPE_COS).put(TYPE_KEY, COS);
DatabaseServiceImpl.client = mock(ElasticClient.class);
when(asyncResult.succeeded()).thenReturn(false);
doAnswer(
new Answer<AsyncResult<JsonObject>>() {
@Override
public AsyncResult<JsonObject> answer(InvocationOnMock arg0) throws Throwable {
((Handler<AsyncResult<JsonObject>>) arg0.getArgument(2)).handle(asyncResult);
return null;
}
})
.when(DatabaseServiceImpl.client)
.searchAsync(anyString(),anyString(), any());
dbService.listOwnerOrCos(
request,
handler -> {
if (handler.failed()) {
verify(DatabaseServiceImpl.client, times(1)).searchAsync(anyString(),anyString(), any());
vertxTestContext.completeNow();
} else {
vertxTestContext.failNow("Fail");
}
});
}

@Test
@Description("test countQuery when method returns Null")
public void testCountQueryHandler(VertxTestContext vertxTestContext) {
Expand Down

0 comments on commit a3045b6

Please sign in to comment.