Skip to content

Commit

Permalink
Add more Optional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ralscha committed Oct 11, 2014
1 parent 77b21e0 commit 4886c78
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,18 @@ public static RemotingApi allApis(String namespace) {
Boolean.FALSE));
remotingApi.addAction("remoteProviderOptional", new Action("method19", 0,
Boolean.FALSE));

remotingApi.addPollingProvider(new PollingProvider("remoteProviderOptional",
"opoll1", "opoll1"));
remotingApi.addPollingProvider(new PollingProvider("remoteProviderOptional",
"opoll2", "opoll2"));
remotingApi.addPollingProvider(new PollingProvider("remoteProviderOptional",
"opoll3", "opoll3"));
remotingApi.addPollingProvider(new PollingProvider("remoteProviderOptional",
"opoll4", "opoll4"));
remotingApi.addPollingProvider(new PollingProvider("remoteProviderOptional",
"opoll5", "opoll5"));

return remotingApi;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -42,6 +43,7 @@
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import ch.ralscha.extdirectspring.bean.ExtDirectPollResponse;
import ch.ralscha.extdirectspring.provider.RemoteProviderSimple.BusinessObject;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -277,4 +279,123 @@ public void testMethod19() {
"method19", Void.TYPE, (Object[]) null);
}

@Test
public void testPoll1a() throws Exception {
Map<String, String> params = new LinkedHashMap<String, String>();
params.put("id", "2");

ExtDirectPollResponse resp = ControllerUtil.performPollRequest(mockMvc,
"remoteProviderOptional", "opoll1", "opoll1", params, null);

assertThat(resp).isNotNull();
assertThat(resp.getType()).isEqualTo("event");
assertThat(resp.getName()).isEqualTo("opoll1");
assertThat(resp.getData()).isEqualTo("Result: 2");
assertThat(resp.getWhere()).isNull();
assertThat(resp.getMessage()).isNull();
}

@Test
public void testPoll1b() throws Exception {

ExtDirectPollResponse resp = ControllerUtil.performPollRequest(mockMvc,
"remoteProviderOptional", "opoll1", "opoll1", null, null);

assertThat(resp).isNotNull();
assertThat(resp.getType()).isEqualTo("exception");
assertThat(resp.getName()).isEqualTo("opoll1");
assertThat(resp.getData()).isNull();
assertThat(resp.getMessage()).isEqualTo("Server Error");
assertThat(resp.getWhere()).isNull();
}

@Test
public void testPoll2a() throws Exception {
Map<String, String> params = new LinkedHashMap<String, String>();
params.put("id", "7");

ExtDirectPollResponse resp = ControllerUtil.performPollRequest(mockMvc,
"remoteProviderOptional", "opoll2", "opoll2", params, null);
assertThat(resp).isNotNull();
assertThat(resp.getType()).isEqualTo("event");
assertThat(resp.getName()).isEqualTo("opoll2");
assertThat(resp.getData()).isEqualTo(Integer.valueOf(14));
assertThat(resp.getWhere()).isNull();
assertThat(resp.getMessage()).isNull();
}

@Test
public void testPoll2b() throws Exception {
ExtDirectPollResponse resp = ControllerUtil.performPollRequest(mockMvc,
"remoteProviderOptional", "opoll2", "opoll2", null, null, null, true);

assertThat(resp).isNotNull();
assertThat(resp.getType()).isEqualTo("event");
assertThat(resp.getName()).isEqualTo("opoll2");
assertThat(resp.getData()).isEqualTo(Integer.valueOf(4));
assertThat(resp.getWhere()).isNull();
assertThat(resp.getMessage()).isNull();
}

@Test
public void testPoll3a() throws Exception {
Map<String, String> params = new LinkedHashMap<String, String>();
params.put("id", "3");

ExtDirectPollResponse resp = ControllerUtil.performPollRequest(mockMvc,
"remoteProviderOptional", "opoll3", "opoll3", params, null);

assertThat(resp).isNotNull();
assertThat(resp.getType()).isEqualTo("event");
assertThat(resp.getName()).isEqualTo("opoll3");
assertThat(resp.getData()).isEqualTo(Integer.valueOf(6));
assertThat(resp.getWhere()).isNull();
assertThat(resp.getMessage()).isNull();
}

