Skip to content

Commit

Permalink
VKT(Backend): Try to make mock ONR API responses more varied between …
Browse files Browse the repository at this point in the history
…JVM executions and simultaneously more consistently during the same execution
  • Loading branch information
pkoivisto committed Oct 18, 2024
1 parent f972485 commit f9db57a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

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;
import java.util.stream.Collectors;

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<String, PersonalData> personalDataCache = new HashMap<>();
final PersonalDataFactory personalDataFactory = new PersonalDataFactory();

@Override
public Map<String, PersonalData> fetchPersonalDatas(final List<String> onrIds) {
final PersonalDataFactory personalDataFactory = new PersonalDataFactory();
return onrIds.stream().collect(Collectors.toMap(Function.identity(), personalDataFactory::create));
HashMap<String, PersonalData> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f9db57a

Please sign in to comment.