@Test
  public void testFilters() throws Exception {

    int index = 1;
    for (String json : jsonList) {
      MvcResult result = ControllerUtil.performRouterRequest(this.mockMvc, json);
      List<ExtDirectResponse> responses =
          ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());

      assertThat(responses).hasSize(1);
      ExtDirectResponse resp = responses.get(0);
      assertThat(resp.getAction()).isEqualTo("remoteProviderStoreRead");
      assertThat(resp.getMethod()).isEqualTo("methodFilter");
      assertThat(resp.getType()).isEqualTo("rpc");
      assertThat(resp.getTid()).isEqualTo(index);
      assertThat(resp.getMessage()).isNull();
      assertThat(resp.getWhere()).isNull();
      assertThat(resp.getResult()).isNotNull();

      List<Row> rows =
          ControllerUtil.convertValue(
              resp.getResult(),
              new TypeReference<List<Row>>() {
                // nothing here
              });

      assertThat(rows).hasSize(1);
      assertThat(rows.get(0).getId()).isEqualTo(index);

      index++;
    }

    assertThat(index).isEqualTo(34);
  }
  @Test
  public void testWithAdditionalParametersOptional() {
    ExtDirectStoreReadResult<Row> rows =
        (ExtDirectStoreReadResult<Row>)
            ControllerUtil.sendAndReceive(
                mockMvc,
                "remoteProviderStoreReadDeprecated",
                "method7",
                new TypeReference<ExtDirectStoreReadResult<Row>>() {
                  /* nothing_here */
                });
    RouterControllerStoreReadTest.assert100Rows(new ArrayList<Row>(rows.getRecords()), ":null");

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

    rows =
        (ExtDirectStoreReadResult<Row>)
            ControllerUtil.sendAndReceive(
                mockMvc,
                "remoteProviderStoreReadDeprecated",
                "method7",
                new TypeReference<ExtDirectStoreReadResult<Row>>() {
                  /* nothing_here */
                },
                readRequest);
    RouterControllerStoreReadTest.assert100Rows(new ArrayList<Row>(rows.getRecords()), ":11");
  }
  @SuppressWarnings("unchecked")
  private void callTreeLoadAndCheckResult(String method) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("aHeader", "true");

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

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

    String 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));

    headers = new HttpHeaders();
    headers.add("aHeader", "false");

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

    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));
  }
  @SuppressWarnings("unchecked")
  @Test
  public void testAdditionalParameters() {

    Map<String, Object> requestParameters = new LinkedHashMap<String, Object>();
    requestParameters.put("node", "root");
    requestParameters.put("foo", "foo");
    requestParameters.put("today", ISODateTimeFormat.date().print(new LocalDate()));

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

    String appendix = ":foo;" + new LocalDate().toString();
    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", "root");
    requestParameters.put("today", ISODateTimeFormat.date().print(new LocalDate().plusDays(10)));

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

    appendix = ":defaultValue;" + new LocalDate().plusDays(10).toString();
    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));
  }
