List of usage examples for org.springframework.mock.web MockMultipartHttpServletRequest setParameter
public void setParameter(String name, String value)
From source file:ch.ralscha.extdirectspring.bean.ExtDirectResponseBuilderTest.java
@Test public void testBuilderUploadResponse() throws IOException { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setParameter("extAction", "action"); request.setParameter("extMethod", "method"); request.setParameter("extType", "type"); request.setParameter("extTID", "1"); MockHttpServletResponse servletResponse = new MockHttpServletResponse(); ExtDirectResponseBuilder.create(request, servletResponse) .addResultProperty("additionalProperty", Boolean.FALSE) .addResultProperty("text", "a lot of "text"").buildAndWrite(); assertThat(servletResponse.getContentType()).isEqualTo("text/html;charset=UTF-8"); String content = servletResponse.getContentAsString(); assertThat(servletResponse.getContentLength()).isEqualTo(content.getBytes("UTF-8").length); assertThat(content).startsWith("<html><body><textarea>"); assertThat(content).endsWith("</textarea></body></html>"); String json = content.substring(content.indexOf("{"), content.lastIndexOf("}") + 1); assertThat(json).contains("\\""); json = json.replace("\\"", "\'"); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> header = mapper.readValue(json, Map.class); assertThat(header.get("action")).isEqualTo("action"); assertThat(header.get("method")).isEqualTo("method"); assertThat(header.get("type")).isEqualTo("type"); assertThat(header.get("tid")).isEqualTo(1); @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) header.get("result"); assertThat(result).hasSize(3);//from w w w . j a v a2 s. c o m assertThat((Boolean) result.get("success")).isTrue(); assertThat(result.get("text")).isEqualTo("a lot of 'text'"); assertThat(result.get("additionalProperty")).isEqualTo(Boolean.FALSE); }
From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java
public void createTaxonGroups(int count, int rand, boolean createAttributes) throws Exception { if (rand > 0) { count = count + (random.nextBoolean() ? random.nextInt(rand) : -random.nextInt(rand)); }/* ww w . ja v a 2s.co m*/ log.info(String.format("Creating %d Taxon Groups", count)); // Look for the directory of images, if found we will use that // otherwise we will use a generated set of images. byte[] defaultThumbnail = createImage(250, 140, "Test Taxon Group Thumb"); byte[] defaultImage = createImage(640, 480, "Test Taxon Group Image"); Map<String, byte[]> defaultImageMap = new HashMap<String, byte[]>(); defaultImageMap.put("image", defaultImage); defaultImageMap.put("thumbNail", defaultThumbnail); Preference testDataDirPref = prefDAO.getPreferenceByKey(TEST_DATA_IMAGE_DIR); MockMultipartHttpServletRequest request; MockHttpServletResponse response; for (int i = 0; i < count; i++) { request = new MockMultipartHttpServletRequest(); response = new MockHttpServletResponse(); request.setMethod("POST"); request.setRequestURI("/bdrs/admin/taxongroup/edit.htm"); request.setParameter("name", TEST_GROUPS[random.nextInt(TEST_GROUPS.length)]); request.setParameter("behaviourIncluded", String.valueOf(random.nextBoolean())); request.setParameter("firstAppearanceIncluded", String.valueOf(random.nextBoolean())); request.setParameter("lastAppearanceIncluded", String.valueOf(random.nextBoolean())); request.setParameter("habitatIncluded", String.valueOf(random.nextBoolean())); request.setParameter("weatherIncluded", String.valueOf(random.nextBoolean())); request.setParameter("numberIncluded", String.valueOf(random.nextBoolean())); // Image and Thumbnail for (String propertyName : new String[] { "image", "thumbNail" }) { String key = String.format("%s_file", propertyName); String image_filename = String.format("%s_filename.png", propertyName); byte[] imageData = getRandomImage(testDataDirPref, 250, 140); imageData = imageData == null ? defaultImageMap.get(propertyName) : imageData; MockMultipartFile mockImageFile = new MockMultipartFile(key, image_filename, "image/png", imageData); ((MockMultipartHttpServletRequest) request).addFile(mockImageFile); request.setParameter(propertyName, image_filename); } // Attributes if (createAttributes) { int curWeight = 0; String attributeOptions = "Option A, Option B, Option C, Option D"; String rangeIntOptions = "0, 50"; int index = 0; for (Boolean isTag : new Boolean[] { true, false }) { for (AttributeType attrType : AttributeType.values()) { request.addParameter("add_attribute", String.valueOf(index)); request.setParameter(String.format("add_weight_%d", index), String.valueOf(curWeight)); request.setParameter(String.format("add_name_%d", index), String.format("%s name%s", attrType.getName(), isTag ? " Tag" : "")); request.setParameter(String.format("add_description_%d", index), String.format("%s description%s", attrType.getName(), isTag ? " Tag" : "")); request.setParameter(String.format("add_typeCode_%d", index), attrType.getCode()); request.setParameter(String.format("add_tag_%d", index), isTag.toString().toLowerCase()); //request.setParameter(String.format("add_scope_%d", index), scope.toString()); if (AttributeType.STRING_WITH_VALID_VALUES.equals(attrType)) { request.setParameter(String.format("add_option_%d", index), attributeOptions); } else if (AttributeType.INTEGER_WITH_RANGE.equals(attrType)) { request.setParameter(String.format("add_option_%d", index), rangeIntOptions); } index = index + 1; curWeight = curWeight + 100; } } } handle(request, response); } }
From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java
public void createTaxonProfile() throws Exception { log.info(String.format("Creating Taxon Profiles")); TaxonomyManagementController controller = appContext.getBean(TaxonomyManagementController.class); Preference testDataDirPref = prefDAO.getPreferenceByKey(TEST_DATA_IMAGE_DIR); MockMultipartHttpServletRequest request; MockHttpServletResponse response;//from w w w . j a v a 2s . c om List<SpeciesProfile> profileList; for (IndicatorSpecies species : taxaDAO.getIndicatorSpecies()) { profileList = controller.loadSpeciesProfileTemplate(species.getTaxonGroup(), species.getInfoItems()); request = new MockMultipartHttpServletRequest(); response = new MockHttpServletResponse(); request.setMethod("POST"); request.setRequestURI("/bdrs/admin/taxonomy/edit.htm"); request.setParameter("taxonPk", species.getId().toString()); request.setParameter("scientificName", species.getScientificName()); request.setParameter("commonName", species.getCommonName()); request.setParameter("taxonRank", species.getTaxonRank().toString()); request.setParameter("parentPk", species.getParent() == null ? "" : species.getParent().getId().toString()); request.setParameter("taxonGroupPk", species.getTaxonGroup().getId().toString()); request.setParameter("author", species.getAuthor() == null ? "" : species.getAuthor()); request.setParameter("year", species.getYear() == null ? "" : species.getYear()); int index = 0; int weight = 0; File speciesImage = getRandomFile(testDataDirPref, new WebImageFileFilter()); for (SpeciesProfile profile : profileList) { request.addParameter("new_profile", String.valueOf(index)); request.setParameter(String.format("new_profile_type_%d", index), profile.getType()); request.setParameter(String.format("new_profile_content_%d", index), generateSpeciesProfileData(profile, testDataDirPref, speciesImage, request)); request.setParameter(String.format("new_profile_description_%d", index), profile.getDescription()); request.setParameter(String.format("new_profile_header_%d", index), profile.getHeader()); request.setParameter(String.format("new_profile_weight_%d", index), String.valueOf(weight)); weight = weight + 100; index = index + 1; } handle(request, response); } }
From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java
public void createTaxa(int count, int rand) throws Exception { MockMultipartHttpServletRequest request; MockHttpServletResponse response;//from w ww.j a va2 s . c om if (rand > 0) { count = count + (random.nextBoolean() ? random.nextInt(rand) : -random.nextInt(rand)); } log.info(String.format("Creating %d Taxa", count)); Map<TaxonGroup, String> groupToGenusMap = new HashMap<TaxonGroup, String>(); String genusName; for (TaxonGroup taxonGroup : taxaDAO.getTaxonGroups()) { genusName = generateScientificName(TaxonRank.GENUS); groupToGenusMap.put(taxonGroup, genusName); for (int i = 0; i < count; i++) { request = new MockMultipartHttpServletRequest(); response = new MockHttpServletResponse(); request.setMethod("POST"); request.setRequestURI("/bdrs/admin/taxonomy/edit.htm"); TaxonRank rank = TaxonRank.SPECIES; request.setParameter("taxonRank", rank.toString()); String scientificName = generateScientificName(rank, genusName); request.setParameter("scientificName", scientificName); request.setParameter("commonName", generateCommonName(rank, scientificName)); request.setParameter("parentPk", ""); request.setParameter("taxonGroupPk", taxonGroup.getId().toString()); request.setParameter("author", generateRandomPersonName()); request.setParameter("year", String.valueOf(1700 + random.nextInt(2011 - 1700))); createTaxonAttributes(request, taxonGroup); handle(request, response); } } TaxonGroup life = taxaDAO.getTaxonGroups().get(0); List<IndicatorSpecies> genusList = new ArrayList<IndicatorSpecies>(); for (TaxonGroup taxonGroup : taxaDAO.getTaxonGroups()) { TaxonRank rank = TaxonRank.GENUS; IndicatorSpecies genus = new IndicatorSpecies(); genus.setScientificName(groupToGenusMap.get(taxonGroup)); genus.setCommonName(generateCommonName(rank, genus.getScientificName())); genus.setTaxonGroup(life); genus.setTaxonRank(rank); genus.setAuthor(generateRandomPersonName()); genus.setYear(String.valueOf(1700 + random.nextInt(2011 - 1700))); genus = taxaDAO.save(genus); genus = createTaxonAttributes(genus); genusList.add(genus); for (IndicatorSpecies species : taxaDAO.getIndicatorSpecies(taxonGroup)) { species.setParent(genus); taxaDAO.save(species); } } IndicatorSpecies previousTaxon = null; for (int i = 0; i < TAXON_RANKS.length - 2; i++) { IndicatorSpecies taxon = new IndicatorSpecies(); taxon.setScientificName(generateScientificName(TAXON_RANKS[i])); taxon.setCommonName(generateCommonName(TAXON_RANKS[i], taxon.getScientificName())); taxon.setTaxonGroup(life); taxon.setParent(previousTaxon); taxon.setTaxonRank(TAXON_RANKS[i]); taxon.setAuthor(generateRandomPersonName()); taxon.setYear(String.valueOf(1700 + random.nextInt(2011 - 1700))); taxon = taxaDAO.save(taxon); taxon = createTaxonAttributes(taxon); previousTaxon = taxon; } for (IndicatorSpecies genus : genusList) { genus.setParent(previousTaxon); taxaDAO.save(genus); } }
From source file:org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilterTests.java
public void contentLengthTest(Long expectedContentLength, SimpleHostRoutingFilter filter, MockMultipartHttpServletRequest request) throws IOException { byte[] data = "poprqwueproqiwuerpoqweiurpo".getBytes(); MockMultipartFile file = new MockMultipartFile("test.zip", "test.zip", "application/zip", data); String boundary = "q1w2e3r4t5y6u7i8o9"; request.setContentType("multipart/form-data; boundary=" + boundary); request.setContent(createFileContent(data, boundary, "application/zip", "test.zip")); request.addFile(file);/*from www.j ava2 s. c om*/ request.setMethod("POST"); request.setParameter("variant", "php"); request.setParameter("os", "mac"); request.setParameter("version", "3.4"); request.setRequestURI("/app/echo"); MockHttpServletResponse response = new MockHttpServletResponse(); RequestContext.getCurrentContext().setRequest(request); RequestContext.getCurrentContext().setResponse(new MockHttpServletResponse()); URL url = new URL("http://localhost:" + this.port); RequestContext.getCurrentContext().set("routeHost", url); filter.run(); String responseString = IOUtils.toString( new GZIPInputStream(((CloseableHttpResponse) RequestContext.getCurrentContext().get("zuulResponse")) .getEntity().getContent())); assertTrue(!responseString.isEmpty()); if (expectedContentLength < 0) { assertThat(responseString, containsString("\"" + HttpHeaders.TRANSFER_ENCODING.toLowerCase() + "\":\"chunked\"")); assertThat(responseString, not(containsString(HttpHeaders.CONTENT_LENGTH.toLowerCase()))); } else { assertThat(responseString, containsString( "\"" + HttpHeaders.CONTENT_LENGTH.toLowerCase() + "\":\"" + expectedContentLength + "\"")); } }