org.springframework.test.web.servlet.RequestBuilder Java Examples

The following examples show how to use org.springframework.test.web.servlet.RequestBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CatalogControllerTest.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void subsetCommandTest() throws Exception {

  RequestBuilder rb = MockMvcRequestBuilders.get(path).servletPath(path).param("dataset",
      "scanCdmUnitTests/agg/pointFeatureCollection/netCDFbuoydata/BOD001_000_20050627_20051109.nc");

  MvcResult result = this.mockMvc.perform(rb).andExpect(MockMvcResultMatchers.status().is(200))
      .andExpect(MockMvcResultMatchers.content()
          .string(new StringContains(
              "scanCdmUnitTests/agg/pointFeatureCollection/netCDFbuoydata/BOD001_000_20050627_20051109.nc"))) // LOOK
                                                                                                              // not
                                                                                                              // actually
                                                                                                              // testing
                                                                                                              // subset
      .andExpect(MockMvcResultMatchers.content().contentType(expectType)).andReturn();

  System.out.printf("%s%n", result.getResponse().getContentAsString());
}
 
Example #2
Source File: TestPointController.java    From hyena with Apache License 2.0 6 votes vote down vote up
@Test
public void test_increase_fail_b() throws Exception {
    StringBuilder buf = new StringBuilder();
    buf.append("{").append("\"uid\":\"").append(super.getUid()).append("\",")
            .append("\"point\":\"").append(123).append("\",")
            .append("\"type\":null")
            .append("}");
    RequestBuilder builder = MockMvcRequestBuilders.post("/hyena/point/increase")
            .contentType(MediaType.APPLICATION_JSON)
            .content(buf.toString());

    String resBody = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    logger.info("response = {}", resBody);
    BaseResponse res = JsonUtils.fromJson(resBody, BaseResponse.class);
    Assertions.assertEquals(HyenaConstants.RES_CODE_PARAMETER_ERROR, res.getStatus());
}
 
Example #3
Source File: ConditionTest.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Test
public void stringContactHitRule() throws Exception {
    String arr = "[[\"0\", \"18\", \"substring\", \"a,11,12\"], [\"28\", \"39\", \"concat\", \"b,a\"]]";
    rule.setPayload("{\"a\":\"12345678901234567\",\"b\":\"12345678901234567\",\"c\":10,\"d\":10,\"e\":10}");

    rule.setSelectField("a");
    rule.setConditionField("a.substring(11,12)==\"2\" and b.concat(a)!=\"1234567890123456712345678901234567\"");
    rule.setFunctionArray(arr);
    rule.setConditionType(2);
    rule.setId("20191230");

    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result3 = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    log.info("result3:{}", result3.getResponse().getContentAsString());
    assertEquals(200, result3.getResponse().getStatus());
}
 
Example #4
Source File: CdmrControllerTest.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void cdmRemoteRequestCapabilitiesTest() throws Exception {
  RequestBuilder rb = MockMvcRequestBuilders.get(path).servletPath(path).param("req", "capabilities");

  logger.debug("{}?req=capabilities", path);

  MvcResult result = this.mockMvc.perform(rb).andExpect(MockMvcResultMatchers.status().is(200))
      .andExpect(MockMvcResultMatchers.content().contentType(ContentType.xml.getContentHeader())).andReturn();

  /*
   * String content =
   * "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
   * "<cdmRemoteCapabilities location=\"http://localhost:80/cdmremote/NCOF/POLCOMS/IRISH_SEA/files/20060925_0600.nc\">"
   * +
   * "  <featureDataset type=\"GRID\" url=\"http://localhost:80/cdmremote/NCOF/POLCOMS/IRISH_SEA/files/20060925_0600.nc\" />"
   * +
   * "</cdmRemoteCapabilities>"; // LAME
   * 
   * assert content.equals(result.getResponse().getContentAsString());
   */
}
 
