Skip to content

Commit

Permalink
Added specifications for new functionality.
Browse files Browse the repository at this point in the history
Removed tracing.
  • Loading branch information
Asgeir Nilsen committed Sep 30, 2018
1 parent c9cf3a5 commit d4032ca
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 96 deletions.
11 changes: 2 additions & 9 deletions src/main/java/no/fint/provider/events/admin/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
Expand All @@ -39,12 +36,8 @@ public class AdminService {
public void refreshAssets() {
if (StringUtils.isEmpty(assetsEndpoint))
return;
try {
validAssets = restTemplate.getForObject(assetsEndpoint, String[].class);
log.info("Valid assets: {}", Arrays.toString(validAssets));
} catch (RestClientException e) {
log.info("Unable to fetch valid assets from {}: {}", assetsEndpoint, e);
}
validAssets = restTemplate.getForObject(assetsEndpoint, String[].class);
log.info("Valid assets: {}", Arrays.toString(validAssets));
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package no.fint.provider.events.response;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import no.fint.audit.FintAuditService;
import no.fint.event.model.Event;
Expand All @@ -11,26 +9,15 @@
import no.fint.provider.events.eventstate.EventStateService;
import no.fint.provider.events.exceptions.UnknownEventException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class ResponseService {

@Value("${fint.provider.tracing:false}")
private boolean tracing;

@Autowired
private EventStateService eventStateService;

Expand All @@ -40,39 +27,10 @@ public class ResponseService {
@Autowired
private FintEvents fintEvents;

private Path traceFile;
private ObjectMapper objectMapper;

@PostConstruct
public void init() throws IOException {
if (tracing) {
traceFile = Files.createTempFile("response", ".json");
Files.write(traceFile, "[\n{}".getBytes());
objectMapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
log.info("Tracing response events to {}", traceFile.toAbsolutePath());
}
}

@PreDestroy
public void shutdown() throws IOException {
if (tracing)
try (OutputStream os = Files.newOutputStream(traceFile, StandardOpenOption.APPEND, StandardOpenOption.SYNC)) {
os.write("\n]\n".getBytes());
}
}

public void handleAdapterResponse(Event event) {
log.debug("{}: Response for {} from {} status {} with {} elements.",
event.getCorrId(), event.getAction(), event.getOrgId(), event.getStatus(),
Optional.ofNullable(event.getData()).map(List::size).orElse(0));
if (tracing) {
try (OutputStream os = Files.newOutputStream(traceFile, StandardOpenOption.APPEND, StandardOpenOption.SYNC)){
os.write(",\n".getBytes());
objectMapper.writeValue(os, event);
} catch (IOException e) {
log.info("Unable to trace event", e);
}
}
if (event.isHealthCheck()) {
event.setStatus(Status.UPSTREAM_QUEUE);
fintEvents.sendUpstream(event);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package no.fint.provider.events.subscriber;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import no.fint.audit.FintAuditService;
import no.fint.event.model.Event;
Expand All @@ -14,24 +12,12 @@
import no.fint.provider.events.eventstate.EventStateService;
import no.fint.provider.events.sse.SseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

@Slf4j
@Component
public class DownstreamSubscriber implements FintEventListener {

@Value("${fint.provider.tracing:false}")
private boolean tracing;

@Autowired
private SseService sseService;

Expand All @@ -44,38 +30,9 @@ public class DownstreamSubscriber implements FintEventListener {
@Autowired
private ProviderProps providerProps;

private Path traceFile;
private ObjectMapper objectMapper;

@PostConstruct
public void init() throws IOException {
if (tracing) {
traceFile = Files.createTempFile("downstream", ".json");
Files.write(traceFile, "[\n{}".getBytes());
objectMapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
log.info("Tracing downstream events to {}", traceFile.toAbsolutePath());
}
}

@PreDestroy
public void shutdown() throws IOException {
if (tracing)
try (OutputStream os = Files.newOutputStream(traceFile, StandardOpenOption.APPEND, StandardOpenOption.SYNC)) {
os.write("\n]\n".getBytes());
}
}

@Override
public void accept(Event event) {
log.debug("Event received: {}", event);
if (tracing) {
try (OutputStream os = Files.newOutputStream(traceFile, StandardOpenOption.APPEND, StandardOpenOption.SYNC)){
os.write(",\n".getBytes());
objectMapper.writeValue(os, event);
} catch (IOException e) {
log.info("Unable to trace event", e);
}
}
if (event.isHealthCheck()) {
event.addObject(new Health(Constants.COMPONENT, HealthStatus.RECEIVED_IN_PROVIDER_FROM_CONSUMER));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,20 @@ class AdminControllerSpec extends MockMvcSpecification {
then:
response.andExpect(status().isNotFound())
}

def "Reject registering unknown orgId"() {
when:
def response = mockMvc.perform(post('/admin/orgIds/invalid.org').header(HeaderConstants.CLIENT, 'spock'))

then:
response.andExpect(status().is4xxClientError())
1 * adminService.register('invalid.org', 'spock') >> false

when:
def response2 = mockMvc.perform(post('/admin/orgIds/valid.org').header(HeaderConstants.CLIENT, 'spock'))

then:
response2.andExpect(status().is2xxSuccessful())
1 * adminService.register('valid.org', 'spock') >> true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package no.fint.provider.events.admin

import no.fint.event.model.DefaultActions
import no.fint.event.model.Event
import no.fint.events.FintEvents
import spock.lang.Specification

class AdminDownstreamSubscriberSpec extends Specification {
private AdminService adminService
private FintEvents fintEvents
private AdminDownstreamSubscriber subscriber

void setup() {
adminService = Mock(AdminService)
subscriber = new AdminDownstreamSubscriber(adminService: adminService)
adminService = Mock()
fintEvents = Mock()
subscriber = new AdminDownstreamSubscriber(adminService: adminService, fintEvents: fintEvents)
subscriber.init()
}

def "Receive register orgId"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.fint.provider.events.admin

import no.fint.event.model.Event
import no.fint.events.FintEvents
import org.springframework.web.client.RestTemplate
import spock.lang.Specification

class AdminServiceSpec extends Specification {
Expand Down Expand Up @@ -53,4 +54,47 @@ class AdminServiceSpec extends Specification {
then:
orgIds.size() == 2
}

def "Accept valid orgID and reject invalid orgID"() {
given:
def fintEvents = Mock(FintEvents)
def adminService = new AdminService(validAssets: [ 'valid.org'], fintEvents: fintEvents)

when:
def valid = adminService.register('valid.org', 'spock')

then:
valid
1 * fintEvents.sendUpstream(_ as Event)

when:
valid = adminService.register('invalid.org', 'spock')

then:
!valid
0 * fintEvents.sendUpstream(_ as Event)
}

def "Refresh works if assets endpoint not configured"() {
given:
def adminService = new AdminService()

when:
adminService.refreshAssets()

then:
noExceptionThrown()
}

def "Able to refresh assets using assets endpoint"() {
given:
def restTemplate = Mock(RestTemplate)
def adminService = new AdminService(assetsEndpoint: 'http://fake.org', restTemplate: restTemplate)

when:
adminService.refreshAssets()

then:
1 * restTemplate.getForObject('http://fake.org', String[]) >> [ 'valid.org', 'valid.com' ].toArray(new String[2])
}
}

0 comments on commit d4032ca

Please sign in to comment.