Java Code Examples for org.springframework.mock.web.MockHttpServletRequest#setQueryString()

The following examples show how to use org.springframework.mock.web.MockHttpServletRequest#setQueryString() . 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: WmsControllerTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testReadImage() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();

	Map<String, String> parameters = new HashMap<String, String>();
	parameters.put("SERVICE", "WMS");
	parameters.put("layers", "bluemarble");
	parameters.put("WIDTH", "512");
	parameters.put("HEIGHT", "512");
	parameters.put("bbox", "-52.01245495052001,-28.207099921352835,11.947593278789554,35.75294830795673");
	parameters.put("format", "image/jpeg");
	parameters.put("version", "1.1.1");
	parameters.put("srs", "EPSG:4326");
	parameters.put("styles", "");
	parameters.put("request", "GetMap");
	request.setParameters(parameters);
	request.setRequestURI("d/wms/proxyBlue/");
	request.setQueryString("SERVICE=WMS&layers=bluemarble&" +
			"WIDTH=512&HEIGHT=512&bbox=-52.01245495052001,-28.207099921352835,11.947593278789554," +
			"35.75294830795673&format=image/jpeg&version=1.1.1&srs=EPSG:4326&styles=&request=GetMap");
	request.setMethod("GET");
	wmsController.getWms(request, response);
	new ImageAssert(response).assertEqualImage("wms.jpg", false, DELTA);
}
 
Example 2
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisIncludeWithNullIterableRelationshipCall() throws Exception {
	Node root = new Node(1L, null, null);
	// by making the setting children null and requesting them in the
	// include statement should cause a serialization error
	Node child1 = new Node(2L, root, null);
	Node child2 = new Node(3L, root, null);
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes");
	request.setRequestURI("/api/nodes");
	request.setQueryString("include[nodes]=children");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	assertEquals(500, response.getStatus());
}
 
Example 3
Source File: SampleKatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisInclude() throws Exception {

	Node root = new Node(1L, null, null);
	Node child1 = new Node(2L, root, Collections.<Node>emptySet());
	Node child2 = new Node(3L, root, Collections.<Node>emptySet());
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes/1");
	request.setRequestURI("/api/nodes/1");
	request.setQueryString("include[nodes]=parent");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	String responseContent = response.getContentAsString();
	assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 4
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisInclude() throws Exception {

	Node root = new Node(1L, null, null);
	Node child1 = new Node(2L, root, Collections.<Node>emptySet());
	Node child2 = new Node(3L, root, Collections.<Node>emptySet());
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes/1");
	request.setRequestURI("/api/nodes/1");
	request.setQueryString("include[nodes]=parent");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	String responseContent = response.getContentAsString();
	assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 5
Source File: CrnkServletRejectJsonTest.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Option to reject plain JSON GET requests is enabled explicitly.
 */
@Test
public void testRejectPlainJson() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath("/api");
    request.setPathInfo("/tasks");
    request.setRequestURI("/api/tasks");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "application/json");
    request.addParameter("filter[Task][name]", "John");
    request.setQueryString(URLEncoder.encode("filter[Task][name]", StandardCharsets.UTF_8.name()) + "=John");

    MockHttpServletResponse response = new MockHttpServletResponse();

    servlet.service(request, response);

    assertEquals(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus());
    String responseContent = response.getContentAsString();
    assertTrue(responseContent == null || "".equals(responseContent.trim()));
}
 
Example 6
Source File: CrnkServletTest.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnacceptableRequestContentType() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath("/api");
    request.setPathInfo("/tasks");
    request.setRequestURI("/api/tasks");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "application/xml");
    request.addParameter("filter[Task][name]", "John");
    request.setQueryString(URLEncoder.encode("filter[Task][name]", StandardCharsets.UTF_8.name()) + "=John");

    MockHttpServletResponse response = new MockHttpServletResponse();

    servlet.service(request, response);

    assertEquals(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus());
    String responseContent = response.getContentAsString();
    assertTrue(responseContent == null || "".equals(responseContent.trim()));
}
 