Example #5
Source File: TestPointController.java    From hyena with Apache License 2.0 6 votes vote down vote up
@Test
public void test_listPointRecord() throws Exception {
    Thread.sleep(100L);
    ListPointRecParam param = new ListPointRecParam();
    param.setType(super.getPointType());
    RequestBuilder builder = MockMvcRequestBuilders.post("/hyena/point/listPointRecord")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JsonUtils.toJsonString(param));

    String resBody = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    logger.info("response = {}", resBody);
    ListResponse<PointRecDto> res = JsonUtils.fromJson(resBody, new TypeReference<>() {

    });
    List<PointRecDto> list = res.getData();
    Assertions.assertFalse(list.isEmpty());
    Assertions.assertTrue(res.getTotal() > 0L);
}
 
Example #6
Source File: GridDatasetControllerTest.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void getGridSubsetOnGridDataset() throws Exception {
  RequestBuilder rb = MockMvcRequestBuilders.get("/ncss/grid/testGFSfmrc/GFS_CONUS_80km_nc_best.ncd")
      .servletPath("/ncss/grid/testGFSfmrc/GFS_CONUS_80km_nc_best.ncd")
      .param("accept", SupportedFormat.NETCDF3.getFormatName())
      .param("var", "Relative_humidity_height_above_ground", "Temperature_height_above_ground");

  MvcResult result = this.mockMvc.perform(rb).andExpect(MockMvcResultMatchers.status().isOk())
      .andExpect(MockMvcResultMatchers.content().contentType(SupportedFormat.NETCDF3.getMimeType()))
      .andExpect(MockMvcResultMatchers.header().string(Constants.Content_Disposition, new FilenameMatcher(".nc")))
      .andReturn();

  System.out.printf("Headers%n");
  for (String name : result.getResponse().getHeaderNames()) {
    System.out.printf("%s= %s%n", name, result.getResponse().getHeader(name));
  }
}
 
Example #7
Source File: CdmrControllerTest.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void cdmRemoteRequestNcmlTest() throws Exception {
  RequestBuilder rb = MockMvcRequestBuilders.get(path).servletPath(path).param("req", "ncml");

  MvcResult result = this.mockMvc.perform(rb).andExpect(MockMvcResultMatchers.status().is(200))
      .andExpect(MockMvcResultMatchers.content().contentType(ContentType.xml.getContentHeader())).andReturn();

  // We want this statement to succeed without exception.
  // Throws NullPointerException if header doesn't exist
  // Throws IllegalArgumentException if header value is not a valid date.
  result.getResponse().getDateHeader("Last-Modified");

  Document doc = XmlUtil.getStringResponseAsDoc(result.getResponse());

  int hasDims = NcmlParserUtil.getNcMLElements("netcdf/dimension", doc).size();
  int hasAtts = NcmlParserUtil.getNcMLElements("netcdf/attribute", doc).size();
  int hasVars = NcmlParserUtil.getNcMLElements("//variable", doc).size();

  // Not really checking the content just the number of elements
  assertEquals(this.ndims, hasDims);
  assertEquals(this.natts, hasAtts);
  assertEquals(this.nvars, hasVars);
}
 
Example #8
Source File: TestStationFCController.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
@Ignore("takes too long")
public void getAllStnsOnStationDataset() throws Exception {
  RequestBuilder rb = MockMvcRequestBuilders.get(dataset).servletPath(dataset).param("accept", "netcdf")
      .param("stns", "all").param("var", "air_temperature", "dew_point_temperature")
      .param("time_start", "2006-03-25T00:00:00Z").param("time_end", "2006-03-26T00:00:00Z");


  long start = System.currentTimeMillis();
  try {
    this.mockMvc.perform(rb).andExpect(MockMvcResultMatchers.status().isOk())
        .andExpect(MockMvcResultMatchers.content().contentType(ContentType.netcdf.getContentHeader()));
  } finally {
    long took = System.currentTimeMillis() - start;
    System.out.printf("that took %d msecs%n", took);
  }

}
 
Example #9
Source File: TestPointController.java    From hyena with Apache License 2.0 6 votes vote down vote up
@Test
public void test_listPoint() throws Exception {

    ListPointParam param = new ListPointParam();
    param.setType(super.getPointType());
    param.setUidList(List.of(super.getUid()));
    param.setStart(0L).setSize(10);


    RequestBuilder builder = MockMvcRequestBuilders.post("/hyena/point/listPoint")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JsonUtils.toJsonString(param));

    String resBody = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    logger.info("response = {}", resBody);
    ListResponse<PointPo> res = JsonUtils.fromJson(resBody, new TypeReference<ListResponse<PointPo>>() {

    });
    List<PointPo> list = res.getData();
    Assertions.assertFalse(list.isEmpty());
}
 
