Skip to content

Commit

Permalink
INTYGFV-16770: Added aggregator for get certificate events (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhornfeldt authored Aug 20, 2024
1 parent eafa8b8 commit 2002c73
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 1 deletion.
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 @@ -43,6 +43,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 @@ -347,6 +348,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 @@ -34,6 +34,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 @@ -58,6 +59,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 @@ -729,6 +732,18 @@ 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().toArray(CertificateEventDTO[]::new);
}

public Map<String, StatisticsForUnitDTO> getStatistics(UnitStatisticsRequestDTO request) {
final var url = baseUrl + UNIT_ENDPOINT_URL + "/certificates/statistics";

Expand Down
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,41 @@
/*
* 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 java.util.List;
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 {

List<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

0 comments on commit 2002c73

Please sign in to comment.