Skip to content

Commit

Permalink
Add more unit tests for Optional support
Browse files Browse the repository at this point in the history
  • Loading branch information
ralscha committed Oct 11, 2014
1 parent ecb8485 commit 9cdd741
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,10 @@ public static RemotingApi allApis(String namespace) {
remotingApi.addAction("remoteProviderOptional",
new Action("namedMethod4", Arrays.asList("bd")));

remotingApi.addSseProvider("remoteProviderOptional", "sse1");
remotingApi.addSseProvider("remoteProviderOptional", "sse2");
remotingApi.addSseProvider("remoteProviderOptional", "sse3");

return remotingApi;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.springframework.web.context.WebApplicationContext;

import ch.ralscha.extdirectspring.bean.ExtDirectPollResponse;
import ch.ralscha.extdirectspring.bean.SSEvent;
import ch.ralscha.extdirectspring.provider.RemoteProviderSimpleNamed.ResultObject;

@RunWith(SpringJUnit4ClassRunner.class)
Expand Down Expand Up @@ -498,4 +499,107 @@ public void testNamed4() {
ControllerUtil.sendAndReceiveNamed(mockMvc, null, null, "remoteProviderOptional",
"namedMethod4", "3.141:defaultHeaderValue", params);
}

@Test
public void testSse1() throws Exception {
Map<String, String> params = new HashMap<String, String>();
params.put("id", "1");
params.put("dummy", "sr");

List<SSEvent> events = ControllerUtil.performSseRequest(mockMvc,
"remoteProviderOptional", "sse1", params, null, null);
assertThat(events).hasSize(1);
SSEvent event = events.get(0);

assertThat(event.getEvent()).isNull();
assertThat(event.getComment()).isNull();
assertThat(event.getData()).startsWith("1:sr");
assertThat(event.getId()).isNull();
assertThat(event.getRetry()).isNull();

params = new HashMap<String, String>();
params.put("id", "2");

events = ControllerUtil.performSseRequest(mockMvc, "remoteProviderOptional",
"sse1", params, null, null);
assertThat(events).hasSize(1);
event = events.get(0);

assertThat(event.getEvent()).isNull();
assertThat(event.getComment()).isNull();
assertThat(event.getData()).startsWith("2:default_value");
assertThat(event.getId()).isNull();
assertThat(event.getRetry()).isNull();

events = ControllerUtil.performSseRequest(mockMvc, "remoteProviderOptional",
"sse1", null, null, null);
assertThat(events).hasSize(1);
event = events.get(0);

assertThat(event.getEvent()).isNull();
assertThat(event.getComment()).isNull();
assertThat(event.getData()).startsWith("-1:default_value");
assertThat(event.getId()).isNull();
assertThat(event.getRetry()).isNull();
}

@Test
public void testSse2() throws Exception {
List<Cookie> cookies = new ArrayList<Cookie>();
cookies.add(new Cookie("cookie", "cookieValue"));
Map<String, String> params = new HashMap<String, String>();
params.put("id", "11");

List<SSEvent> events = ControllerUtil.performSseRequest(mockMvc,
"remoteProviderOptional", "sse2", params, null, cookies);
assertThat(events).hasSize(1);
SSEvent event = events.get(0);

assertThat(event.getEvent()).isNull();
assertThat(event.getComment()).isNull();
assertThat(event.getData()).isEqualTo("11;cookieValue");
assertThat(event.getId()).isEqualTo("2");
assertThat(event.getRetry()).isNull();

events = ControllerUtil.performSseRequest(mockMvc, "remoteProviderOptional",
"sse2", params, null, null);
assertThat(events).hasSize(1);
event = events.get(0);

assertThat(event.getEvent()).isNull();
assertThat(event.getComment()).isNull();
assertThat(event.getData()).isEqualTo("11;defaultCookieValue");
assertThat(event.getId()).isEqualTo("2");
assertThat(event.getRetry()).isNull();
}

@Test
public void testSse3() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.add("requestHeader", "requestHeader");
Map<String, String> params = new HashMap<String, String>();
params.put("id", "12");

List<SSEvent> events = ControllerUtil.performSseRequest(mockMvc,
"remoteProviderOptional", "sse3", params, headers, null);
assertThat(events).hasSize(1);
SSEvent event = events.get(0);

assertThat(event.getEvent()).isNull();
assertThat(event.getComment()).isNull();
assertThat(event.getData()).isEqualTo("12;requestHeader");
assertThat(event.getId()).isEqualTo("3");
assertThat(event.getRetry()).isNull();

events = ControllerUtil.performSseRequest(mockMvc, "remoteProviderOptional",
"sse3", params, null, null);
assertThat(events).hasSize(1);
event = events.get(0);

assertThat(event.getEvent()).isNull();
assertThat(event.getComment()).isNull();
assertThat(event.getData()).isEqualTo("12;defaultRequestHeaderValue");
assertThat(event.getId()).isEqualTo("3");
assertThat(event.getRetry()).isNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import ch.ralscha.extdirectspring.annotation.ExtDirectMethod;
import ch.ralscha.extdirectspring.annotation.ExtDirectMethodType;
import ch.ralscha.extdirectspring.bean.SSEvent;
import ch.ralscha.extdirectspring.provider.RemoteProviderSimpleNamed.ResultObject;

@Service
Expand Down Expand Up @@ -239,4 +240,30 @@ public Optional<String> namedMethod4(
return Optional.of(bd.orElse(new BigDecimal("3.141")) + ":"
+ header.orElse("defaultHeaderValue"));
}

@ExtDirectMethod(value = ExtDirectMethodType.SSE, group = "optional")
public Optional<String> sse1(@RequestParam(value = "id") Optional<Integer> id,
Optional<String> dummy) {
return Optional.of(id.orElse(-1) + ":" + dummy.orElse("default_value"));
}

@ExtDirectMethod(value = ExtDirectMethodType.SSE, group = "optional")
public SSEvent sse2(@RequestParam(value = "id") Optional<Integer> id,
@CookieValue Optional<String> cookie) {
SSEvent event = new SSEvent();
event.setId("2");
event.setData(id.orElse(-1) + ";" + cookie.orElse("defaultCookieValue"));
return event;
}

@ExtDirectMethod(value = ExtDirectMethodType.SSE, group = "optional")
public SSEvent sse3(@RequestParam(value = "id") Optional<Integer> id,
@RequestHeader Optional<String> requestHeader) {
SSEvent event = new SSEvent();
event.setId("3");
event.setData(id.orElse(-1) + ";"
+ requestHeader.orElse("defaultRequestHeaderValue"));
return event;
}

}

0 comments on commit 9cdd741

Please sign in to comment.