Example #10
Source File: TestSystemController.java    From hyena with Apache License 2.0 6 votes vote down vote up
@Test
public void test_dumpQueue() throws Exception {
    String pointType = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
    RequestBuilder builder = MockMvcRequestBuilders.get("/hyena/system/dumpQueue").param("name", pointType);

    String resBody = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    logger.info("response = {}", resBody);
    BaseResponse res = JsonUtils.fromJson(resBody, BaseResponse.class);
    Assertions.assertEquals(HyenaConstants.RES_CODE_SUCCESS, res.getStatus());

    // add twice
    resBody = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    logger.info("response = {}", resBody);
    res = JsonUtils.fromJson(resBody, BaseResponse.class);
    Assertions.assertEquals(HyenaConstants.RES_CODE_SUCCESS, res.getStatus());
}
 
Example #11
Source File: TestPointController.java    From hyena with Apache License 2.0 6 votes vote down vote up
@Test
public void test_freeze() throws Exception {
    PointFreezeParam param = new PointFreezeParam();
    param.setType(super.getPointType());
    param.setUid(super.getUid());
    param.setSubUid(super.getSubUid());
    param.setPoint(BigDecimal.valueOf(1L));
    RequestBuilder builder = MockMvcRequestBuilders.post("/hyena/point/freeze")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JsonUtils.toJsonString(param));

    String resBody = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    logger.info("response = {}", resBody);
    ObjectResponse<PointOpResult> res = JsonUtils.fromJson(resBody, new TypeReference<ObjectResponse<PointOpResult>>() {

    });
    PointPo result = res.getData();
    Assertions.assertNotNull(result);
}
 
Example #12
Source File: ConditionTest.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteTheRule() throws Exception {
    String arr = "";
    rule.setFromDestination("from.com.weevent.test");
    rule.setPayload("{\"a\":1,\"b\":\"test\",\"c\":10}");
    rule.setSelectField("a,eventId,topicName,brokerId,groupId,now,currentDate,currentTime");
    rule.setConditionField("c==10 and a>10");
    rule.setToDestination("to.com.weevent.test");
    rule.setFunctionArray(arr);
    rule.setConditionType(2);
    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    log.info("result3:{}", result.getResponse().getContentAsString());
    assertEquals(200, result.getResponse().getStatus());

    String url2 = "/deleteCEPRuleById";
    RequestBuilder requestBuilder2 = MockMvcRequestBuilders.post(url2).contentType(MediaType.APPLICATION_JSON).param("id", rule.getId());
    MvcResult result2 = mockMvc.perform(requestBuilder2).andDo(print()).andReturn();
    log.info("result:{}", result2.getResponse().getContentAsString());
    assertEquals(200, result.getResponse().getStatus());
}
 
Example #13
Source File: TestPointController.java    From hyena with Apache License 2.0 6 votes vote down vote up
@Test
public void test_unfreeze() throws Exception {
    PointUsage freeze = new PointUsage();
    freeze.setPoint(BigDecimal.valueOf(9L)).setType(super.getPointType())
            .setUid(super.getUid()).setSubUid(super.getSubUid());
    this.pointUsageFacade.freeze(freeze);

    PointUnfreezeParam param = new PointUnfreezeParam();
    param.setType(super.getPointType());
    param.setUid(super.getUid());
    param.setSubUid(super.getSubUid());
    param.setPoint(BigDecimal.valueOf(9L));
    RequestBuilder builder = MockMvcRequestBuilders.post("/hyena/point/unfreeze")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JsonUtils.toJsonString(param));

    String resBody = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    logger.info("response = {}", resBody);
    ObjectResponse<PointOpResult> res = JsonUtils.fromJson(resBody, new TypeReference<ObjectResponse<PointOpResult>>() {

    });
    PointPo result = res.getData();
    Assertions.assertNotNull(result);
}
 