Example 7
Source File: BuildURIFromHttpRequest.java    From levelup-java-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void replace_query_parameter () {
	
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setQueryString("primaryKey=987");
	
	UriComponents ucb =
	        ServletUriComponentsBuilder
		        .fromRequest(request)
		        .replaceQueryParam("primaryKey", "{id}")
	        	.build()
	            .expand("123")
	            .encode();
	
	assertEquals("http://localhost?primaryKey=123", ucb.toString());
}
 
Example 8
Source File: HtmlUnitRequestBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
	Charset charset = getCharset();
	String httpMethod = this.webRequest.getHttpMethod().name();
	UriComponents uriComponents = uriComponents();
	String path = uriComponents.getPath();

	MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(
			servletContext, httpMethod, (path != null ? path : ""));
	parent(request, this.parentBuilder);
	String host = uriComponents.getHost();
	request.setServerName(host != null ? host : "");  // needs to be first for additional headers
	authType(request);
	request.setCharacterEncoding(charset.name());
	content(request, charset);
	contextPath(request, uriComponents);
	contentType(request);
	cookies(request);
	headers(request);
	locales(request);
	servletPath(uriComponents, request);
	params(request, uriComponents);
	ports(uriComponents, request);
	request.setProtocol("HTTP/1.1");
	request.setQueryString(uriComponents.getQuery());
	String scheme = uriComponents.getScheme();
	request.setScheme(scheme != null ? scheme : "");
	request.setPathInfo(null);

	return postProcess(request);
}
 
Example 9
Source File: WmsControllerCacheTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
//@Ignore // test fails as the passivated state is not removed, see CACHE-33
public void testReadCachedImage() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	Map<String, String> parameters = new HashMap<String, String>();
	parameters.put("SERVICE", "WMS");
	parameters.put("layers", "bluemarble");
	parameters.put("WIDTH", "512");
	parameters.put("HEIGHT", "512");
	parameters.put("bbox", "-52.01245495052001,-28.207099921352835,11.947593278789554,35.75294830795673");
	parameters.put("format", "image/jpeg");
	parameters.put("version", "1.1.1");
	parameters.put("srs", "EPSG:4326");
	parameters.put("styles", "");
	parameters.put("request", "GetMap");
	request.setParameters(parameters);
	request.setRequestURI("d/wms/cachedBlue/");
	request.setQueryString("SERVICE=WMS&layers=bluemarble&" +
			"WIDTH=512&HEIGHT=512&bbox=-52.01245495052001,-28.207099921352835,11.947593278789554," +
			"35.75294830795673&format=image/jpeg&version=1.1.1&srs=EPSG:4326&styles=&request=GetMap");
	request.setMethod("GET");

	testRecorder.clear();
	wmsController.getWms(request, response);
	new ImageAssert(response).assertEqualImage("wms.jpg", false, DELTA);
	assertThat(testRecorder.matches(CachingLayerHttpService.TEST_RECORDER_GROUP,
			CachingLayerHttpService.TEST_RECORDER_PUT_IN_CACHE)).isEmpty();

	testRecorder.clear();
	wmsController.getWms(request, response);
	new ImageAssert(response).assertEqualImage("wms.jpg", false, DELTA);
	assertThat(testRecorder.matches(CachingLayerHttpService.TEST_RECORDER_GROUP,
			CachingLayerHttpService.TEST_RECORDER_GET_FROM_CACHE)).isEmpty();
}
 
Example 10
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testURLTransaction() throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo/bar");
    request.setQueryString("foo=bar");
    filterChain.doFilter(request, new MockHttpServletResponse());
    Url url = reporter.getFirstTransaction().getContext().getRequest().getUrl();
    assertThat(url.getProtocol()).isEqualTo("http");
    assertThat(url.getSearch()).isEqualTo("foo=bar");
    assertThat(url.getPort().toString()).isEqualTo("80");
    assertThat(url.getHostname()).isEqualTo("localhost");
    assertThat(url.getPathname()).isEqualTo("/foo/bar");
    assertThat(url.getFull().toString()).isEqualTo("http://localhost/foo/bar?foo=bar");
}
 
