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());
}
}
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());
}
}
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;
}
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());
}
}
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);
}
}
Aggregations