From 4b3d14bb7c4d23cf0cedeb587f27bf53c6541cb4 Mon Sep 17 00:00:00 2001 From: Sander Nilsen Rysjedal Date: Tue, 17 Dec 2024 10:03:38 +0100 Subject: [PATCH] returns exception if you put resource that does not excist in database --- src/main/java/no/fintlabs/NotInDataBaseException.java | 7 +++++++ .../personopplysning/PersonopplysningRepository.java | 7 ++++++- .../event/PersonopplysningEventPublisher.java | 2 +- src/main/resources/application-db.yaml | 3 ++- src/main/resources/application-local.yml | 8 ++++++-- 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/main/java/no/fintlabs/NotInDataBaseException.java diff --git a/src/main/java/no/fintlabs/NotInDataBaseException.java b/src/main/java/no/fintlabs/NotInDataBaseException.java new file mode 100644 index 0000000..dd6d9bd --- /dev/null +++ b/src/main/java/no/fintlabs/NotInDataBaseException.java @@ -0,0 +1,7 @@ +package no.fintlabs; + +public class NotInDataBaseException extends RuntimeException { + public NotInDataBaseException(String message) { + super(message); + } +} diff --git a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRepository.java b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRepository.java index a41c86d..0af2c8d 100644 --- a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRepository.java +++ b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRepository.java @@ -3,8 +3,10 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import no.fint.model.resource.personvern.kodeverk.PersonopplysningResource; +import no.fintlabs.NotInDataBaseException; import no.fintlabs.adapter.events.WriteableResourceRepository; import no.fintlabs.adapter.models.event.RequestFintEvent; +import no.fintlabs.adapter.operation.OperationType; import no.fintlabs.model.personopplysning.model.PersonopplysningEntity; import org.springframework.stereotype.Repository; @@ -32,10 +34,13 @@ public List getUpdatedResources() { @Override public PersonopplysningResource saveResources(PersonopplysningResource personopplysningResource, RequestFintEvent requestFintEvent) { + if (requestFintEvent.getOperationType().equals(OperationType.UPDATE) && !personopplysningJpaRepository.existsById(personopplysningResource.getSystemId().getIdentifikatorverdi())) { + throw new NotInDataBaseException("Can not update resource that is not in the database"); + } + PersonopplysningEntity personopplysningEntity = PersonopplysningMappingService.toEntity(personopplysningResource); log.info("Save personopplysning {}", personopplysningResource.getNavn()); personopplysningJpaRepository.save(personopplysningEntity); return personopplysningResource; } - } \ No newline at end of file diff --git a/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventPublisher.java b/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventPublisher.java index ee41a36..04fab1f 100644 --- a/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventPublisher.java +++ b/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventPublisher.java @@ -33,7 +33,7 @@ public void doCheckForNewEvents() { } @Override - protected void handleEvent(RequestFintEvent requestFintEvent, PersonopplysningResource personopplysningResource) { + public void handleEvent(RequestFintEvent requestFintEvent, PersonopplysningResource personopplysningResource) { ResponseFintEvent response = createResponse(requestFintEvent); response.setOperationType(requestFintEvent.getOperationType()); diff --git a/src/main/resources/application-db.yaml b/src/main/resources/application-db.yaml index 2a2a374..496ebb0 100644 --- a/src/main/resources/application-db.yaml +++ b/src/main/resources/application-db.yaml @@ -1,11 +1,12 @@ spring: jpa: properties: + hibernate: + dialect: org.hibernate.dialect.PostgreSQLDialect jdbc: time_zone: UTC lob: non_contextual_creation: true - dialect: org.hibernate.dialect.PostgreSQLDialect enable_lazy_load_no_trans: true hibernate: ddl-auto: update diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 2b22e68..4889c24 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -1,5 +1,9 @@ fint: metamodell: https://beta.felleskomponent.no/fint/metamodell/klasse + database: + username: postgres + password: password + url: jdbc:postgresql://localhost:5436/fint-personvern-kodeverk-adapter adapter: id: https://vigoiks.no/fintlabs-no/personvern/kodeverk-henrik-test password: ${fint.adapter.password} @@ -24,9 +28,9 @@ fint: spring: datasource: - url: jdbc:postgresql://localhost:5432/fint-personvern-kodeverk-adapter + url: jdbc:postgresql://localhost:5436/fint-personvern-kodeverk-adapter username: postgres - password: postgres + password: password security: oauth2: client: