Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ralscha committed Oct 13, 2014
1 parent 2e0e83a commit 0f25df2
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,10 @@ public static RemotingApi allApis(String namespace) {
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("method9", 1,
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("method10", 1,
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("method11", 1,
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("methodFilter", 1,
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("methodMetadata", 1,
Expand Down Expand Up @@ -1080,6 +1084,11 @@ public static RemotingApi allApis(String namespace) {
remotingApi.addAction("remoteProviderOptional", new Action("storeRead2", 1,
Boolean.FALSE));

remotingApi.addAction("remoteProviderOptional", new Action("treeLoad1", 1,
Boolean.FALSE));
remotingApi.addAction("remoteProviderOptional", new Action("treeLoad2", 1,
Boolean.FALSE));

return remotingApi;
}

Expand Down Expand Up @@ -1155,6 +1164,10 @@ public static RemotingApi emptyGroupApis(String namespace) {
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("method9", 1,
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("method10", 1,
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("method11", 1,
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("methodFilter", 1,
Boolean.FALSE));
remotingApi.addAction("remoteProviderStoreRead", new Action("methodMetadata", 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import ch.ralscha.extdirectspring.bean.ExtDirectStoreResult;
import ch.ralscha.extdirectspring.bean.SSEvent;
import ch.ralscha.extdirectspring.provider.RemoteProviderSimpleNamed.ResultObject;
import ch.ralscha.extdirectspring.provider.RemoteProviderTreeLoad.Node;
import ch.ralscha.extdirectspring.provider.Row;

import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -690,4 +691,79 @@ public void testStoreRead2() {
}
}

@Test
public void testTreeLoad1() {
Map<String, Object> requestParameters = new LinkedHashMap<String, Object>();
requestParameters.put("node", "root");

List<Cookie> cookies = new ArrayList<Cookie>();
cookies.add(new Cookie("theCookie", "value"));

List<Node> nodes = (List<Node>) ControllerUtil.sendAndReceive(mockMvc, false,
null, cookies, "remoteProviderOptional", "treeLoad1", false,
new TypeReference<List<Node>>() {/* nothinghere */
}, requestParameters);

String appendix = ":defaultValue2;value;true;true;true;en";

assertThat(nodes).hasSize(5).containsSequence(
new Node("n1", "Node 1" + appendix, false),
new Node("n2", "Node 2" + appendix, false),
new Node("n3", "Node 3" + appendix, false),
new Node("n4", "Node 4" + appendix, false),
new Node("n5", "Node 5" + appendix, false));

requestParameters = new LinkedHashMap<String, Object>();
requestParameters.put("node", "n2");
requestParameters.put("foo", "f");

nodes = (List<Node>) ControllerUtil.sendAndReceive(mockMvc, false, null, cookies,
"remoteProviderOptional", "treeLoad1", false,
new TypeReference<List<Node>>() {/* nothinghere */
}, requestParameters);

appendix = ":f;value;true;true;true;en";

assertThat(nodes).hasSize(5).containsSequence(
new Node("id1", "Node 2.1" + appendix, true),
new Node("id2", "Node 2.2" + appendix, true),
new Node("id3", "Node 2.3" + appendix, true),
new Node("id4", "Node 2.4" + appendix, true),
new Node("id5", "Node 2.5" + appendix, true));
}

@Test
public void testTreeLoad2() {
HttpHeaders headers = new HttpHeaders();
headers.add("aHeader", "false");

Map<String, Object> requestParameters = new LinkedHashMap<String, Object>();
requestParameters.put("node", "root");

List<Node> nodes = (List<Node>) ControllerUtil.sendAndReceive(mockMvc, headers,
"remoteProviderOptional", "treeLoad2", new TypeReference<List<Node>>() {/* nothinghere */
}, requestParameters);

String appendix = ":false;true;true";

assertThat(nodes).hasSize(5).containsSequence(
new Node("n1", "Node 1" + appendix, false),
new Node("n2", "Node 2" + appendix, false),
new Node("n3", "Node 3" + appendix, false),
new Node("n4", "Node 4" + appendix, false),
new Node("n5", "Node 5" + appendix, false));

nodes = (List<Node>) ControllerUtil.sendAndReceive(mockMvc, (HttpHeaders) null,
"remoteProviderOptional", "treeLoad2", new TypeReference<List<Node>>() {/* nothinghere */
}, requestParameters);

appendix = ":true;true;true";
assertThat(nodes).hasSize(5).containsSequence(
new Node("n1", "Node 1" + appendix, false),
new Node("n2", "Node 2" + appendix, false),
new Node("n3", "Node 3" + appendix, false),
new Node("n4", "Node 4" + appendix, false),
new Node("n5", "Node 5" + appendix, false));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.Cookie;

import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
Expand Down Expand Up @@ -547,6 +550,89 @@ public void testMessageProperty() {

}

@Test
public void testRequestParam() {
Map<String, Object> readRequest = new HashMap<String, Object>();
readRequest.put("id", 10);
readRequest.put("query", "name");

ExtDirectStoreResult<Row> storeResponse = (ExtDirectStoreResult<Row>) ControllerUtil
.sendAndReceive(mockMvc, "remoteProviderStoreRead", "method10",
new TypeReference<ExtDirectStoreResult<Row>>() {
// nothing here
}, readRequest);

assertThat(storeResponse.getTotal()).isEqualTo(50L);
assertThat(storeResponse.getRecords()).hasSize(50);
int ix = 0;
for (Row row : storeResponse.getRecords()) {
assertThat(row.getName()).startsWith("name: " + ix + ":10;en");
ix += 2;
}

readRequest = new HashMap<String, Object>();
readRequest.put("query", "name");

storeResponse = (ExtDirectStoreResult<Row>) ControllerUtil.sendAndReceive(
mockMvc, "remoteProviderStoreRead", "method10",
new TypeReference<ExtDirectStoreResult<Row>>() {
// nothing here
}, readRequest);

assertThat(storeResponse.getTotal()).isEqualTo(50L);
assertThat(storeResponse.getRecords()).hasSize(50);
ix = 0;
for (Row row : storeResponse.getRecords()) {
assertThat(row.getName()).startsWith("name: " + ix + ":20;en");
ix += 2;
}
}

@Test
public void testCookieAndRequestHeader() {

HttpHeaders headers = new HttpHeaders();
headers.add("requestHeader", "rValue");

List<Cookie> cookies = new ArrayList<Cookie>();
cookies.add(new Cookie("cookie", "cValue"));

Map<String, Object> readRequest = new HashMap<String, Object>();
readRequest.put("query", "name");

ExtDirectStoreResult<Row> storeResponse = (ExtDirectStoreResult<Row>) ControllerUtil
.sendAndReceive(mockMvc, headers, cookies, "remoteProviderStoreRead",
"method11", new TypeReference<ExtDirectStoreResult<Row>>() {
// nothing here
}, readRequest);

assertThat(storeResponse.getTotal()).isEqualTo(50L);
assertThat(storeResponse.getRecords()).hasSize(50);
int ix = 0;
for (Row row : storeResponse.getRecords()) {
assertThat(row.getName()).startsWith("name: " + ix + ":cValue:rValue");
ix += 2;
}

readRequest = new HashMap<String, Object>();
readRequest.put("query", "name");

storeResponse = (ExtDirectStoreResult<Row>) ControllerUtil.sendAndReceive(
mockMvc, "remoteProviderStoreRead", "method11",
new TypeReference<ExtDirectStoreResult<Row>>() {
// nothing here
}, readRequest);

assertThat(storeResponse.getTotal()).isEqualTo(50L);
assertThat(storeResponse.getRecords()).hasSize(50);
ix = 0;
for (Row row : storeResponse.getRecords()) {
assertThat(row.getName()).startsWith(
"name: " + ix + ":defaultCookie:defaultHeader");
ix += 2;
}
}

@Test
public void testMetadata() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.fest.assertions.api.Assertions.entry;

import java.math.BigDecimal;
import java.security.Principal;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
Expand All @@ -27,6 +28,7 @@
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
Expand All @@ -40,6 +42,7 @@
import ch.ralscha.extdirectspring.bean.ExtDirectStoreResult;
import ch.ralscha.extdirectspring.bean.SSEvent;
import ch.ralscha.extdirectspring.provider.RemoteProviderSimpleNamed.ResultObject;
import ch.ralscha.extdirectspring.provider.RemoteProviderTreeLoad.Node;

@Service
public class RemoteProviderOptional {
Expand Down Expand Up @@ -297,4 +300,29 @@ public ExtDirectStoreResult<Row> storeRead2(ExtDirectStoreReadRequest request,
":" + cookie.orElse("defaultCookie") + ":"
+ requestHeader.orElse("defaultHeader"));
}

@ExtDirectMethod(value = ExtDirectMethodType.TREE_LOAD, group = "optional")
public List<Node> treeLoad1(@RequestParam("node") Optional<String> node,
HttpServletResponse response, final HttpServletRequest request,
@RequestParam Optional<String> foo, @CookieValue Optional<String> theCookie,
final HttpSession session, Locale locale, Principal principal) {

return RemoteProviderTreeLoad.createTreeList(
node.get(),
":" + foo.orElse("defaultValue2") + ";"
+ theCookie.orElse("defaultCookieValue") + ";"
+ (response != null) + ";" + (request != null) + ";"
+ (session != null) + ";" + locale);
}

@ExtDirectMethod(value = ExtDirectMethodType.TREE_LOAD, group = "optional")
public List<Node> treeLoad2(@RequestParam("node") Optional<String> node,
HttpServletResponse response, @RequestHeader Optional<Boolean> aHeader,
HttpServletRequest request) {

return RemoteProviderTreeLoad.createTreeList(node.get(),
":" + aHeader.orElse(true) + ";" + (response != null) + ";"
+ (request != null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -35,6 +36,8 @@
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
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;
Expand Down Expand Up @@ -130,6 +133,34 @@ public ExtDirectStoreResult<Row> method9(ExtDirectStoreReadRequest request) {
return result;
}

@ExtDirectMethod(value = ExtDirectMethodType.STORE_READ)
public ExtDirectStoreResult<Row> method10(ExtDirectStoreReadRequest request,
Locale locale, @RequestParam(value = "id", required = false,
defaultValue = "20") Integer id) {

if (!id.equals(20)) {
assertThat(id).isEqualTo(10);
assertThat(request.getParams().size()).isEqualTo(1);
assertThat(request.getParams()).contains(entry("id", 10));
}
else {
assertThat(id).isEqualTo(20);
assertThat(request.getParams().isEmpty()).isTrue();
}
assertThat(locale).isEqualTo(Locale.ENGLISH);

return RemoteProviderStoreRead.createExtDirectStoreResult(request, ":" + id + ";"
+ locale);
}

@ExtDirectMethod(value = ExtDirectMethodType.STORE_READ)
public ExtDirectStoreResult<Row> method11(ExtDirectStoreReadRequest request,
@CookieValue(defaultValue = "defaultCookie") String cookie, @RequestHeader(
defaultValue = "defaultHeader") String requestHeader) {
return RemoteProviderStoreRead.createExtDirectStoreResult(request, ":" + cookie
+ ":" + requestHeader);
}

public static ExtDirectStoreResult<Row> createExtDirectStoreResult(
ExtDirectStoreReadRequest request, final String appendix) {
List<Row> rows = createRows(appendix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static List<Node> createTreeList(String id) {
return createTreeList(id, "");
}

private static List<Node> createTreeList(String id, String appendix) {
public static List<Node> createTreeList(String id, String appendix) {
List<Node> result = new ArrayList<Node>();
if (id.equals("root")) {
for (int i = 1; i <= 5; ++i) {
Expand Down

0 comments on commit 0f25df2

Please sign in to comment.