Example 11
Source File: CrnkFilterTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void onCollectionRequestWithParamsGetShouldReturnCollection() throws Exception {
    MockFilterChain filterChain = new MockFilterChain();

    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath("/api");
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "*/*");
    request.addParameter("filter[name]", "First task");
    request.setQueryString(URLEncoder.encode("filter[name]", StandardCharsets.UTF_8.name()) + "=First task");

    MockHttpServletResponse response = new MockHttpServletResponse();

    filter.doFilter(request, response, filterChain);

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data[0].type");
    assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
    assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
Example 12
Source File: OpenURLServiceImplTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Sets up the query string and parameters based on the URL.
 * @param req
 */
public void init(MockHttpServletRequest req) {
	String url = req.getRequestURL().toString();
	int queryStart = url.indexOf('?');
	if (queryStart >= 0) {
		String query = url.substring(queryStart+1);
		req.setQueryString(query);
		req.setParameters(parseQueryString(query));
	}
}
 
Example 13
Source File: CrnkServletTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testInclude() throws Exception {

    Node root = new Node(1L, null, null);
    Node child1 = new Node(2L, root, Collections.emptySet());
    Node child2 = new Node(3L, root, Collections.emptySet());
    root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));

    ResourceRepository nodeRepository = (ResourceRepository) servlet.getBoot().getResourceRegistry()
            .getEntry(Node.class).getResourceRepository().getImplementation();
    nodeRepository.save(root);
    nodeRepository.save(child1);
    nodeRepository.save(child2);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setServletPath("/api");
    request.setPathInfo("/nodes/1");
    request.setRequestURI("/api/nodes/1");
    request.setQueryString("include[nodes]=parent");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    Map<String, String> params = new HashMap<>();
    params.put("include[nodes]", "children");
    request.setParameters(params);
    request.addHeader("Accept", "*/*");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    String responseContent = response.getContentAsString();
    assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 14
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testKatharsisIncludeNestedWithDefault() throws Exception {
	Node root = new Node(1L, null, null);
	Locale engLocale = new Locale(1L, java.util.Locale.ENGLISH);
	Node child1 = new Node(2L, root, Collections.<Node>emptySet());
	NodeComment child1Comment = new NodeComment(1L, "Child 1", child1, engLocale);
	child1.setNodeComments(new LinkedHashSet<>(Collections.singleton(child1Comment)));
	Node child2 = new Node(3L, root, Collections.<Node>emptySet(), Collections.<NodeComment>emptySet());
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);

	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes/1");
	request.setRequestURI("/api/nodes/1");
	request.setQueryString("include[nodes]=children.nodeComments");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children.nodeComments.langLocale");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	String responseContent = response.getContentAsString();
	assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 15
Source File: HtmlUnitRequestBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
	String charset = getCharset();
	String httpMethod = this.webRequest.getHttpMethod().name();
	UriComponents uriComponents = uriComponents();

	MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(servletContext, httpMethod,
			uriComponents.getPath());
	parent(request, this.parentBuilder);
	request.setServerName(uriComponents.getHost()); // needs to be first for additional headers
	authType(request);
	request.setCharacterEncoding(charset);
	content(request, charset);
	contextPath(request, uriComponents);
	contentType(request);
	cookies(request);
	headers(request);
	locales(request);
	servletPath(uriComponents, request);
	params(request, uriComponents);
	ports(uriComponents, request);
	request.setProtocol("HTTP/1.1");
	request.setQueryString(uriComponents.getQuery());
	request.setScheme(uriComponents.getScheme());
	pathInfo(uriComponents,request);

	return postProcess(request);
}
 