Example #14
Source File: ServiceTest.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Test
public void multiHitRule() throws Exception {
    String arr ="[[\"0\",\"8\", \"abs\",\"age\"]]";
    rule.setFromDestination("from.com.weevent.test");
    rule.setPayload("{\"age\":1,\"name\":\"rocky\"}");
    rule.setSelectField("age,name,eventId,topicName,brokerId,groupId");
    rule.setConditionField("abs(age)>20 and name!=\"name\"");
    rule.setToDestination("to.com.weevent.test");
    rule.setFunctionArray(arr);
    rule.setConditionType(1);
    rule.setId("202002101213");
    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result3 = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    log.info("result3:{}", result3.getResponse().getStatus());
    assertEquals(200, result3.getResponse().getStatus());
}
 
Example #15
Source File: TestPointController.java    From hyena with Apache License 2.0 6 votes vote down vote up
@Test
public void test_getIncreasedPoint() throws Exception {
    Thread.sleep(100L);
    Calendar start = Calendar.getInstance();
    start.add(Calendar.DATE, -1);
    Calendar end = Calendar.getInstance();
    end.add(Calendar.DATE, 1);

    RequestBuilder builder = MockMvcRequestBuilders.get("/hyena/point/getIncreasedPoint")
            .param("type", super.getPointType())
            .param("uid", super.getUid())
            .param("start", DateUtils.toYyyyMmDdHhMmSs(start))
            .param("end", DateUtils.toYyyyMmDdHhMmSs(end));

    String resBody = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    logger.info("response = {}", resBody);
    ObjectResponse<Long> res = JsonUtils.fromJson(resBody, new TypeReference<ObjectResponse<Long>>() {

    });
    Long result = res.getData();
    Assertions.assertNotNull(result);
    Assertions.assertTrue(result  > 0L);
}
 
Example #16
Source File: UserControllerUnitTest.java    From java-starthere with MIT License 6 votes vote down vote up
@Test
public void getUserByName() throws Exception
{
    String apiUrl = "/users/user/name/testing";

    Mockito.when(userService.findByName("testing")).thenReturn(userList.get(0));

    RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);
    MvcResult r = mockMvc.perform(rb).andReturn(); // this could throw an exception
    String tr = r.getResponse().getContentAsString();

    ObjectMapper mapper = new ObjectMapper();
    String er = mapper.writeValueAsString(userList.get(0));

    System.out.println("Expect: " + er);
    System.out.println("Actual: " + tr);

    assertEquals("Rest API Returns List", er, tr);
}
 
Example #17
Source File: HtmlUnitRequestBuilder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public Object merge(@Nullable Object parent) {
	if (parent instanceof RequestBuilder) {
		if (parent instanceof MockHttpServletRequestBuilder) {
			MockHttpServletRequestBuilder copiedParent = MockMvcRequestBuilders.get("/");
			copiedParent.merge(parent);
			this.parentBuilder = copiedParent;
		}
		else {
			this.parentBuilder = (RequestBuilder) parent;
		}
		if (parent instanceof SmartRequestBuilder) {
			this.parentPostProcessor = (SmartRequestBuilder) parent;
		}
	}
	return this;
}
 
Example #18
Source File: UserControllerUnitTest.java    From java-starthere with MIT License 6 votes vote down vote up
@Test
public void listReallyAllUsers() throws Exception
{
    String apiUrl = "/users/users/all";

    Mockito.when(userService.findAll(Pageable.unpaged())).thenReturn(userList);

    RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);

    // the following actually performs a real controller call
    MvcResult r = mockMvc.perform(rb).andReturn(); // this could throw an exception
    String tr = r.getResponse().getContentAsString();

    ObjectMapper mapper = new ObjectMapper();
    String er = mapper.writeValueAsString(userList);

    System.out.println("Expect: " + er);
    System.out.println("Actual: " + tr);

    assertEquals("Rest API Returns List", er, tr);
}
 
