Search in sources :

Example 1 with RestNoLookupResultException

use of org.motechproject.mds.exception.rest.RestNoLookupResultException in project motech by motech.

the class MdsRestFacadeImpl method executeLookup.

@Override
@Transactional
public Object executeLookup(String lookupName, Map<String, String> lookupMap, QueryParams queryParams, boolean includeBlob) {
    if (lookupExecutors.containsKey(lookupName)) {
        LookupExecutor executor = lookupExecutors.get(lookupName);
        Object result = executor.execute(lookupMap, queryParams);
        if (result instanceof Collection) {
            if (includeBlob) {
                for (T value : ((Collection<T>) result)) {
                    getBlobs(value);
                }
            }
            return new RestResponse(entityName, entityClass.getName(), moduleName, namespace, executor.executeCount(lookupMap), queryParams, RestProjection.createProjectionCollection((Collection) result, restFields, blobFields));
        } else {
            if (result == null) {
                throw new RestNoLookupResultException("No result for lookup:" + lookupName);
            }
            if (includeBlob) {
                getBlobs((T) result);
            }
            return new RestResponse(entityName, entityClass.getName(), moduleName, namespace, 1l, new QueryParams(1, 1), RestProjection.createProjection(result, restFields, blobFields));
        }
    } else if (forbiddenLookupMethodNames.contains(lookupName)) {
        throw new RestLookupExecutionForbiddenException(lookupName);
    } else {
        throw new RestLookupNotFoundException(lookupName);
    }
}
Also used : RestNoLookupResultException(org.motechproject.mds.exception.rest.RestNoLookupResultException) RestLookupExecutionForbiddenException(org.motechproject.mds.exception.rest.RestLookupExecutionForbiddenException) RestLookupNotFoundException(org.motechproject.mds.exception.rest.RestLookupNotFoundException) Collection(java.util.Collection) QueryParams(org.motechproject.mds.query.QueryParams) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with RestNoLookupResultException

use of org.motechproject.mds.exception.rest.RestNoLookupResultException in project motech by motech.

the class MdsRestControllerTest method shouldReturn404WhenResultNotFoundForSingleValueLookup.

@Test
public void shouldReturn404WhenResultNotFoundForSingleValueLookup() throws Exception {
    when(restFacadeRetriever.getRestFacade(ENTITY_NAME, MODULE_NAME, NAMESPACE)).thenReturn(restFacade);
    when(restFacade.executeLookup(eq(LOOKUP_NAME), any(Map.class), any(QueryParams.class), anyBoolean())).thenThrow(new RestNoLookupResultException("No result found!"));
    String url = buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?lookup=" + LOOKUP_NAME + "&" + LOOKUP_PAGINATION_STR;
    mockMvc.perform(get(url)).andExpect(status().isNotFound());
}
Also used : RestNoLookupResultException(org.motechproject.mds.exception.rest.RestNoLookupResultException) QueryParams(org.motechproject.mds.query.QueryParams) Map(java.util.Map) Test(org.junit.Test)

Aggregations

RestNoLookupResultException (org.motechproject.mds.exception.rest.RestNoLookupResultException)2 QueryParams (org.motechproject.mds.query.QueryParams)2 Collection (java.util.Collection)1 Map (java.util.Map)1 Test (org.junit.Test)1 RestLookupExecutionForbiddenException (org.motechproject.mds.exception.rest.RestLookupExecutionForbiddenException)1 RestLookupNotFoundException (org.motechproject.mds.exception.rest.RestLookupNotFoundException)1 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)1 Transactional (org.springframework.transaction.annotation.Transactional)1