Example 16
Source File: HttpUtilTest.java    From yes-cart with Apache License 2.0 4 votes vote down vote up
@Test
public void testAllParameters() throws Exception {

    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/category/abc/sku/xyz");
    request.setQueryString("a=a1");
    request.addParameter("a", "a2");
    request.addParameter("b", "b1");

    Map<String, List<String>> parameters;
    Iterator<Map.Entry<String, List<String>>> parametersIt;
    Map.Entry<String, List<String>> p1, p2, p3, p4;

    parameters = HttpUtil.allParameters(request, Collections.emptySet());

    assertEquals(2, parameters.size());

    // Make sure order is preserved!!!!
    parametersIt = parameters.entrySet().iterator();

    p1 = parametersIt.next();
    assertEquals("a", p1.getKey());
    assertEquals(2, p1.getValue().size());
    assertEquals("a1", p1.getValue().get(0));
    assertEquals("a2", p1.getValue().get(1));

    p2 = parametersIt.next();
    assertEquals("b", p2.getKey());
    assertEquals(1, p2.getValue().size());
    assertEquals("b1", p2.getValue().get(0));

    parameters = HttpUtil.allParameters(request, new HashSet<>(Collections.singletonList("category")));

    assertEquals(3, parameters.size());

    // Make sure order is preserved!!!!
    parametersIt = parameters.entrySet().iterator();

    p1 = parametersIt.next();
    assertEquals("category", p1.getKey());
    assertEquals(1, p1.getValue().size());
    assertEquals("abc", p1.getValue().get(0));

    p2 = parametersIt.next();
    assertEquals("a", p2.getKey());
    assertEquals(2, p2.getValue().size());
    assertEquals("a1", p2.getValue().get(0));
    assertEquals("a2", p2.getValue().get(1));

    p3 = parametersIt.next();
    assertEquals("b", p3.getKey());
    assertEquals(1, p3.getValue().size());
    assertEquals("b1", p3.getValue().get(0));

    parameters = HttpUtil.allParameters(request, new HashSet<>(Arrays.asList("sku", "category")));

    assertEquals(4, parameters.size());

    // Make sure order is preserved!!!!
    parametersIt = parameters.entrySet().iterator();

    p1 = parametersIt.next();
    assertEquals("category", p1.getKey());
    assertEquals(1, p1.getValue().size());
    assertEquals("abc", p1.getValue().get(0));

    p2 = parametersIt.next();
    assertEquals("sku", p2.getKey());
    assertEquals(1, p2.getValue().size());
    assertEquals("xyz", p2.getValue().get(0));

    p3 = parametersIt.next();
    assertEquals("a", p3.getKey());
    assertEquals(2, p3.getValue().size());
    assertEquals("a1", p3.getValue().get(0));
    assertEquals("a2", p3.getValue().get(1));

    p4 = parametersIt.next();
    assertEquals("b", p4.getKey());
    assertEquals(1, p4.getValue().size());
    assertEquals("b1", p4.getValue().get(0));

}
 
