Skip to content

Commit

Permalink
Improved error handling and added some more tests (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornandre authored Aug 15, 2023
1 parent 8e54a64 commit 42d7847
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/java/no/ssb/dlp/pseudo/service/sid/SidMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public Object map(Object data) {
}
}
}
SidInfo result = bulkRequest.get(fnr).awaitResult().get(fnr);
SidInfo result = bulkRequest.get(fnr).awaitResult()
.orElseThrow(() -> new RuntimeException("SID service did not respond")).get(fnr);
if (result == null || result.getSnr() == null) {
log.warn("No SID-mapping found for fnr starting with {}", Strings.padEnd(fnr, 6, ' ').substring(0, 6));
return fnr;
Expand Down Expand Up @@ -140,8 +141,8 @@ public void onComplete() {
log.info("Thread completed after {} seconds", stopwatch.stop().elapsed(TimeUnit.SECONDS));
}

public T awaitResult() {
return await().result;
public Optional<T> awaitResult() {
return Optional.ofNullable(await().result);
}

private ObservableSubscriber<T> await() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package no.ssb.dlp.pseudo.service.sid;

import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.test.annotation.MockBean;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static org.mockito.Mockito.*;

@MicronautTest
public class ExternalSidServiceTest {

@Inject
SidService sidService;
@Inject
SidClient sidClient;

@Test
public void testInvokeSingleFnr() {
// sidService should call our sidClient mock
when(sidClient.lookup(any(SidRequest.class))).thenReturn(Publishers.just(
SidInfo.builder().fnr("11854898347").snr("0001ha3").build())
);
sidService.lookupFnr("11854898347", Optional.ofNullable(null));

verify(sidClient, times(1)).lookup(any(SidRequest.class));
}

@Test
public void testInvokeMultiFnr() {
// sidService should call our sidClient mock
when(sidClient.lookup(any(MultiSidRequest.class))).thenReturn(Publishers.just(
MultiSidResponse.builder().mapping(MultiSidResponse.Mapping.builder()
.fnrList(Arrays.asList("11854898347"))
.fnr(Arrays.asList("11854898347"))
.snr(Arrays.asList("0001ha3"))
.build()).build())
);
sidService.lookupFnr(List.of("11854898347"), Optional.ofNullable(null));

verify(sidClient, times(1)).lookup(any(MultiSidRequest.class));
}
@MockBean(SidClient.class)
SidClient sidClient() {
return mock(SidClient.class);
}
}

0 comments on commit 42d7847

Please sign in to comment.