Search in sources :

Example 56 with ServiceException

use of gov.ca.cwds.rest.services.ServiceException in project API by ca-cwds.

the class ReferralServiceTest method testCreateNullIDError.

@Override
@Test
public void testCreateNullIDError() throws Exception {
    try {
        Referral referralDomain = MAPPER.readValue(fixture("fixtures/domain/legacy/Referral/valid/valid.json"), Referral.class);
        gov.ca.cwds.data.persistence.cms.Referral toCreate = new gov.ca.cwds.data.persistence.cms.Referral(null, referralDomain, "0XA");
        when(referralDao.create(any(gov.ca.cwds.data.persistence.cms.Referral.class))).thenReturn(toCreate);
        @SuppressWarnings("unused") PostedReferral expected = new PostedReferral(toCreate);
    } catch (ServiceException e) {
        assertEquals("Referral ID cannot be empty", e.getMessage());
    }
}
Also used : ServiceException(gov.ca.cwds.rest.services.ServiceException) Referral(gov.ca.cwds.rest.api.domain.cms.Referral) PostedReferral(gov.ca.cwds.rest.api.domain.cms.PostedReferral) PostedReferral(gov.ca.cwds.rest.api.domain.cms.PostedReferral) Test(org.junit.Test)

Example 57 with ServiceException

use of gov.ca.cwds.rest.services.ServiceException in project api-core by ca-cwds.

the class ElasticSearchPerson method makeESPerson.

/**
 * Produce an ElasticSearchPerson domain instance from native ElasticSearch {@link SearchHit}.
 * Parse JSON results and populate associated fields. ElasticSearch Java API returns an overly
 * broad type of {@code Map<String,Object>}. The enum "knows" how to extract columns of a given
 * type.
 *
 * <p>
 * <strong>Classloader Note:</strong> When running in an application server, the root classloader
 * may not know of our domain/persistence class, and so we look it up using the current thread's
 * classloader, like so:
 * </p>
 *
 * <blockquote>
 * {@code Class.forName("some.nested.class", false, Thread.currentThread().getContextClassLoader())}
 * </blockquote>
 *
 * @param hit search result
 * @return populated domain-level ES object
 * @deprecated retrieves ES document objects for the obsolete Person Search.
 * @see #pullCol(Map, ESColumn)
 */
@Deprecated
public static ElasticSearchPerson makeESPerson(final SearchHit hit) {
    final Map<String, Object> m = hit.getSource();
    ElasticSearchPerson ret = new ElasticSearchPerson(ElasticSearchPerson.<String>pullCol(m, ESColumn.ID), ElasticSearchPerson.<String>pullCol(m, ESColumn.FIRST_NAME), ElasticSearchPerson.<String>pullCol(m, ESColumn.LAST_NAME), null, null, ElasticSearchPerson.<String>pullCol(m, ESColumn.GENDER), ElasticSearchPerson.<String>pullCol(m, ESColumn.BIRTH_DATE), ElasticSearchPerson.<String>pullCol(m, ESColumn.SSN), ElasticSearchPerson.<String>pullCol(m, ESColumn.TYPE), ElasticSearchPerson.<String>pullCol(m, ESColumn.SOURCE), "", null, null, null, null);
    if (!StringUtils.isBlank(ret.getSourceType()) && !StringUtils.isBlank(ret.getSourceJson())) {
        try {
            // Tech debt: reverse compatibility with existing ElasticSearch documents.
            if (ret.getSourceType().startsWith("gov.ca.cwds.rest.api.")) {
                LOGGER.warn("LEGACY CLASS IN ES! class={}, id={}", ret.getSourceType(), ret.getId());
            }
            // Remove excess whitespace.
            // No job should store excess whitespace in ElasticSearch!
            final String json = ret.getSourceJson().replaceAll("\\s+\",", "\",");
            // Dynamically instantiate the domain class specified by "type" and load from JSON.
            // Note: When running in an application server, the app server's root classloader may not
            // know of our domain/persistence class, but the current thread's classloader should.
            final Object obj = MAPPER.readValue(json, Class.forName(ret.getSourceType(), false, Thread.currentThread().getContextClassLoader()));
            ret.sourceObj = obj;
            handleHighlights(hit, ret);
        } catch (Exception e) {
            throw new ServiceException("ElasticSearch Person error: " + e.getMessage() + ", ES person id=" + ret.getId(), e);
        }
    }
    return ret;
}
Also used : ServiceException(gov.ca.cwds.rest.services.ServiceException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ServiceException(gov.ca.cwds.rest.services.ServiceException)

Example 58 with ServiceException

use of gov.ca.cwds.rest.services.ServiceException in project api-core by ca-cwds.

the class SimpleResourceDelegateTest method find_A$Object_T$ServiceException.

@Test(expected = ServiceException.class)
public void find_A$Object_T$ServiceException() throws Exception {
    final String key = "asdfasdf";
    when(svc.find(any())).thenThrow(new ServiceException(new IllegalArgumentException()));
    final Response response = target.find(key);
    assertTrue(response.getStatus() != Response.Status.OK.ordinal());
}
Also used : Response(javax.ws.rs.core.Response) ServiceException(gov.ca.cwds.rest.services.ServiceException) Test(org.junit.Test)

Example 59 with ServiceException

use of gov.ca.cwds.rest.services.ServiceException in project api-core by ca-cwds.

the class SimpleResourceDelegateTest method handle_A$Object_T$ServiceException.

@Test(expected = ApiException.class)
public void handle_A$Object_T$ServiceException() throws Exception {
    TestApiRequest req = new TestApiRequest("&*^(%*#");
    when(svc.handle(req)).thenThrow(new ApiException(new ServiceException(new IllegalArgumentException())));
    target.handle(req);
    final Response response = target.handle(req);
    assertTrue(response.getStatus() != Response.Status.OK.ordinal());
}
Also used : Response(javax.ws.rs.core.Response) ServiceException(gov.ca.cwds.rest.services.ServiceException) ApiException(gov.ca.cwds.rest.api.ApiException) Test(org.junit.Test)

Aggregations

ServiceException (gov.ca.cwds.rest.services.ServiceException)59 Test (org.junit.Test)22 EntityNotFoundException (javax.persistence.EntityNotFoundException)16 EntityExistsException (javax.persistence.EntityExistsException)15 StaffPerson (gov.ca.cwds.data.persistence.cms.StaffPerson)7 PostedReferral (gov.ca.cwds.rest.api.domain.cms.PostedReferral)5 PostedReporter (gov.ca.cwds.rest.api.domain.cms.PostedReporter)5 PostedStaffPerson (gov.ca.cwds.rest.api.domain.cms.PostedStaffPerson)5 StaffPerson (gov.ca.cwds.rest.api.domain.cms.StaffPerson)5 PostedAllegation (gov.ca.cwds.rest.api.domain.cms.PostedAllegation)4 PostedAllegationPerpetratorHistory (gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory)4 PostedClient (gov.ca.cwds.rest.api.domain.cms.PostedClient)4 PostedLongText (gov.ca.cwds.rest.api.domain.cms.PostedLongText)4 Referral (gov.ca.cwds.rest.api.domain.cms.Referral)3 Reporter (gov.ca.cwds.rest.api.domain.cms.Reporter)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Address (gov.ca.cwds.data.persistence.cms.Address)2 Allegation (gov.ca.cwds.data.persistence.cms.Allegation)2 AllegationPerpetratorHistory (gov.ca.cwds.data.persistence.cms.AllegationPerpetratorHistory)2 ChildClient (gov.ca.cwds.data.persistence.cms.ChildClient)2