Example 17
Source File: HttpUtilTest.java    From yes-cart with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetParameters2WithDuplicatesPreserve() throws Exception {

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/category/abc/sku/xyz/category/abc/sku/xyz");
    request.setQueryString("a=a1&a=a2&b=b1&a=a1&a=a2&b=b1");

    Map<String, List<String>> parameters;
    Iterator<Map.Entry<String, List<String>>> parametersIt;
    Map.Entry<String, List<String>> p1, p2, p3, p4;

    parameters = HttpUtil.getParameters(request, Collections.emptySet());

    assertEquals(2, parameters.size());

    // Make sure order is preserved!!!!
    parametersIt = parameters.entrySet().iterator();

    p1 = parametersIt.next();
    assertEquals("a", p1.getKey());
    assertEquals(4, p1.getValue().size());
    assertEquals("a1", p1.getValue().get(0));
    assertEquals("a2", p1.getValue().get(1));
    assertEquals("a1", p1.getValue().get(2));
    assertEquals("a2", p1.getValue().get(3));

    p2 = parametersIt.next();
    assertEquals("b", p2.getKey());
    assertEquals(2, p2.getValue().size());
    assertEquals("b1", p2.getValue().get(0));
    assertEquals("b1", p2.getValue().get(1));

    parameters = HttpUtil.getParameters(request, new HashSet<>(Collections.singletonList("category")));

    assertEquals(3, parameters.size());

    // Make sure order is preserved!!!!
    parametersIt = parameters.entrySet().iterator();

    p1 = parametersIt.next();
    assertEquals("category", p1.getKey());
    assertEquals(2, p1.getValue().size());
    assertEquals("abc", p1.getValue().get(0));
    assertEquals("abc", p1.getValue().get(1));

    p2 = parametersIt.next();
    assertEquals("a", p2.getKey());
    assertEquals(4, p2.getValue().size());
    assertEquals("a1", p2.getValue().get(0));
    assertEquals("a2", p2.getValue().get(1));
    assertEquals("a1", p2.getValue().get(2));
    assertEquals("a2", p2.getValue().get(3));

    p3 = parametersIt.next();
    assertEquals("b", p3.getKey());
    assertEquals(2, p3.getValue().size());
    assertEquals("b1", p3.getValue().get(0));
    assertEquals("b1", p3.getValue().get(1));

    parameters = HttpUtil.getParameters(request, new HashSet<>(Arrays.asList("sku", "category")));

    assertEquals(4, parameters.size());

    // Make sure order is preserved!!!!
    parametersIt = parameters.entrySet().iterator();

    p1 = parametersIt.next();
    assertEquals("category", p1.getKey());
    assertEquals(2, p1.getValue().size());
    assertEquals("abc", p1.getValue().get(0));
    assertEquals("abc", p1.getValue().get(1));

    p2 = parametersIt.next();
    assertEquals("sku", p2.getKey());
    assertEquals(2, p2.getValue().size());
    assertEquals("xyz", p2.getValue().get(0));
    assertEquals("xyz", p2.getValue().get(1));

    p3 = parametersIt.next();
    assertEquals("a", p3.getKey());
    assertEquals(4, p3.getValue().size());
    assertEquals("a1", p3.getValue().get(0));
    assertEquals("a2", p3.getValue().get(1));
    assertEquals("a1", p3.getValue().get(2));
    assertEquals("a2", p3.getValue().get(3));

    p4 = parametersIt.next();
    assertEquals("b", p4.getKey());
    assertEquals(2, p4.getValue().size());
    assertEquals("b1", p4.getValue().get(0));
    assertEquals("b1", p4.getValue().get(1));

}
 
Example 18
Source File: EntityTypeResponseMapperTest.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
void toEntityTypesResponse() throws URISyntaxException {
  MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
  mockHttpServletRequest.setMethod("GET");
  mockHttpServletRequest.setRequestURI("/api/metadata");
  mockHttpServletRequest.setQueryString("page=1");
  RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest));

  EntityType entityType = mock(EntityType.class);
  when(entityType.getId()).thenReturn("MyEntityTypeId");

  int total = 5;
  EntityTypes entityTypes =
      EntityTypes.builder().setEntityTypes(singletonList(entityType)).setTotal(total).build();

  int size = 1;
  int number = 1;

  EntityTypeResponseData entityTypeResponseData =
      EntityTypeResponseData.builder()
          .setId("MyEntityTypeId")
          .setAttributes(
              AttributesResponse.builder()
                  .setLinks(
                      LinksResponse.create(
                          null,
                          new URI("http://localhost/api/metadata/MyEntityTypeId/attributes"),
                          null))
                  .build())
          .setAbstract(false)
          .setIndexingDepth(0)
          .build();

  EntityTypeResponse entityTypeResponse =
      EntityTypeResponse.builder()
          .setLinks(
              LinksResponse.create(
                  null, new URI("http://localhost/api/metadata/MyEntityTypeId"), null))
          .setData(entityTypeResponseData)
          .build();

  EntityTypesResponse entityTypesResponse =
      EntityTypesResponse.builder()
          .setLinks(
              LinksResponse.create(
                  new URI("http://localhost/api/metadata?page=0"),
                  new URI("http://localhost/api/metadata?page=1"),
                  new URI("http://localhost/api/metadata?page=2")))
          .setItems(singletonList(entityTypeResponse))
          .setPage(PageResponse.create(size, total, number))
          .build();
  assertEquals(
      entityTypesResponse, entityTypeV3Mapper.toEntityTypesResponse(entityTypes, size, number));
}
 
