Clover icon

sunshower-sdk

  1. Project Clover database Sat May 12 2018 05:15:40 UTC
  2. Package io.sunshower.sdk.test

File ChannelEndpointTest.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
31
3
1
128
108
5
0.16
10.33
3
1.67

Classes

Class Line # Actions
ChannelEndpointTest 29 31 5
1.0100%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.sdk.test;
2   
3    import io.sunshower.sdk.channel.Message;
4    import io.sunshower.sdk.lang.IdentifierElement;
5    import io.sunshower.sdk.v1.core.auth.model.UsernamePasswordCredentialElement;
6    import io.sunshower.test.ws.Remote;
7    import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
8    import org.junit.jupiter.api.Test;
9   
10    import javax.inject.Inject;
11    import javax.ws.rs.client.WebTarget;
12    import javax.ws.rs.core.MediaType;
13    import javax.ws.rs.sse.SseEventSource;
14   
15    import java.util.ArrayList;
16    import java.util.Collections;
17    import java.util.List;
18    import java.util.Objects;
19    import java.util.concurrent.ExecutionException;
20    import java.util.concurrent.ExecutorService;
21    import java.util.concurrent.TimeUnit;
22    import java.util.concurrent.TimeoutException;
23    import java.util.concurrent.atomic.AtomicInteger;
24   
25    import static org.hamcrest.CoreMatchers.is;
26    import static org.junit.Assert.assertThat;
27    import static org.junit.Assert.assertTrue;
28   
 
29    class ChannelEndpointTest extends SdkTest {
30   
31    @Inject private WebTarget webTarget;
32   
33    @Inject private ExecutorService service;
34   
35    @Remote private ChannelEndpoint channelEndpoint;
36   
 
37  1 toggle @Test
38    void ensureChannelInterceptorProvidesCorrectMediaType() {
39  1 final ResteasyWebTarget target = (ResteasyWebTarget) webTarget;
40  1 String resp =
41    target
42    .path(ChannelEndpoint.class)
43    .path("mediatype")
44    .request(MediaType.APPLICATION_ATOM_XML)
45    .accept(MediaType.TEXT_PLAIN)
46    .buildGet()
47    .invoke(String.class);
48  1 assertThat(resp, is(MediaType.APPLICATION_ATOM_XML));
49    }
50   
 
51  1 toggle @Test
52    void ensureMessagesWorkCorrectly()
53    throws InterruptedException, TimeoutException, ExecutionException {
54  1 final ResteasyWebTarget target = (ResteasyWebTarget) webTarget;
55  1 IdentifierElement ticker =
56    target
57    .path(ChannelEndpoint.class)
58    .path("newMessages")
59    .request(MediaType.APPLICATION_JSON_TYPE)
60    .buildGet()
61    .invoke(IdentifierElement.class);
62   
63  1 ResteasyWebTarget channel =
64    target.path(ChannelEndpoint.class).path("" + ticker.getValue()).path("messages");
65   
66  1 SseEventSource source = SseEventSource.target(channel).build();
67  1 final List<Message> e = Collections.synchronizedList(new ArrayList<>());
68   
69  1 source.register(
70    c -> {
71  10 Message frap = c.readData(Message.class, MediaType.APPLICATION_JSON_TYPE);
72  10 e.add(frap);
73    });
74  1 source.open();
75   
76  1 target
77    .path(ChannelEndpoint.class)
78    .path("" + ticker.getValue())
79    .path("start")
80    .request(MediaType.APPLICATION_JSON_TYPE)
81    .buildGet()
82    .invoke();
83  6232562 while (e.size() < 10) {
84  6232561 Thread.yield();
85    }
86   
87  1 assertThat(e.stream().allMatch(Objects::nonNull), is(true));
88  1 assertTrue(e.size() >= 10);
89    }
90   
 
91  1 toggle @Test
92    void ensureStartingAndStoppingWork() throws InterruptedException {
93  1 final ResteasyWebTarget target = (ResteasyWebTarget) webTarget;
94  1 IdentifierElement ticker =
95    target
96    .path(ChannelEndpoint.class)
97    .path("ticker")
98    .request(MediaType.APPLICATION_JSON_TYPE)
99    .buildGet()
100    .invoke(IdentifierElement.class);
101   
102  1 ResteasyWebTarget channel =
103    target.path(ChannelEndpoint.class).path("" + ticker.getValue()).path("channel");
104   
105  1 SseEventSource source = SseEventSource.target(channel).build();
106  1 final List<Frap> e = new ArrayList<>();
107  1 source.register(
108    c -> {
109  46 Frap frap = c.readData(Frap.class, MediaType.APPLICATION_JSON_TYPE);
110  46 e.add(frap);
111    });
112  1 target
113    .path(ChannelEndpoint.class)
114    .path("" + ticker.getValue())
115    .path("start")
116    .request(MediaType.APPLICATION_JSON_TYPE)
117    .buildGet()
118    .invoke();
119   
120  1 source.open();
121  1616796 while (e.size() < 10) {
122  1616795 Thread.yield();
123    }
124   
125  1 assertThat(e.stream().allMatch(Objects::nonNull), is(true));
126  1 assertTrue(e.size() >= 10);
127    }
128    }