From f9db57a5931e3143e908f2080b1dfa76f6678ef2 Mon Sep 17 00:00:00 2001 From: Pyry Koivisto Date: Fri, 18 Oct 2024 14:44:39 +0300 Subject: [PATCH] VKT(Backend): Try to make mock ONR API responses more varied between JVM executions and simultaneously more consistently during the same execution --- .../onr/mock/MockOnrOperationApiImpl.java | 16 ++++++++++++++-- .../service/onr/mock/PersonalDataFactory.java | 8 +++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/backend/vkt/src/main/java/fi/oph/vkt/service/onr/mock/MockOnrOperationApiImpl.java b/backend/vkt/src/main/java/fi/oph/vkt/service/onr/mock/MockOnrOperationApiImpl.java index de4a59771..830b6e81e 100644 --- a/backend/vkt/src/main/java/fi/oph/vkt/service/onr/mock/MockOnrOperationApiImpl.java +++ b/backend/vkt/src/main/java/fi/oph/vkt/service/onr/mock/MockOnrOperationApiImpl.java @@ -2,6 +2,7 @@ import fi.oph.vkt.service.onr.OnrOperationApi; import fi.oph.vkt.service.onr.PersonalData; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -9,9 +10,20 @@ public class MockOnrOperationApiImpl implements OnrOperationApi { + // Cache personal data in a cache to try and ensure we return the same data per oid + // at least during the lifetime of the current JVM process + final Map personalDataCache = new HashMap<>(); + final PersonalDataFactory personalDataFactory = new PersonalDataFactory(); + @Override public Map fetchPersonalDatas(final List onrIds) { - final PersonalDataFactory personalDataFactory = new PersonalDataFactory(); - return onrIds.stream().collect(Collectors.toMap(Function.identity(), personalDataFactory::create)); + HashMap datas = new HashMap<>(); + for (String onrId : onrIds) { + if (!personalDataCache.containsKey(onrId)) { + personalDataCache.put(onrId, personalDataFactory.create(onrId)); + } + datas.put(onrId, personalDataCache.get(onrId)); + } + return datas; } } diff --git a/backend/vkt/src/main/java/fi/oph/vkt/service/onr/mock/PersonalDataFactory.java b/backend/vkt/src/main/java/fi/oph/vkt/service/onr/mock/PersonalDataFactory.java index 87bddb081..5beef2fc0 100644 --- a/backend/vkt/src/main/java/fi/oph/vkt/service/onr/mock/PersonalDataFactory.java +++ b/backend/vkt/src/main/java/fi/oph/vkt/service/onr/mock/PersonalDataFactory.java @@ -2,15 +2,13 @@ import fi.oph.vkt.service.onr.PersonalData; import fi.oph.vkt.util.CyclicIterable; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class PersonalDataFactory { - private final AtomicInteger counter = new AtomicInteger(); + private final Random rng = new Random(); + private final AtomicInteger counter = new AtomicInteger(rng.nextInt()); public PersonalData create(final String onrId) { final int counterValue = counter.incrementAndGet();