use of se.inera.intyg.webcert.web.service.utkast.dto.SaveDraftResponse in project webcert by sklintyg.
the class UtkastServiceImplTest method testSaveDraftDraftFirstSave.
@Test
public void testSaveDraftDraftFirstSave() throws Exception {
ValidationMessage valMsg = new ValidationMessage("a.category", "a.field.somewhere", ValidationMessageType.OTHER, "This is soooo wrong!");
ValidateDraftResponse validationResponse = new ValidateDraftResponse(ValidationStatus.INVALID, Collections.singletonList(valMsg));
WebCertUser user = createUser();
Utlatande utlatande = mock(Utlatande.class);
GrundData grunddata = new GrundData();
grunddata.setSkapadAv(new HoSPersonal());
grunddata.setPatient(defaultPatient);
when(utlatande.getGrundData()).thenReturn(grunddata);
when(mockUtkastRepository.findOne(INTYG_ID)).thenReturn(utkast);
when(moduleRegistry.getModuleApi(INTYG_TYPE)).thenReturn(mockModuleApi);
when(mockModuleApi.validateDraft(anyString())).thenReturn(validationResponse);
when(mockModuleApi.getUtlatandeFromJson(anyString())).thenReturn(utlatande);
when(mockUtkastRepository.save(utkast)).thenReturn(utkast);
when(mockModuleApi.shouldNotify(any(String.class), any(String.class))).thenReturn(true);
when(userService.getUser()).thenReturn(user);
when(mockModuleApi.updateBeforeSave(anyString(), any(HoSPersonal.class))).thenReturn("{}");
SaveDraftResponse res = draftService.saveDraft(INTYG_ID, UTKAST_VERSION, INTYG_JSON, true);
verify(mockUtkastRepository).save(any(Utkast.class));
// Assert notification message
verify(notificationService).sendNotificationForDraftChanged(any(Utkast.class));
// Assert pdl log
verify(logService).logUpdateIntyg(any(LogRequest.class));
verify(mockMonitoringService).logUtkastEdited(INTYG_ID, INTYG_TYPE);
assertNotNull("An DraftValidation should be returned", res);
assertEquals("Validation should fail", UtkastStatus.DRAFT_INCOMPLETE, res.getStatus());
}
use of se.inera.intyg.webcert.web.service.utkast.dto.SaveDraftResponse in project webcert by sklintyg.
the class UtkastModuleApiControllerTest method testSaveDraft.
@Test
public void testSaveDraft() {
String intygTyp = "fk7263";
String intygId = "intyg1";
String draftAsJson = "test";
byte[] payload = draftAsJson.getBytes();
setupUser(AuthoritiesConstants.PRIVILEGE_SKRIVA_INTYG, intygTyp, false, AuthoritiesConstants.FEATURE_HANTERA_INTYGSUTKAST);
when(utkastService.saveDraft(intygId, UTKAST_VERSION, draftAsJson, true)).thenReturn(new SaveDraftResponse(UTKAST_VERSION, UtkastStatus.DRAFT_COMPLETE));
Response response = moduleApiController.saveDraft(intygTyp, intygId, UTKAST_VERSION, false, payload, request);
verify(utkastService).saveDraft(intygId, UTKAST_VERSION, draftAsJson, true);
assertEquals(OK.getStatusCode(), response.getStatus());
}
use of se.inera.intyg.webcert.web.service.utkast.dto.SaveDraftResponse in project webcert by sklintyg.
the class UtkastServiceImplTest method testSaveDraftSecondSave.
@Test
public void testSaveDraftSecondSave() throws Exception {
ValidationMessage valMsg = new ValidationMessage("a.category", "a.field.somewhere", ValidationMessageType.OTHER, "This is soooo wrong!");
ValidateDraftResponse validationResponse = new ValidateDraftResponse(ValidationStatus.INVALID, Collections.singletonList(valMsg));
WebCertUser user = createUser();
Utlatande utlatande = mock(Utlatande.class);
GrundData grunddata = new GrundData();
grunddata.setSkapadAv(new HoSPersonal());
grunddata.setPatient(defaultPatient);
when(utlatande.getGrundData()).thenReturn(grunddata);
when(mockUtkastRepository.findOne(INTYG_ID)).thenReturn(utkast);
when(moduleRegistry.getModuleApi(INTYG_TYPE)).thenReturn(mockModuleApi);
when(mockModuleApi.validateDraft(anyString())).thenReturn(validationResponse);
when(mockModuleApi.getUtlatandeFromJson(anyString())).thenReturn(utlatande);
when(mockUtkastRepository.save(utkast)).thenReturn(utkast);
when(mockModuleApi.shouldNotify(any(String.class), any(String.class))).thenReturn(true);
when(userService.getUser()).thenReturn(user);
when(mockModuleApi.updateBeforeSave(anyString(), any(HoSPersonal.class))).thenReturn("{}");
SaveDraftResponse res = draftService.saveDraft(INTYG_ID, UTKAST_VERSION, INTYG_JSON, false);
verify(mockUtkastRepository).save(any(Utkast.class));
// Assert notification message
verify(notificationService).sendNotificationForDraftChanged(any(Utkast.class));
// Assert that no logs are called
verifyZeroInteractions(logService);
verifyZeroInteractions(mockMonitoringService);
assertNotNull("An DraftValidation should be returned", res);
assertEquals("Validation should fail", UtkastStatus.DRAFT_INCOMPLETE, res.getStatus());
}
use of se.inera.intyg.webcert.web.service.utkast.dto.SaveDraftResponse in project webcert by sklintyg.
the class UtkastModuleApiController method saveDraft.
/**
* Persists the supplied draft certificate using the intygId as key.
*
* @param intygsId The id of the certificate.
* @param payload Object holding the certificate and its current status.
*/
@PUT
@Path("/{intygsTyp}/{intygsId}/{version}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET)
public Response saveDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId, @PathParam("version") long version, @DefaultValue("false") @QueryParam("autoSave") boolean autoSave, byte[] payload, @Context HttpServletRequest request) {
authoritiesValidator.given(getWebCertUserService().getUser(), intygsTyp).features(AuthoritiesConstants.FEATURE_HANTERA_INTYGSUTKAST).privilege(AuthoritiesConstants.PRIVILEGE_SKRIVA_INTYG).orThrow();
LOG.debug("Saving utkast with id '{}', autosave is {}", intygsId, autoSave);
String draftAsJson = fromBytesToString(payload);
LOG.debug("---- intyg : " + draftAsJson);
boolean firstSave = false;
HttpSession session = request.getSession(true);
String lastSavedDraft = (String) session.getAttribute(LAST_SAVED_DRAFT);
if (!intygsId.equals(lastSavedDraft)) {
firstSave = true;
}
session.setAttribute(LAST_SAVED_DRAFT, intygsId);
try {
SaveDraftResponse saveResponse = utkastService.saveDraft(intygsId, version, draftAsJson, firstSave);
return Response.ok().entity(saveResponse).build();
} catch (OptimisticLockException | OptimisticLockingFailureException e) {
monitoringLogService.logUtkastConcurrentlyEdited(intygsId, intygsTyp);
throw new WebCertServiceException(WebCertServiceErrorCodeEnum.CONCURRENT_MODIFICATION, e.getMessage());
}
}
Aggregations