URIBuilder { public static String buildURIFromPattern(String pattern, List<Parameter> parameters) { if (parameters != null) { for (Parameter parameter : parameters) { String wadltemplate = "{" + parameter.getName() + "}"; String swaggerTemplate = "/:" + parameter.getName(); if (pattern.contains(wadltemplate)) { pattern = pattern.replace(wadltemplate, encodePath(parameter.getValue())); } else if (pattern.contains(swaggerTemplate)) { pattern = pattern.replace(":" + parameter.getName(), encodePath(parameter.getValue())); } else { if (!pattern.contains("?")) { pattern += "?"; } if (pattern.contains("=")) { pattern += "&"; } pattern += parameter.getName() + "=" + encodeValue(parameter.getValue()); } } } return pattern; }  static String buildURIFromPattern(String pattern, List<Parameter> parameters); static String buildURIFromPattern(String pattern, Map<String, String> parameters);  }
@Test public void testBuildURIFromPatternWithNoParameters() { String pattern = "http: try { String uri = URIBuilder.buildURIFromPattern(pattern, new ArrayList<Parameter>()); } catch (NullPointerException npe) { fail("buildURIFromPattern should not fail with no parameters"); } }
@Test public void testBuildURIFromPattern() { Parameter yearParam = new Parameter(); yearParam.setName("year"); yearParam.setValue("2017"); Parameter monthParam = new Parameter(); monthParam.setName("month"); monthParam.setValue("08"); Parameter statusParam = new Parameter(); statusParam.setName("status"); statusParam.setValue("published"); Parameter pageParam = new Parameter(); pageParam.setName("page"); pageParam.setValue("0"); List<Parameter> parameters = new ArrayList<>(); parameters.add(yearParam); parameters.add(monthParam); parameters.add(statusParam); parameters.add(pageParam); String pattern = "http: String uri = URIBuilder.buildURIFromPattern(pattern, parameters); assertTrue("http: || "http: pattern = "http: uri = URIBuilder.buildURIFromPattern(pattern, parameters); assertTrue("http: || "http: }
@Test public void testBuildURIFromPatternWithEncoding() { Parameter nameParam = new Parameter(); nameParam.setName("name"); nameParam.setValue("Eclair Cafe"); Parameter descriptionParam = new Parameter(); descriptionParam.setName("description"); descriptionParam.setValue("My desc"); List<Parameter> parameters = new ArrayList<>(); parameters.add(nameParam); parameters.add(descriptionParam); String pattern = "http: String uri = URIBuilder.buildURIFromPattern(pattern, parameters); assertTrue("http: pattern = "http: uri = URIBuilder.buildURIFromPattern(pattern, parameters); assertTrue("http: }
@Test public void testBuildURIFromPatternWithMap() { Map<String, String> parameters = new HashMap<>(); parameters.put("year", "2018"); parameters.put("month", "05"); parameters.put("status", "published"); parameters.put("page", "0"); String pattern = "http: String uri = URIBuilder.buildURIFromPattern(pattern, parameters); assertTrue("http: || "http: pattern = "http: uri = URIBuilder.buildURIFromPattern(pattern, parameters); assertTrue("http: || "http: }