예제 #5
0
  public static Object sendAndReceive(
      RouterController controller,
      MockHttpServletRequest request,
      final String action,
      String method,
      boolean namedParameter,
      Object data,
      final Object result) {

    MockHttpServletResponse response = new MockHttpServletResponse();

    int tid = (int) (Math.random() * 1000);
    Map<String, Object> edRequest = createRequestJson(action, method, namedParameter, tid, data);

    request.setContent(ControllerUtil.writeAsByte(edRequest));
    try {
      controller.router(request, response, Locale.ENGLISH);
    } catch (IOException e) {
      fail("call controller.router: " + e.getMessage());
    }
    List<ExtDirectResponse> responses = readDirectResponses(response.getContentAsByteArray());
    assertThat(responses).hasSize(1);

    ExtDirectResponse edResponse = responses.get(0);

    assertThat(edResponse.getAction()).isEqualTo(action);
    assertThat(edResponse.getMethod()).isEqualTo(method);
    assertThat(edResponse.getTid()).isEqualTo(tid);
    assertThat(edResponse.getWhere()).isNull();

    if (result == null) {
      assertThat(edResponse.getType()).isEqualTo("exception");
      assertThat(edResponse.getResult()).isNull();
      assertThat(edResponse.getMessage()).isEqualTo("Server Error");
    } else {
      assertThat(edResponse.getType()).isEqualTo("rpc");
      assertThat(edResponse.getMessage()).isNull();
      if (result == Void.TYPE) {
        assertThat(edResponse.getResult()).isNull();
      } else if (result instanceof Class<?>) {
        Object r = ControllerUtil.convertValue(edResponse.getResult(), (Class<?>) result);
        return r;
      } else if (result instanceof TypeReference) {
        Object r = ControllerUtil.convertValue(edResponse.getResult(), (TypeReference<?>) result);
        return r;
      } else {
        assertThat(edResponse.getResult()).isEqualTo(result);
      }
    }

    return edResponse.getResult();
  }
  @SuppressWarnings("unchecked")
  @Test
  public void testNoAdditionalParameters() {

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

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

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

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

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

    assertThat(nodes)
        .hasSize(5)
        .containsSequence(
            new Node("id1", "Node 1.1", true),
            new Node("id2", "Node 1.2", true),
            new Node("id3", "Node 1.3", true),
            new Node("id4", "Node 1.4", true),
            new Node("id5", "Node 1.5", true));
  }
