Search in sources :

Example 1 with RestLookupNotFoundException

use of org.motechproject.mds.exception.rest.RestLookupNotFoundException 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 RestLookupNotFoundException

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

the class MdsRestControllerTest method shouldReturn404ForNotExistingLookups.

// lookup errors
@Test
public void shouldReturn404ForNotExistingLookups() 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 RestLookupNotFoundException(LOOKUP_NAME));
    mockMvc.perform(get(buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?lookup=" + LOOKUP_NAME)).andExpect(status().isNotFound());
    verify(restFacade).executeLookup(eq(LOOKUP_NAME), any(Map.class), any(QueryParams.class), anyBoolean());
}
Also used : RestLookupNotFoundException(org.motechproject.mds.exception.rest.RestLookupNotFoundException) QueryParams(org.motechproject.mds.query.QueryParams) Map(java.util.Map) Test(org.junit.Test)

Aggregations

RestLookupNotFoundException (org.motechproject.mds.exception.rest.RestLookupNotFoundException)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 RestNoLookupResultException (org.motechproject.mds.exception.rest.RestNoLookupResultException)1 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)1 Transactional (org.springframework.transaction.annotation.Transactional)1