Search in sources :

Example 1 with ServiceException

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

the class StaffPersonServiceTest method failsWhenPostedStaffPersonIdBlank.

@SuppressWarnings("javadoc")
@Test
public void failsWhenPostedStaffPersonIdBlank() throws Exception {
    try {
        StaffPerson staffPersonDomain = MAPPER.readValue(fixture("fixtures/domain/legacy/StaffPerson/valid/valid.json"), StaffPerson.class);
        gov.ca.cwds.data.persistence.cms.StaffPerson toCreate = new gov.ca.cwds.data.persistence.cms.StaffPerson("   ", staffPersonDomain, "2017-01-07");
        when(staffPersonDao.create(any(gov.ca.cwds.data.persistence.cms.StaffPerson.class))).thenReturn(toCreate);
        PostedStaffPerson expected = new PostedStaffPerson(toCreate);
        Assert.fail("Expected AssertionError not thrown");
    } catch (ServiceException e) {
        assertEquals("StaffPerson ID cannot be empty", e.getMessage());
    }
}
Also used : PostedStaffPerson(gov.ca.cwds.rest.api.domain.cms.PostedStaffPerson) StaffPerson(gov.ca.cwds.rest.api.domain.cms.StaffPerson) PostedStaffPerson(gov.ca.cwds.rest.api.domain.cms.PostedStaffPerson) ServiceException(gov.ca.cwds.rest.services.ServiceException) Test(org.junit.Test)

Example 2 with ServiceException

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

the class StaffPersonServiceTest method failsWhenPostedStaffPersonIdEmmpty.

@SuppressWarnings("javadoc")
@Test
public void failsWhenPostedStaffPersonIdEmmpty() throws Exception {
    try {
        StaffPerson staffPersonDomain = MAPPER.readValue(fixture("fixtures/domain/legacy/StaffPerson/valid/valid.json"), StaffPerson.class);
        gov.ca.cwds.data.persistence.cms.StaffPerson toCreate = new gov.ca.cwds.data.persistence.cms.StaffPerson("", staffPersonDomain, "2017-01-07");
        when(staffPersonDao.create(any(gov.ca.cwds.data.persistence.cms.StaffPerson.class))).thenReturn(toCreate);
        PostedStaffPerson expected = new PostedStaffPerson(toCreate);
        Assert.fail("Expected AssertionError not thrown");
    } catch (ServiceException e) {
        assertEquals("StaffPerson ID cannot be empty", e.getMessage());
    }
}
Also used : PostedStaffPerson(gov.ca.cwds.rest.api.domain.cms.PostedStaffPerson) StaffPerson(gov.ca.cwds.rest.api.domain.cms.StaffPerson) PostedStaffPerson(gov.ca.cwds.rest.api.domain.cms.PostedStaffPerson) ServiceException(gov.ca.cwds.rest.services.ServiceException) Test(org.junit.Test)

Example 3 with ServiceException

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

the class ESPerson method makeESPerson.

/**
   * Produce an ESPerson domain from native ElasticSearch {@link SearchHit}. Parse JSON results and
   * populate associated fields.
   * 
   * <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
   * @see #pullCol(Map, ESColumn)
   */
public static ESPerson makeESPerson(SearchHit hit) {
    final Map<String, Object> m = hit.getSource();
    ESPerson ret = new ESPerson(ESPerson.<String>pullCol(m, ESColumn.ID), ESPerson.<String>pullCol(m, ESColumn.FIRST_NAME), ESPerson.<String>pullCol(m, ESColumn.LAST_NAME), ESPerson.<String>pullCol(m, ESColumn.GENDER), ESPerson.<String>pullCol(m, ESColumn.BIRTH_DATE), ESPerson.<String>pullCol(m, ESColumn.SSN), ESPerson.<String>pullCol(m, ESColumn.TYPE), ESPerson.<String>pullCol(m, ESColumn.SOURCE), null, 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 ELASTICSEARCH! class={}, id={}", ret.getSourceType(), ret.getId());
            }
            if (!StringUtils.isBlank(ret.getSourceJson())) {
                // Remove excess whitespace. Don't 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.
                // STORY #137216799: again.
                final Object obj = MAPPER.readValue(json, Class.forName(ret.getSourceType().replaceAll("gov\\.ca\\.cwds\\.rest\\.api\\.", "gov\\.ca\\.cwds\\.data\\."), false, Thread.currentThread().getContextClassLoader()));
                ret.sourceObj = obj;
            }
        } catch (ClassNotFoundException ce) {
            throw new ServiceException("ElasticSearch Person error: Failed to instantiate class " + ret.getSourceType() + ", ES person id=" + ret.getId(), ce);
        } 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) ServiceException(gov.ca.cwds.rest.services.ServiceException)

Example 4 with ServiceException

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

the class AllegationPerpetratorHistoryServiceTest method testCreateBlankIDError.

@Override
@Test
public void testCreateBlankIDError() throws Exception {
    try {
        AllegationPerpetratorHistory allegationPerpetratorHistoryDomain = MAPPER.readValue(fixture("fixtures/domain/legacy/AllegationPerpetratorHistory/valid/valid.json"), AllegationPerpetratorHistory.class);
        gov.ca.cwds.data.persistence.cms.AllegationPerpetratorHistory toCreate = new gov.ca.cwds.data.persistence.cms.AllegationPerpetratorHistory(" ", allegationPerpetratorHistoryDomain, "ABC");
        when(allegationPerpetratorHistoryDao.create(any(gov.ca.cwds.data.persistence.cms.AllegationPerpetratorHistory.class))).thenReturn(toCreate);
        PostedAllegationPerpetratorHistory expected = new PostedAllegationPerpetratorHistory(toCreate);
    } catch (ServiceException e) {
        assertEquals("AllegationPerpetratorHistory ID cannot be blank", e.getMessage());
    }
}
Also used : PostedAllegationPerpetratorHistory(gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory) ServiceException(gov.ca.cwds.rest.services.ServiceException) PostedAllegationPerpetratorHistory(gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory) AllegationPerpetratorHistory(gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory) Test(org.junit.Test)

Example 5 with ServiceException

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

the class CrossReportService method update.

/**
   * {@inheritDoc}
   * 
   * @see gov.ca.cwds.rest.services.CrudsService#update(java.io.Serializable,
   *      gov.ca.cwds.rest.api.Request)
   */
@Override
public gov.ca.cwds.rest.api.domain.cms.CrossReport update(Serializable primaryKey, Request request) {
    assert request instanceof gov.ca.cwds.rest.api.domain.cms.CrossReport;
    gov.ca.cwds.rest.api.domain.cms.CrossReport crossReport = (gov.ca.cwds.rest.api.domain.cms.CrossReport) request;
    try {
        String lastUpdatedId = staffPersonIdRetriever.getStaffPersonId();
        CrossReport managed = new CrossReport(crossReport.getThirdId(), crossReport, lastUpdatedId);
        managed = crossReportDao.update(managed);
        return new gov.ca.cwds.rest.api.domain.cms.CrossReport(managed);
    } catch (EntityNotFoundException e) {
        LOGGER.info("CrossReport not found : {}", crossReport);
        throw new ServiceException(e);
    }
}
Also used : CrossReport(gov.ca.cwds.data.persistence.cms.CrossReport) ServiceException(gov.ca.cwds.rest.services.ServiceException) EntityNotFoundException(javax.persistence.EntityNotFoundException)

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