Example #19
Source File: ConditionTest.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Test
public void ceilHitRule() throws Exception {
    String arr = "[[\"0\", \"7\", \"floor\", \"c\"]]";
    String url = "/startCEPRule";
    rule.setFromDestination("from.com.weevent.test");
    rule.setPayload("{\"a\":1,\"b\":\"test\",\"c\":10}");
    rule.setSelectField("a,eventId,topicName,brokerId,groupId");
    rule.setConditionField("ceil(c)<10");
    rule.setToDestination("to.com.weevent.test");
    rule.setFunctionArray(arr);
    rule.setConditionType(2);
    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result3 = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    log.info("result3:{}", result3.getResponse().getContentAsString());
    assertEquals(200, result3.getResponse().getStatus());
}
 
Example #20
Source File: CdmrControllerTest.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void cdmRemoteRequestHeaderTest() throws Exception {
  RequestBuilder rb = MockMvcRequestBuilders.get(path).servletPath(path).param("req", "header");

  MvcResult result = this.mockMvc.perform(rb).andExpect(MockMvcResultMatchers.status().is(200))
      .andExpect(MockMvcResultMatchers.content().contentType(ContentType.binary.getContentHeader())).andReturn();

  // We want this statement to succeed without exception.
  // Throws NullPointerException if header doesn't exist
  // Throws IllegalArgumentException if header value is not a valid date.
  result.getResponse().getDateHeader("Last-Modified");

  // response is a ncstream
  ByteArrayInputStream bais = new ByteArrayInputStream(result.getResponse().getContentAsByteArray());
  CdmRemote cdmr = new CdmRemote(bais, "test");
  cdmr.close();
}
 
Example #21
Source File: UserControllerUnitTest.java    From java-starthere with MIT License 6 votes vote down vote up
@Test
public void listAllUsers() throws Exception
{
    String apiUrl = "/users/users";

    Mockito.when(userService.findAll(any(Pageable.class))).thenReturn(userList);

    RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);

    // the following actually performs a real controller call
    MvcResult r = mockMvc.perform(rb).andReturn(); // this could throw an exception
    String tr = r.getResponse().getContentAsString();

    ObjectMapper mapper = new ObjectMapper();
    String er = mapper.writeValueAsString(userList);

    System.out.println("Expect: " + er);
    System.out.println("Actual: " + tr);

    assertEquals("Rest API Returns List", er, tr);
}
 
Example #22
Source File: ConditionTest.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Test
public void systemParameterToDB() throws Exception {
    String arr = "";
    String url = "/startCEPRule";
    rule.setFromDestination("from.com.weevent.test");
    rule.setPayload("{\"a\":1,\"b\":\"test\",\"c\":10}");
    rule.setSelectField("a,eventId,topicName,brokerId,groupId,now,currentDate,currentTime");
    rule.setConditionField("c==10 and a>10");
    rule.setToDestination("to.com.weevent.test");
    rule.setFunctionArray(arr);
    rule.setConditionType(2);
    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    assertEquals(200, result.getResponse().getStatus());
    log.info("result3:{}", result.getResponse().getContentAsString());
}
 
Example #23
Source File: ConditionTest.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Test
public void currentTimeSystemToTopic2() throws Exception {
    String arr = "[[\"10\",\"21\",\"currentDate\",\"datatime\"]]";
    String url = "/startCEPRule";
    rule.setId("110000");
    rule.setFromDestination("from.com.weevent.test");
    rule.setPayload("{\"age\":1,\"name\":\"test\",\"datatime\":20200210}");
    rule.setSelectField("a,eventId,currentDate");
    rule.setConditionField("datatime>=currentDate");
    rule.setToDestination("to.com.weevent.test");
    rule.setFunctionArray(arr);
    rule.setConditionType(1);
    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    assertEquals(200, result.getResponse().getStatus());
    log.info("result3:{}", result);
}
 
Example #24
Source File: ConditionTest.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Test
public void currentTimeSystemToDB() throws Exception {
    String arr = "";
    String url = "/startCEPRule";
    rule.setFromDestination("from.com.weevent.test");
    rule.setPayload("{\"a\":1,\"b\":\"test\",\"c\":10}");
    rule.setSelectField("a,eventId,currentTime");
    rule.setConditionField("c==10 and a>10");
    rule.setToDestination("to.com.weevent.test");
    rule.setFunctionArray(arr);
    rule.setConditionType(2);
    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    assertEquals(200, result.getResponse().getStatus());
    log.info("result3:{}", result);
}
 
