Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTYGFV-16770: Added aggregator for get certificate events #1089

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2024 Inera AB (http://www.inera.se)
*
* This file is part of sklintyg (https://github.com/sklintyg).
*
* sklintyg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sklintyg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package se.inera.intyg.webcert.web.csintegration.aggregate;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import se.inera.intyg.common.support.modules.support.facade.dto.CertificateEventDTO;
import se.inera.intyg.webcert.web.csintegration.util.CertificateServiceProfile;
import se.inera.intyg.webcert.web.service.facade.GetCertificateEventsFacadeService;

@Service("getCertificateEventsAggregator")
public class GetCertificateEventsAggregator implements GetCertificateEventsFacadeService {

private final GetCertificateEventsFacadeService getCertificateEventsFromWC;
private final GetCertificateEventsFacadeService getCertificateEventsFromCS;
private final CertificateServiceProfile certificateServiceProfile;

public GetCertificateEventsAggregator(
@Qualifier("getCertificateEventsFromWebcert")
GetCertificateEventsFacadeService getCertificateEventsFromWC,
@Qualifier("getCertificateEventsFromCertificateService")
GetCertificateEventsFacadeService replaceCertificateFromCertificateService,
CertificateServiceProfile certificateServiceProfile) {
this.getCertificateEventsFromWC = getCertificateEventsFromWC;
this.getCertificateEventsFromCS = replaceCertificateFromCertificateService;
this.certificateServiceProfile = certificateServiceProfile;
}

@Override
public CertificateEventDTO[] getCertificateEvents(String certificateId) {
if (!certificateServiceProfile.active()) {
return getCertificateEventsFromWC.getCertificateEvents(certificateId);
}

final var responseFromCS = getCertificateEventsFromCS.getCertificateEvents(certificateId);

return responseFromCS != null ? responseFromCS : getCertificateEventsFromWC.getCertificateEvents(certificateId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2024 Inera AB (http://www.inera.se)
*
* This file is part of sklintyg (https://github.com/sklintyg).
*
* sklintyg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sklintyg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package se.inera.intyg.webcert.web.csintegration.certificate;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import se.inera.intyg.common.support.modules.support.facade.dto.CertificateEventDTO;
import se.inera.intyg.webcert.web.csintegration.integration.CSIntegrationRequestFactory;
import se.inera.intyg.webcert.web.csintegration.integration.CSIntegrationService;
import se.inera.intyg.webcert.web.service.facade.GetCertificateEventsFacadeService;

@Service("getCertificateEventsFromCertificateService")
@Slf4j
@RequiredArgsConstructor
public class GetCertificateEventsFromCertificateService implements GetCertificateEventsFacadeService {

private final CSIntegrationService csIntegrationService;
private final CSIntegrationRequestFactory csIntegrationRequestFactory;

@Override
public CertificateEventDTO[] getCertificateEvents(String certificateId) {
log.debug("Attempting to retrieve events for certificate '{}' from Certificate Service", certificateId);

if (Boolean.FALSE.equals(csIntegrationService.certificateExists(certificateId))) {
log.debug("Certificate '{}' does not exist in certificate service", certificateId);
return null;
}

final var events = csIntegrationService.getCertificateEvents(
certificateId, csIntegrationRequestFactory.getCertificateEventsRequest()
);

log.debug("Got '{}' events for certificate '{}' from Certificate Service", events.length, certificateId);

return events;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import se.inera.intyg.webcert.web.csintegration.integration.dto.DeleteCertificateRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.DeleteMessageRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.ForwardCertificateRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateEventsRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateFromMessageRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateMessageRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateRequestDTO;
Expand Down Expand Up @@ -345,6 +346,15 @@ public ForwardCertificateRequestDTO forwardCertificateRequest() {
.build();
}

public GetCertificateEventsRequestDTO getCertificateEventsRequest() {
return GetCertificateEventsRequestDTO.builder()
.user(certificateServiceUserHelper.get())
.unit(certificateServiceUnitHelper.getUnit())
.careUnit(certificateServiceUnitHelper.getCareUnit())
.careProvider(certificateServiceUnitHelper.getCareProvider())
.build();
}

private String convertReason(String reason) {
switch (reason) {
case "FEL_PATIENT":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.web.client.RestTemplate;
import se.inera.intyg.common.support.facade.model.Certificate;
import se.inera.intyg.common.support.facade.model.question.Question;
import se.inera.intyg.common.support.modules.support.facade.dto.CertificateEventDTO;
import se.inera.intyg.common.support.modules.support.facade.dto.ValidationErrorDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.AnswerComplementRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.AnswerComplementResponseDTO;
Expand All @@ -57,6 +58,8 @@
import se.inera.intyg.webcert.web.csintegration.integration.dto.DeleteMessageRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.ForwardCertificateRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.ForwardCertificateResponseDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateEventsRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateEventsResponseDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateFromMessageRequestDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateFromMessageResponseDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateMessageInternalResponseDTO;
Expand Down Expand Up @@ -724,4 +727,16 @@ public List<Certificate> lockDrafts(LockDraftsRequestDTO request) {

return response.getCertificates();
}

public CertificateEventDTO[] getCertificateEvents(String certificateId, GetCertificateEventsRequestDTO request) {
final var url = baseUrl + CERTIFICATE_ENDPOINT_URL + "/" + certificateId + "/events";

final var response = restTemplate.postForObject(url, request, GetCertificateEventsResponseDTO.class);

if (response == null || response.getEvents() == null) {
throw new IllegalStateException(NULL_RESPONSE_EXCEPTION);
}

return response.getEvents();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2024 Inera AB (http://www.inera.se)
*
* This file is part of sklintyg (https://github.com/sklintyg).
*
* sklintyg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sklintyg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package se.inera.intyg.webcert.web.csintegration.integration.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import lombok.Builder;
import lombok.Value;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateEventsRequestDTO.GetCertificateEventsRequestDTOBuilder;
import se.inera.intyg.webcert.web.csintegration.unit.CertificateServiceUnitDTO;
import se.inera.intyg.webcert.web.csintegration.user.CertificateServiceUserDTO;

@JsonDeserialize(builder = GetCertificateEventsRequestDTOBuilder.class)
@Value
@Builder
public class GetCertificateEventsRequestDTO {

CertificateServiceUserDTO user;
CertificateServiceUnitDTO unit;
CertificateServiceUnitDTO careUnit;
CertificateServiceUnitDTO careProvider;

@JsonPOJOBuilder(withPrefix = "")
public static class GetCertificateEventsRequestDTOBuilder {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2024 Inera AB (http://www.inera.se)
*
* This file is part of sklintyg (https://github.com/sklintyg).
*
* sklintyg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sklintyg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package se.inera.intyg.webcert.web.csintegration.integration.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import lombok.Builder;
import lombok.Value;
import se.inera.intyg.common.support.modules.support.facade.dto.CertificateEventDTO;
import se.inera.intyg.webcert.web.csintegration.integration.dto.GetCertificateEventsResponseDTO.GetCertificateEventsResponseDTOBuilder;

@JsonDeserialize(builder = GetCertificateEventsResponseDTOBuilder.class)
@Value
@Builder
public class GetCertificateEventsResponseDTO {

CertificateEventDTO[] events;

@JsonPOJOBuilder(withPrefix = "")
public static class GetCertificateEventsResponseDTOBuilder {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import se.inera.intyg.webcert.web.web.controller.api.dto.Relations;
import se.inera.intyg.webcert.web.web.controller.api.dto.Relations.FrontendRelations;

@Service
@Service("getCertificateEventsFromWebcert")
public class GetCertificateEventsFacadeServiceImpl implements GetCertificateEventsFacadeService {

private static final Logger LOG = LoggerFactory.getLogger(GetCertificateEventsFacadeServiceImpl.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public class CertificateController {
@Autowired
private ReadyForSignFacadeService readyForSignFacadeService;
@Autowired
@Qualifier("getCertificateEventsAggregator")
private GetCertificateEventsFacadeService getCertificateEventsFacadeService;
@Autowired
private GetCertificateResourceLinks getCertificateResourceLinks;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2024 Inera AB (http://www.inera.se)
*
* This file is part of sklintyg (https://github.com/sklintyg).
*
* sklintyg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sklintyg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package se.inera.intyg.webcert.web.csintegration.aggregate;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import se.inera.intyg.common.support.modules.support.facade.dto.CertificateEventDTO;
import se.inera.intyg.webcert.web.csintegration.util.CertificateServiceProfile;
import se.inera.intyg.webcert.web.service.facade.GetCertificateEventsFacadeService;

class GetCertificateEventsAggregatorTest {

private static final String ID = "ID";
private static final CertificateEventDTO[] EVENTS = {new CertificateEventDTO()};

GetCertificateEventsFacadeService getCertificateEventsFromWC;
GetCertificateEventsFacadeService getCertificateEventsFromCS;
CertificateServiceProfile certificateServiceProfile;
GetCertificateEventsFacadeService aggregator;

@BeforeEach
void setup() {
getCertificateEventsFromWC = mock(GetCertificateEventsFacadeService.class);
getCertificateEventsFromCS = mock(GetCertificateEventsFacadeService.class);
certificateServiceProfile = mock(CertificateServiceProfile.class);

aggregator = new GetCertificateEventsAggregator(
getCertificateEventsFromWC,
getCertificateEventsFromCS,
certificateServiceProfile);
}

@Test
void shouldForwardFromWebcertIfProfileIsInactive() {
aggregator.getCertificateEvents(ID);

Mockito.verify(getCertificateEventsFromWC).getCertificateEvents(ID);
}

@Nested
class ActiveProfile {

@BeforeEach
void setup() {
when(certificateServiceProfile.active())
.thenReturn(true);
}

@Test
void shouldForwardFromCSIfProfileIsActiveAndCertificateExistsInCS() {
when(getCertificateEventsFromCS.getCertificateEvents(ID))
.thenReturn(EVENTS);
aggregator.getCertificateEvents(ID);

Mockito.verify(getCertificateEventsFromWC, times(0)).getCertificateEvents(ID);
}

@Test
void shouldForwardFromWCIfProfileIsInactiveAndCertificateDoesNotExistInCS() {
aggregator.getCertificateEvents(ID);

Mockito.verify(getCertificateEventsFromWC, times(1)).getCertificateEvents(ID);
}
}
}
Loading
Loading