Search in sources :

Example 26 with ServiceException

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

the class ClientServiceTest method testCreateEmptyIDError.

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

Example 27 with ServiceException

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

the class CmsSystemCodeCacheLoader method main.

public static void main(String[] args) {
    try {
        ApiSystemCodeDao dao = new SystemCodeDaoFileImpl();
        if (args.length > 1) {
        // dao = new FileSystemCodeDao(new File(args[0]));
        }
        ApiSystemCodeCache cache = new CmsSystemCodeCacheService(dao);
    } catch (ServiceException e) {
        LOGGER.error("FATAL ERROR", e);
    }
}
Also used : CmsSystemCodeCacheService(gov.ca.cwds.data.persistence.cms.CmsSystemCodeCacheService) SystemCodeDaoFileImpl(gov.ca.cwds.data.persistence.cms.SystemCodeDaoFileImpl) ServiceException(gov.ca.cwds.rest.services.ServiceException) ApiSystemCodeDao(gov.ca.cwds.data.persistence.cms.ApiSystemCodeDao) ApiSystemCodeCache(gov.ca.cwds.data.persistence.cms.ApiSystemCodeCache)

Example 28 with ServiceException

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

the class CmsKeyIdGenerator method doubleToStrN.

/**
 * @param dstLen the string width
 * @param src source value
 * @param powers the power vector for the destination base
 * @return the double to string
 */
public static String doubleToStrN(int dstLen, double src, final BigDecimal[] powers) {
    int i;
    int p = 0;
    double integral;
    double raw;
    final char[] dest = new char[20];
    final BigDecimal bdSrc = BigDecimal.valueOf(src);
    // NOSONAR
    for (i = 0; bdSrc.doubleValue() >= powers[i].doubleValue(); i++, p++) ;
    // Left-pad the string with the destination string width.
    final int pad = dstLen - p;
    if (pad < 0) {
        throw new ServiceException("invalid pad value");
    } else {
        for (i = 0; i < pad; i++) {
            dest[i] = ALPHABET[0];
        }
        for (i = 0; i < p; i++) {
            raw = src / powers[p - i - 1].doubleValue();
            // Break down the number and convert the integer portion to a character.
            integral = (int) raw;
            dest[i + pad] = ALPHABET[(int) Math.abs(integral)];
            // NOSONAR
            src -= (integral * powers[p - i - 1].doubleValue());
        }
    }
    final StringBuilder buf = new StringBuilder();
    for (final char c : dest) {
        if (c > 0) {
            buf.append(c);
        }
    }
    return buf.toString();
}
Also used : ServiceException(gov.ca.cwds.rest.services.ServiceException) BigDecimal(java.math.BigDecimal)

Example 29 with ServiceException

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

the class ElasticSearchPerson method handleHighlights.

protected static void handleHighlights(final SearchHit hit, final ElasticSearchPerson ret) {
    // ElasticSearch Java API returns map of highlighted fields
    final Map<String, HighlightField> h = hit.getHighlightFields();
    final Map<String, String> highlightValues = new LinkedHashMap<>();
    // Go through the HighlightFields returned from ES deal with fragments and create map.
    for (final Map.Entry<String, HighlightField> entry : h.entrySet()) {
        String highlightValue;
        final HighlightField highlightField = entry.getValue();
        final Text[] fragments = highlightField.fragments();
        if (fragments != null && fragments.length != 0) {
            final String[] texts = new String[fragments.length];
            for (int i = 0; i < fragments.length; i++) {
                texts[i] = fragments[i].string().trim();
            }
            highlightValue = StringUtils.join(texts, "...");
            highlightValues.put(DomainChef.camelCaseToLowerUnderscore(highlightField.getName()), highlightValue);
        }
    }
    // Update this ElasticSearchPerson property with the highlighted text.
    String highLights = "";
    try {
        highLights = MAPPER.writeValueAsString(highlightValues);
    } catch (JsonProcessingException e) {
        throw new ServiceException("ElasticSearch Person error: Failed serialize map to JSON " + ret.getSourceType() + ", doc id=" + ret.getId(), e);
    }
    ret.setHighlightFields(highLights);
    ret.setHighlights(highlightValues);
}
Also used : HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Text(org.elasticsearch.common.text.Text) LinkedHashMap(java.util.LinkedHashMap) ServiceException(gov.ca.cwds.rest.services.ServiceException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 30 with ServiceException

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

the class ServiceBackedResourceDelegateTest method setup.

@Before
public void setup() throws Exception {
    nonUniqueDomainObject = new ResourceDelegateTestDomainObject(ID_FOUND);
    uniqueDomainObject = new ResourceDelegateTestDomainObject(ID_NOT_FOUND);
    unexpectedExceptionDomainObject = new ResourceDelegateTestDomainObject(new Long(13));
    validationErrorMessageDomainObject = new ResourceDelegateTestDomainObject(new Long(15));
    ArrayList messages = new ArrayList<>();
    validationErrorMessageDomainObject.setMessages(messages);
    when(crudsService.find(ID_NOT_FOUND)).thenReturn(null);
    when(crudsService.find(ID_FOUND)).thenReturn(nonUniqueDomainObject);
    when(crudsService.delete(ID_NOT_FOUND)).thenReturn(null);
    when(crudsService.delete(ID_FOUND)).thenReturn(nonUniqueDomainObject);
    when(crudsService.create(eq(uniqueDomainObject))).thenReturn(new PrimaryKeyResponse(nonUniqueDomainObject.getId()));
    when(crudsService.create(eq(nonUniqueDomainObject))).thenThrow(new ServiceException(new EntityExistsException()));
    when(crudsService.create(eq(unexpectedExceptionDomainObject))).thenThrow(new ServiceException(new RuntimeException()));
    when(crudsService.create(eq(validationErrorMessageDomainObject))).thenReturn(validationErrorMessageDomainObject);
    when(crudsService.update(eq(1L), eq(unexpectedExceptionDomainObject))).thenThrow(new ServiceException(new RuntimeException()));
    when(crudsService.update(eq(-1L), eq(uniqueDomainObject))).thenThrow(new ServiceException(new EntityNotFoundException()));
    when(crudsService.update(eq(1L), eq(nonUniqueDomainObject))).thenReturn(new PrimaryKeyResponse(nonUniqueDomainObject.getId()));
}
Also used : ServiceException(gov.ca.cwds.rest.services.ServiceException) PrimaryKeyResponse(gov.ca.cwds.rest.api.PrimaryKeyResponse) ArrayList(java.util.ArrayList) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityExistsException(javax.persistence.EntityExistsException) Before(org.junit.Before)

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