Example #25
Source File: TestPointFCsubsetting.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void getSubsettedData() throws Exception {
  // problem is that browser wont display text/csv in line, so use tesxt/plain; see thredds.server.ncss.view
  // .dsg.PointWriter.WriterCSV
  String expectFormat =
      (format == SupportedFormat.CSV_STREAM) ? ContentType.text.getContentHeader() : format.getMimeType();

  RequestBuilder rb = MockMvcRequestBuilders.get(dataset + req + format.getFormatName()).servletPath(dataset);
  System.out.printf("%nURL='%s'%n", dataset + req + format.getFormatName());
  MvcResult result = this.mockMvc.perform(rb).andExpect(MockMvcResultMatchers.status().isOk())
      .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(expectFormat)).andReturn();

  MockHttpServletResponse response = result.getResponse();
  System.out.printf("getSubsettedData format=%s status = %d type=%s%n", format, response.getStatus(),
      response.getContentType());
}
 
Example #26
Source File: UserControllerUnitTest.java    From java-starthere with MIT License 6 votes vote down vote up
@Test
public void getUserById() throws Exception
{
    String apiUrl = "/users/user/12";

    Mockito.when(userService.findUserById(12)).thenReturn(userList.get(1));

    RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);
    MvcResult r = mockMvc.perform(rb).andReturn(); // this could throw an exception
    String tr = r.getResponse().getContentAsString();

    ObjectMapper mapper = new ObjectMapper();
    String er = mapper.writeValueAsString(userList.get(1));

    System.out.println("Expect: " + er);
    System.out.println("Actual: " + tr);

    assertEquals("Rest API Returns List", er, tr);
}
 
Example #27
Source File: HtmlUnitRequestBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public Object merge(@Nullable Object parent) {
	if (parent instanceof RequestBuilder) {
		if (parent instanceof MockHttpServletRequestBuilder) {
			MockHttpServletRequestBuilder copiedParent = MockMvcRequestBuilders.get("/");
			copiedParent.merge(parent);
			this.parentBuilder = copiedParent;
		}
		else {
			this.parentBuilder = (RequestBuilder) parent;
		}
		if (parent instanceof SmartRequestBuilder) {
			this.parentPostProcessor = (SmartRequestBuilder) parent;
		}
	}
	return this;
}
 
Example #28
Source File: ConditionTest.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
@Test
public void conditionEqualToTopic() throws Exception {
    String arr = "";
    rule.setPayload("{\"a\":1,\"b\":\"test\",\"c\":10}");
    rule.setSelectField("a,eventId,topicName,brokerId,groupId");
    rule.setConditionField("c==10");
    rule.setFunctionArray(arr);
    rule.setConditionType(1);
    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    assertEquals(200, result.getResponse().getStatus());
    log.info("result3:{}", result);
}
 
Example #29
Source File: ConditionTest.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
@Test
public void nowSystemToDB() throws Exception {
    String arr = "";
    rule.setFromDestination("from.com.weevent.test");
    rule.setPayload("{\"a\":1,\"b\":\"test\",\"c\":10}");
    rule.setSelectField("a,eventId,now");
    rule.setConditionField("c==10 and a>10");
    rule.setToDestination("to.com.weevent.test");
    rule.setFunctionArray(arr);
    rule.setConditionType(2);
    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    assertEquals(200, result.getResponse().getStatus());
    log.info("result3:{}", result);
}
 
Example #30
Source File: ConditionTest.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
@Test
public void conditionConplexToTopic2() throws Exception {
    String arr = "";
    String url = "/startCEPRule";
    rule.setPayload("{\"a\":1,\"b\":\"test\",\"c\":10}");
    rule.setSelectField("a,eventId,topicName,brokerId,groupId");
    rule.setConditionField("c==10 and a>10");
    rule.setFunctionArray(arr);
    rule.setConditionType(1);
    RequestBuilder requestBuilder3 = MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(JsonHelper.object2Json(rule));
    MvcResult result = mockMvc.perform(requestBuilder3).andDo(print()).andReturn();
    assertEquals(200, result.getResponse().getStatus());
    log.info("result3:{}", result);
}