Search in sources :

Example 1 with IvrException

use of org.motechproject.mots.exception.IvrException in project mots by motech-implementations.

the class ModuleAssignmentServiceTest method assignModulesToDistrictShouldThrowCustomExceptionIfIvrServiceThrow.

@Test(expected = ModuleAssignmentException.class)
public void assignModulesToDistrictShouldThrowCustomExceptionIfIvrServiceThrow() throws Exception {
    when(communityHealthWorkerRepository.findByCommunityFacilityChiefdomDistrictIdAndSelected(any(), any())).thenReturn(Collections.singletonList(CHW));
    doThrow(new IvrException("message")).when(ivrService).addSubscriberToGroups(any(), any());
    moduleAssignmentService.assignModulesToDistrict(districtAssignmentDto);
}
Also used : IvrException(org.motechproject.mots.exception.IvrException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with IvrException

use of org.motechproject.mots.exception.IvrException in project mots by motech-implementations.

the class CommunityHealthWorkerService method saveHealthWorker.

/**
 * Update CHW and IVR Subscriber.
 * @param chw CHW to update
 * @return saved CHW
 */
@PreAuthorize(RoleNames.HAS_CHW_WRITE_ROLE)
public CommunityHealthWorker saveHealthWorker(CommunityHealthWorker chw) {
    String ivrId = chw.getIvrId();
    String phoneNumber = chw.getPhoneNumber();
    String name = chw.getCombinedName();
    Language preferredLanguage = chw.getPreferredLanguage();
    try {
        ivrService.updateSubscriber(ivrId, phoneNumber, name, preferredLanguage);
    } catch (IvrException ex) {
        String message = "Could not update CHW, because of IVR subscriber update error. \n\n" + ex.getClearVotoInfo();
        throw new ChwException(message, ex);
    }
    return healthWorkerRepository.save(chw);
}
Also used : ChwException(org.motechproject.mots.exception.ChwException) IvrException(org.motechproject.mots.exception.IvrException) Language(org.motechproject.mots.domain.enums.Language) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with IvrException

use of org.motechproject.mots.exception.IvrException in project mots by motech-implementations.

the class ModuleAssignmentServiceTest method assignModulesShouldThrowCustomExceptionIfIvrServiceThrow.

@Test(expected = ModuleAssignmentException.class)
public void assignModulesShouldThrowCustomExceptionIfIvrServiceThrow() throws Exception {
    when(moduleProgressService.getModuleProgress(any(), any())).thenReturn(getModuleProgress(ProgressStatus.NOT_STARTED));
    doThrow(new IvrException("message")).when(ivrService).addSubscriberToGroups(any(), any());
    moduleAssignmentService.assignModules(newAssignedModules);
}
Also used : IvrException(org.motechproject.mots.exception.IvrException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with IvrException

use of org.motechproject.mots.exception.IvrException in project mots by motech-implementations.

the class IvrService method sendVotoRequest.

private <T> T sendVotoRequest(String url, MultiValueMap<String, String> params, ParameterizedTypeReference<T> responseType, HttpMethod method) throws IvrException {
    params.add(API_KEY, ivrApiKey);
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParams(params);
    HttpEntity<?> request = new HttpEntity<>(headers);
    try {
        ResponseEntity<T> responseEntity = restTemplate.exchange(builder.build().toString(), method, request, responseType);
        return responseEntity.getBody();
    } catch (RestClientResponseException ex) {
        String responseBodyJson = ex.getResponseBodyAsString();
        String responseMessage = responseBodyJson;
        String clearVotoInfo = "";
        try {
            VotoResponseDto<String> votoResponse = mapper.readValue(responseBodyJson, new TypeReference<VotoResponseDto<String>>() {
            });
            if (votoResponse.getMessage() != null) {
                responseMessage = votoResponse.getMessage();
                clearVotoInfo = "Invalid IVR service response: " + responseMessage;
            }
        } catch (IOException e) {
            responseMessage = responseBodyJson;
        }
        String message = "Invalid IVR service response: " + ex.getRawStatusCode() + " " + ex.getStatusText() + ", Response body: " + responseMessage;
        throw new IvrException(message, ex, clearVotoInfo);
    } catch (RestClientException ex) {
        String message = "Error occurred when sending request to IVR service: " + ex.getMessage();
        throw new IvrException(message, ex);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) IvrException(org.motechproject.mots.exception.IvrException) HttpEntity(org.springframework.http.HttpEntity) VotoResponseDto(org.motechproject.mots.dto.VotoResponseDto) IOException(java.io.IOException) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) RestClientException(org.springframework.web.client.RestClientException) RestClientResponseException(org.springframework.web.client.RestClientResponseException) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 5 with IvrException

use of org.motechproject.mots.exception.IvrException in project mots by motech-implementations.

the class ModuleAssignmentService method assignModules.

/**
 * Assign modules to CHW.
 * @param assignedModules modules assigned for CHW
 */
@SuppressWarnings("checkstyle:variabledeclarationusagedistance")
@Transactional
@PreAuthorize(RoleNames.HAS_ASSIGN_MODULES_ROLE)
public void assignModules(AssignedModules assignedModules) {
    AssignedModules existingAssignedModules = getAssignedModules(assignedModules.getHealthWorker().getId());
    CommunityHealthWorker assignedModulesChw = existingAssignedModules.getHealthWorker();
    Set<Module> oldModules = new HashSet<>(existingAssignedModules.getModules());
    existingAssignedModules.setModules(assignedModules.getModules());
    repository.save(existingAssignedModules);
    entityManager.flush();
    entityManager.refresh(existingAssignedModules);
    Set<Module> newModules = new HashSet<>(existingAssignedModules.getModules());
    Set<Module> modulesToAdd = getModulesToAdd(oldModules, newModules);
    Set<Module> modulesToRemove = getModulesToRemove(oldModules, newModules);
    validateModulesToUnassign(assignedModulesChw, modulesToRemove);
    String ivrId = assignedModulesChw.getIvrId();
    if (ivrId == null) {
        throw new ModuleAssignmentException("Could not assign module to CHW, because CHW has empty IVR id");
    }
    try {
        ivrService.addSubscriberToGroups(ivrId, getIvrGroupsFromModules(modulesToAdd));
        ivrService.removeSubscriberFromGroups(ivrId, getIvrGroupsFromModules(modulesToRemove));
    } catch (IvrException ex) {
        String message = "Could not assign or delete module for CHW, " + "because of IVR module assignment error.\n\n" + ex.getClearVotoInfo();
        throw new ModuleAssignmentException(message, ex);
    }
    moduleProgressService.removeModuleProgresses(assignedModulesChw, modulesToRemove);
    moduleProgressService.createModuleProgresses(assignedModulesChw, modulesToAdd);
}
Also used : AssignedModules(org.motechproject.mots.domain.AssignedModules) IvrException(org.motechproject.mots.exception.IvrException) ModuleAssignmentException(org.motechproject.mots.exception.ModuleAssignmentException) CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker) CourseModule(org.motechproject.mots.domain.CourseModule) Module(org.motechproject.mots.domain.Module) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

IvrException (org.motechproject.mots.exception.IvrException)8 AssignedModules (org.motechproject.mots.domain.AssignedModules)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 ModuleAssignmentException (org.motechproject.mots.exception.ModuleAssignmentException)3 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 CommunityHealthWorker (org.motechproject.mots.domain.CommunityHealthWorker)2 CourseModule (org.motechproject.mots.domain.CourseModule)2 Module (org.motechproject.mots.domain.Module)2 Language (org.motechproject.mots.domain.enums.Language)2 ChwException (org.motechproject.mots.exception.ChwException)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Transactional (org.springframework.transaction.annotation.Transactional)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 IOException (java.io.IOException)1 DistrictAssignmentLog (org.motechproject.mots.domain.DistrictAssignmentLog)1 User (org.motechproject.mots.domain.security.User)1 VotoResponseDto (org.motechproject.mots.dto.VotoResponseDto)1 EntityNotFoundException (org.motechproject.mots.exception.EntityNotFoundException)1 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)1