如何用Spring测试服务器发送的事件?

时间:2016-02-19 07:24:46

标签: spring server-sent-events mockmvc

我已经使用返回SseEmitter的方法实现了一个控制器,现在我想测试它。到目前为止我能找到的唯一方法是:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {SsePaymentReceivedController.class, AutomatBackendContextInitializer.class, EventBusImpl.class})
@WebAppConfiguration
public class SsePaymentReceivedControllerIntegrationTest {

@Inject
WebApplicationContext context;
@Inject
SsePaymentReceivedController sseCoinController;
@Inject
EventBusImpl eventBus;

MockMvc mockMvc;

@Before
public void setUpMockMvc() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}

@Test
public void subscriptionToSseChannelIsFine() throws Exception {
    MvcResult result = mockMvc.perform(get("/sse/payment"))
            .andExpect(status().isOk())
            .andReturn();
    eventBus.fireNotification(new PaymentReceivedNotification("50", Currency.EURO));
    LinkedHashSet<ResponseBodyEmitter.DataWithMediaType> emitters =
            (LinkedHashSet<ResponseBodyEmitter.DataWithMediaType>)Whitebox.getInternalState(((SseEmitter)result.getModelAndView().getModel().get("sseEmitter")), "earlySendAttempts");
    final Iterator<ResponseBodyEmitter.DataWithMediaType> iterator = emitters.iterator();

    ResponseBodyEmitter.DataWithMediaType dataField = iterator.next();
    assertEquals("data:", dataField.getData());

    ResponseBodyEmitter.DataWithMediaType valueField = iterator.next();
    assertEquals("{\"remainingAmount\":\"50\",\"currency\":\"EURO\"}", valueField.getData());

    ResponseBodyEmitter.DataWithMediaType lastField = iterator.next();
    assertEquals("\n\n", lastField.getData());
}
}

必须有一种方法比检查退回模型的内部更好,我正在寻找 - 任何想法?

2 个答案:

答案 0 :(得分:0)

我找到的最佳选择是回复get the content

String event = result.getResponse().getContentAsString()
// remove data: and \n\n
String json = s1.substring(5, s1.length() - 2);
new JsonPathExpectationsHelper("$.remainingAmount").assertValue(json, "50");
new JsonPathExpectationsHelper("$.currency").assertValue(json, "EURO");

答案 1 :(得分:0)

一个好主意可能是使用SSE客户端。您可以将JAX WS RS Client与Jersey EventSource一起使用来实现该目标。

使用javax.ws.rs-api 2.0.1jersey-media-sse 2.25.1,打开SSE连接的方法如下:

Client client = ClientBuilder
        .newBuilder()
        .register(SseFeature.class)
        .build();

EventSource eventSource = EventSource
        .target(client.target("http://localhost:8080/feed/123456"))
        .reconnectingEvery(1, TimeUnit.SECONDS)
        .build();

EventListener listener = inboundEvent -> {

    log.info("Event received: id -> {}, name -> {}, data -> {}", inboundEvent.getId(),
             inboundEvent.getName(), inboundEvent.readData(String.class));

    // Process events ...
};
eventSource.register(listener);
eventSource.open();

因此,如果您想创建用于测试的工具,则可以编写一个类似于以下内容的辅助类:

package org.demo.service.sse;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

import org.glassfish.jersey.media.sse.EventListener;
import org.glassfish.jersey.media.sse.EventSource;
import org.glassfish.jersey.media.sse.InboundEvent;
import org.glassfish.jersey.media.sse.SseFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SseTestClient {

    private static final Logger log = LoggerFactory.getLogger(SseTestClient.class);

    private final String targetUrl;
    private final List<InboundEvent> receivedEvents = new ArrayList<>();

    private String clientName = "SSE client";
    private EventSource eventSource;
    private int currentEventPos = 0;

    public SseTestClient( String targetUrl ) {

        this.targetUrl = targetUrl;
    }

    public SseTestClient setClientName( String clientName ) {

        this.clientName = clientName;
        return this;
    }

    public SseTestClient connectFeed() {

        log.info("[{}] Initializing EventSource...", clientName);

        // Create SSE client and server

        Client client = ClientBuilder
                .newBuilder()
                .register(SseFeature.class)
                .build();

        eventSource = EventSource
                .target(client.target(targetUrl))
                .reconnectingEvery(300, TimeUnit.SECONDS) // Large number so that any disconnection will break tests
                .build();

        EventListener listener = inboundEvent -> {

            log.info("[{}] Event received: name -> {}, data -> {}",
                     clientName, inboundEvent.getName(), inboundEvent.readData(String.class));

            receivedEvents.add(inboundEvent);
        };
        eventSource.register(listener);
        eventSource.open();

        log.info("[{}] EventSource connection opened", clientName);

        return this;
    }

    public int getTotalEventCount() {

        return receivedEvents.size();
    }

    public int getNewEventCount() {

        return receivedEvents.size() - currentEventPos;
    }

    public boolean hasNewEvent() {

        return currentEventPos < receivedEvents.size();
    }

    public InboundEvent getNextEvent() {

        if (currentEventPos >= receivedEvents.size()) {
            return null;
        }
        InboundEvent currentEvent = receivedEvents.get(currentEventPos);
        ++currentEventPos;
        return currentEvent;
    }

    public SseTestClient closeFeed() {

        if (eventSource != null) {
            eventSource.close();
        }
        return this;
    }
}

这使得编写测试变得更加容易:

SseTestClient sseClient = new SseTestClient("http://localhost:8080/feed/123456").connectFeed();

Thread.sleep(1000);

assertTrue(sseClient.hasNewEvent());
assertEquals(2, sseClient.getNewEventCount());
// ...

希望有帮助!