Example 19
Source File: HttpUtilTest.java    From yes-cart with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetParameters2() throws Exception {

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/category/abc/sku/xyz");
    request.setQueryString("a=a1&a=a2&b=b1");

    Map<String, List<String>> parameters;
    Iterator<Map.Entry<String, List<String>>> parametersIt;
    Map.Entry<String, List<String>> p1, p2, p3, p4;

    parameters = HttpUtil.getParameters(request, Collections.emptySet());

    assertEquals(2, parameters.size());

    // Make sure order is preserved!!!!
    parametersIt = parameters.entrySet().iterator();

    p1 = parametersIt.next();
    assertEquals("a", p1.getKey());
    assertEquals(2, p1.getValue().size());
    assertEquals("a1", p1.getValue().get(0));
    assertEquals("a2", p1.getValue().get(1));

    p2 = parametersIt.next();
    assertEquals("b", p2.getKey());
    assertEquals(1, p2.getValue().size());
    assertEquals("b1", p2.getValue().get(0));

    parameters = HttpUtil.getParameters(request, new HashSet<>(Collections.singletonList("category")));

    assertEquals(3, parameters.size());

    // Make sure order is preserved!!!!
    parametersIt = parameters.entrySet().iterator();

    p1 = parametersIt.next();
    assertEquals("category", p1.getKey());
    assertEquals(1, p1.getValue().size());
    assertEquals("abc", p1.getValue().get(0));

    p2 = parametersIt.next();
    assertEquals("a", p2.getKey());
    assertEquals(2, p2.getValue().size());
    assertEquals("a1", p2.getValue().get(0));
    assertEquals("a2", p2.getValue().get(1));

    p3 = parametersIt.next();
    assertEquals("b", p3.getKey());
    assertEquals(1, p3.getValue().size());
    assertEquals("b1", p3.getValue().get(0));

    parameters = HttpUtil.getParameters(request, new HashSet<>(Arrays.asList("sku", "category")));

    assertEquals(4, parameters.size());

    // Make sure order is preserved!!!!
    parametersIt = parameters.entrySet().iterator();

    p1 = parametersIt.next();
    assertEquals("category", p1.getKey());
    assertEquals(1, p1.getValue().size());
    assertEquals("abc", p1.getValue().get(0));

    p2 = parametersIt.next();
    assertEquals("sku", p2.getKey());
    assertEquals(1, p2.getValue().size());
    assertEquals("xyz", p2.getValue().get(0));

    p3 = parametersIt.next();
    assertEquals("a", p3.getKey());
    assertEquals(2, p3.getValue().size());
    assertEquals("a1", p3.getValue().get(0));
    assertEquals("a2", p3.getValue().get(1));

    p4 = parametersIt.next();
    assertEquals("b", p4.getKey());
    assertEquals(1, p4.getValue().size());
    assertEquals("b1", p4.getValue().get(0));

}
 
Example 20
Source File: OpenURLServiceImplTest.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
private MockHttpServletRequest createRequest(String openUrl) {
	MockHttpServletRequest req = new MockHttpServletRequest("GET", openUrl);
	req.setQueryString(openUrl);
	req.setParameters(parseQueryString(openUrl));
	return req;
}