예제 #7
0
  @SuppressWarnings("unchecked")
  private void callMethod(String bean, String method, MapEntry... expectedEntries) {
    Object obj = ControllerUtil.sendAndReceiveObject(mockMvc, bean, method);
    List<Map<String, Object>> records;

    if (obj instanceof List) {
      records = (List<Map<String, Object>>) obj;
    } else {
      Map<String, Object> result = (Map<String, Object>) obj;
      assertThat(result)
          .hasSize(2)
          .contains(MapEntry.entry("success", true))
          .containsKey("records");
      records = (List<Map<String, Object>>) result.get("records");
    }

    assertThat(records).hasSize(2);
    for (int i = 1; i <= records.size(); i++) {
      Map<String, Object> model = records.get(i - 1);
      assertThat(model).hasSize(expectedEntries.length);

      for (MapEntry entry : expectedEntries) {
        if (entry.key.equals("id")) {
          assertThat(model).contains(MapEntry.entry("id", i));
        } else {
          assertThat(model).contains(MapEntry.entry(entry.key, "" + entry.value + i));
        }
      }
    }
  }
  @Test
  public void testWithConversion() throws IOException {

    DateTime today = new DateTime();

    Map<String, Object> resultMap =
        (Map<String, Object>)
            ControllerUtil.sendAndReceive(
                controller,
                "remoteProviderSimple",
                "method14",
                a(
                    ISODateTimeFormat.dateTime().print(today),
                    "normalParameter",
                    ISODateTimeFormat.date().print(today),
                    "99.9%"),
                Map.class);

    assertThat(resultMap.get("endDate")).isEqualTo(today.getMillis());
    ObjectMapper mapper = new ObjectMapper();

    List<Object> expectedValue =
        mapper.readValue(mapper.writeValueAsString(today.toLocalDate()), List.class);
    Object actualValue = resultMap.get("jodaLocalDate");

    assertThat((List<Object>) resultMap.get("jodaLocalDate")).isEqualTo(expectedValue);
    assertThat(resultMap.get("percent")).isEqualTo(0.999);
    assertThat(resultMap.get("normalParameter")).isEqualTo("normalParameter");
    assertThat(resultMap.get("remoteAddr")).isEqualTo("127.0.0.1");
  }
 @Test
 public void methodOptionalHeaderWithoutValueAndDefault1() throws Exception {
   MockHttpServletRequest request = new MockHttpServletRequest();
   request.addHeader("header", "headerValue");
   ControllerUtil.sendAndReceive(
       controller, request, "remoteProviderSimple", "method18", null, "headerValue");
 }
 @Test
 public void methodRequiredHeaderWithoutValue() throws Exception {
   MockHttpServletRequest request = new MockHttpServletRequest();
   request.addHeader("header", "headerValue");
   ControllerUtil.sendAndReceive(
       controller, request, "remoteProviderSimple", "method15", a(1, "v"), "1;v;headerValue");
 }
 @Test
 public void testWithParametersWithTypeConversion() throws IOException {
   ControllerUtil.sendAndReceive(
       controller,
       "remoteProviderSimple",
       "method3",
       a("10", "4.2", 20),
       "method3() called-10-4.2-20");
 }
 @Test
 public void testMixParameterAndSupportedParameters() throws IOException {
   ControllerUtil.sendAndReceive(
       controller,
       "remoteProviderSimple",
       "method13",
       a("true", "c", "ACTIVE", "14", "21", "3.14", "10.01", "1", "2"),
       "method13() called-true-c-ACTIVE-14-21-3.14-10.01-1-2");
 }
 @Test
 public void methodRequiredHeaderWithValue() throws Exception {
   MockHttpServletRequest request = new MockHttpServletRequest();
   request.addHeader("header", "headerValue");
   request.addHeader("anotherName", "headerValue1");
   request.addHeader("anotherName", "headerValue2");
   ControllerUtil.sendAndReceive(
       controller, request, "remoteProviderSimple", "method16", a(11), "11;headerValue1");
 }
 @Test
 public void testNoParametersWithRequestParameter() throws IOException {
   ControllerUtil.sendAndReceive(
       controller,
       "remoteProviderSimple",
       "method1",
       a(1, "requestparameter"),
       "method1() called");
 }
 @Test
 public void testWithParameters() throws IOException {
   ControllerUtil.sendAndReceive(
       controller,
       "remoteProviderSimple",
       "method3",
       a(1, 3.1, "requestParameter"),
       "method3() called-1-3.1-requestParameter");
 }
 @Test
 public void testTypeConversion() throws IOException {
   ControllerUtil.sendAndReceive(
       controller,
       "remoteProviderSimple",
       "method10",
       a("true", "c", "ACTIVE", "14", "21", "3.14", "10.01", "1", "2"),
       "method10() called-true-c-ACTIVE-14-21-3.14-10.01-1-2");
 }
  @Test
  public void methodMultipleHeaders3() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("last", "last");
    request.addHeader("header1", "1st");
    request.addHeader("header2", "2nd");

    ControllerUtil.sendAndReceive(
        controller, request, "remoteProviderSimple", "method19", a(100), "100;1st;2nd;last");
  }
  @Test
  public void methodHeaderWithConversion() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(new MockHttpSession());
    request.addHeader("intHeader", "2");
    request.addHeader("booleanHeader", "true");

    ControllerUtil.sendAndReceive(
        controller, request, "remoteProviderSimple", "method20", null, "2;true");
  }
 @Test
 public void testTypeConversionWithObjects() throws IOException {
   Row aRow = new Row(104, "myRow", true, "100.45");
   ControllerUtil.sendAndReceive(
       controller,
       "remoteProviderSimple",
       "method12",
       a(aRow),
       "Row [id=104, name=myRow, admin=true, salary=100.45]");
 }
 @Test
 public void testNoArgumentsNoRequestParameters() {
   ExtDirectStoreReadResult<Row> rows =
       (ExtDirectStoreReadResult<Row>)
           ControllerUtil.sendAndReceive(
               mockMvc,
               "remoteProviderStoreReadDeprecated",
               "method1",
               new TypeReference<ExtDirectStoreReadResult<Row>>() {
                 /* nothing_here */
               });
   RouterControllerStoreReadTest.assert100Rows(new ArrayList<Row>(rows.getRecords()), "");
 }
  @Test
  public void methodMultipleHeaders1() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(new MockHttpSession());
    request.addHeader("last", "lastHeader");

    ControllerUtil.sendAndReceive(
        controller,
        request,
        "remoteProviderSimple",
        "method19",
        a(100),
        "100;default1;default2;lastHeader");
  }
  @Test
  public void testWithSingleObjectAsReturnType() {
    Map<String, Object> requestParameters = new LinkedHashMap<String, Object>();
    requestParameters.put("node", "root");

    Node node =
        (Node)
            ControllerUtil.sendAndReceive(
                mockMvc, "remoteProviderTreeLoad", "method6", Node.class, requestParameters);

    assertThat(node.id).isEqualTo("n1");
    assertThat(node.text).isEqualTo("Node 1;true;true");
    assertThat(node.leaf).isEqualTo(false);
  }
  @Test
  public void testReturnsObject() throws IOException {
    FormInfo info =
        (FormInfo)
            ControllerUtil.sendAndReceive(
                controller, "remoteProviderSimple", "method8", a(7.34), FormInfo.class);

    assertThat(info.getBack()).isEqualTo(7.34);
    assertThat(info.isAdmin()).isEqualTo(false);
    assertThat(info.getAge()).isEqualTo(32);
    assertThat(info.getName()).isEqualTo("John");
    assertThat(info.getSalary()).isEqualTo(new BigDecimal("8720.2"));
    assertThat(info.getBirthday())
        .isEqualTo(new GregorianCalendar(1986, Calendar.JULY, 22).getTime());
  }
  @Test
  public void testSupportedArguments() {

    ExtDirectStoreReadResult<Row> rows =
        (ExtDirectStoreReadResult<Row>)
            ControllerUtil.sendAndReceive(
                mockMvc,
                "remoteProviderStoreReadDeprecated",
                "method3",
                new TypeReference<ExtDirectStoreReadResult<Row>>() { // nothing
                  // here
                });

    RouterControllerStoreReadTest.assert100Rows(
        new ArrayList<Row>(rows.getRecords()), ":true;true:true;en");
  }
 @Test
 public void testCreateWithDataSingle() {
   ExtDirectStoreReadResult<Row> rows =
       (ExtDirectStoreReadResult<Row>)
           ControllerUtil.sendAndReceive(
               mockMvc,
               "remoteProviderStoreModifySingle",
               "create1",
               new TypeReference<ExtDirectStoreReadResult<Row>>() {
                 /* nothing here */
               },
               new Row(10, "Ralph", true, "109.55"));
   assertThat(rows.getRecords()).hasSize(1);
   assertThat(rows.isSuccess()).isTrue();
   Row row = rows.getRecords().iterator().next();
   assertThat(row.getId()).isEqualTo(10);
   assertThat(row.getName()).isEqualTo("Ralph");
   assertThat(row.getSalary()).isEqualTo(new BigDecimal("109.55"));
 }
 @Test
 public void methodOptionalHeaderWithoutValueAndDefault2() throws Exception {
   ControllerUtil.sendAndReceive(controller, "remoteProviderSimple", "method18", null, "default");
 }
 @Test
 public void testNoParameters2() throws IOException {
   ControllerUtil.sendAndReceive(
       controller, "remoteProviderSimple", "method2", null, "method2() called");
 }
 @Test
 public void testResultStringNull() throws IOException {
   ControllerUtil.sendAndReceive(controller, "remoteProviderSimple", "method7", null, Void.TYPE);
 }
 @Test
 public void testMethodNotFound() throws IOException {
   ControllerUtil.sendAndReceive(
       controller, "remoteProviderSimple", "method4", a(3, 2.5, "string.param"), null);
 }
 @Test
 public void testSupportedArguments() throws IOException {
   ControllerUtil.sendAndReceive(controller, "remoteProviderSimple", "method9", null, 42);
 }