From 9cdd7414f635523ec03b307a00eaabe447db6b2c Mon Sep 17 00:00:00 2001 From: Ralph Schaer Date: Sat, 11 Oct 2014 20:58:15 +0200 Subject: [PATCH] Add more unit tests for Optional support --- .../controller/ApiControllerTest.java | 4 + .../RouterControllerOptionalTest.java | 104 ++++++++++++++++++ .../provider/RemoteProviderOptional.java | 27 +++++ 3 files changed, 135 insertions(+) diff --git a/src/test/java/ch/ralscha/extdirectspring/controller/ApiControllerTest.java b/src/test/java/ch/ralscha/extdirectspring/controller/ApiControllerTest.java index 4af952b0e..4e799df6f 100644 --- a/src/test/java/ch/ralscha/extdirectspring/controller/ApiControllerTest.java +++ b/src/test/java/ch/ralscha/extdirectspring/controller/ApiControllerTest.java @@ -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; } diff --git a/src/test/java/ch/ralscha/extdirectspring/controller/RouterControllerOptionalTest.java b/src/test/java/ch/ralscha/extdirectspring/controller/RouterControllerOptionalTest.java index e51763ec9..da2445525 100644 --- a/src/test/java/ch/ralscha/extdirectspring/controller/RouterControllerOptionalTest.java +++ b/src/test/java/ch/ralscha/extdirectspring/controller/RouterControllerOptionalTest.java @@ -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) @@ -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 params = new HashMap(); + params.put("id", "1"); + params.put("dummy", "sr"); + + List 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(); + 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 cookies = new ArrayList(); + cookies.add(new Cookie("cookie", "cookieValue")); + Map params = new HashMap(); + params.put("id", "11"); + + List 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 params = new HashMap(); + params.put("id", "12"); + + List 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(); + } } diff --git a/src/test/java/ch/ralscha/extdirectspring/provider/RemoteProviderOptional.java b/src/test/java/ch/ralscha/extdirectspring/provider/RemoteProviderOptional.java index 383dd6b12..4b27d98f2 100644 --- a/src/test/java/ch/ralscha/extdirectspring/provider/RemoteProviderOptional.java +++ b/src/test/java/ch/ralscha/extdirectspring/provider/RemoteProviderOptional.java @@ -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 @@ -239,4 +240,30 @@ public Optional namedMethod4( return Optional.of(bd.orElse(new BigDecimal("3.141")) + ":" + header.orElse("defaultHeaderValue")); } + + @ExtDirectMethod(value = ExtDirectMethodType.SSE, group = "optional") + public Optional sse1(@RequestParam(value = "id") Optional id, + Optional dummy) { + return Optional.of(id.orElse(-1) + ":" + dummy.orElse("default_value")); + } + + @ExtDirectMethod(value = ExtDirectMethodType.SSE, group = "optional") + public SSEvent sse2(@RequestParam(value = "id") Optional id, + @CookieValue Optional 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 id, + @RequestHeader Optional requestHeader) { + SSEvent event = new SSEvent(); + event.setId("3"); + event.setData(id.orElse(-1) + ";" + + requestHeader.orElse("defaultRequestHeaderValue")); + return event; + } + }