@Test
public void testPoll3b() throws Exception {
ExtDirectPollResponse resp = ControllerUtil.performPollRequest(mockMvc,
"remoteProviderOptional", "opoll3", "opoll3", null, null);

assertThat(resp).isNotNull();
assertThat(resp.getType()).isEqualTo("event");
assertThat(resp.getName()).isEqualTo("opoll3");
assertThat(resp.getData()).isNull();
assertThat(resp.getWhere()).isNull();
assertThat(resp.getMessage()).isNull();
}

@Test
public void testPoll4() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.add("header", "headerValue");

ExtDirectPollResponse resp = ControllerUtil.performPollRequest(mockMvc,
"remoteProviderOptional", "opoll4", "opoll4", null, headers);

assertThat(resp).isNotNull();
assertThat(resp.getType()).isEqualTo("event");
assertThat(resp.getName()).isEqualTo("opoll4");
assertThat(resp.getData()).isEqualTo("100;dummy;headerValue");
assertThat(resp.getWhere()).isNull();
assertThat(resp.getMessage()).isNull();
}

@Test
public void testPoll5() throws Exception {
List<Cookie> cookies = new ArrayList<Cookie>();
cookies.add(new Cookie("cookie", "cookieValue"));

ExtDirectPollResponse resp = ControllerUtil.performPollRequest(mockMvc,
"remoteProviderOptional", "opoll5", "opoll5", null, null, cookies);

assertThat(resp).isNotNull();
assertThat(resp.getType()).isEqualTo("event");
assertThat(resp.getName()).isEqualTo("opoll5");
assertThat(resp.getData()).isEqualTo("23;dummy;cookieValue");
assertThat(resp.getWhere()).isNull();
assertThat(resp.getMessage()).isNull();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@
*/
package ch.ralscha.extdirectspring.provider;

import static org.fest.assertions.api.Assertions.assertThat;

import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

import ch.ralscha.extdirectspring.annotation.ExtDirectMethod;
import ch.ralscha.extdirectspring.annotation.ExtDirectMethodType;

@Service
public class RemoteProviderOptional {
Expand Down Expand Up @@ -158,4 +165,46 @@ public String method19(@CookieValue Optional<String> stringCookie) {
return stringCookie.orElse(null);
}

@ExtDirectMethod(value = ExtDirectMethodType.POLL, event = "opoll1",
group = "optional")
public Optional<String> opoll1(Locale locale,
@RequestParam(value = "id") Optional<Integer> id) {
assertThat(locale).isEqualTo(Locale.ENGLISH);
return Optional.of("Result: " + id.get());
}

@ExtDirectMethod(value = ExtDirectMethodType.POLL, event = "opoll2",
synchronizeOnSession = true, group = "optional")
public int opoll2(@RequestParam(value = "id") Optional<Integer> id,
HttpServletRequest request) {
assertThat(request).isNotNull();
return id.orElse(2) * 2;
}

@ExtDirectMethod(value = ExtDirectMethodType.POLL, event = "opoll3",
group = "optional")
public Optional<Integer> opoll3(@RequestParam(value = "id") Optional<Integer> id,
Optional<String> dummy) {
assertThat(dummy.isPresent()).isFalse();
if (id.isPresent()) {
return Optional.of(id.get() * 2);
}
return Optional.empty();
}

@ExtDirectMethod(value = ExtDirectMethodType.POLL, group = "optional")
public Optional<String> opoll4(@RequestParam(value = "id") Optional<Integer> id,
Optional<String> dummy, @RequestHeader Optional<String> header) {
return Optional.of(id.orElse(100) + ";" + dummy.orElse("dummy") + ";"
+ header.orElse("header"));
}

@ExtDirectMethod(value = ExtDirectMethodType.POLL, group = "optional")
public Optional<String> opoll5(
@RequestParam(value = "id", required = false) Optional<Integer> id,
Optional<String> dummy, @CookieValue Optional<String> cookie) {
return Optional.of(id.orElse(23) + ";" + dummy.orElse("dummy") + ";"
+ cookie.orElse("cookie"));
}

}

0 comments on commit 4886c78

Please sign in to comment.