MermaidQueryFormatter { public static String format(InsituQuery query) { List<String> queryParams= new ArrayList<>(); if(query.lonMin() != null) { queryParams.add(PARAM_LON_MIN + "=" + query.lonMin()); } if(query.latMin() != null) { queryParams.add(PARAM_LAT_MIN + "=" + query.latMin()); } if(query.lonMax() != null) { queryParams.add(PARAM_LON_MAX + query.lonMax()); } if(query.latMax() != null) { queryParams.add(PARAM_LAT_MAX + "=" + query.latMax()); } if(query.startDate() != null) { try { queryParams.add(PARAM_START_DATE + "=" + URLEncoder.encode(DATE_FORMAT.format(query.startDate()), "UTF-8")); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("start date could not be encoded", e); } } if(query.stopDate() != null) { try { queryParams.add(PARAM_STOP_DATE + "=" + URLEncoder.encode(DATE_FORMAT.format(query.stopDate()), "UTF-8")); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("stop date could not be encoded", e); } } if(query.param() != null && query.param().length > 0) { queryParams.add(PARAM_PARAM + "=" + StringUtils.arrayToCsv(query.param())); } if(query.datasets() != null && query.datasets().length > 0) { queryParams.add(PARAM_CAMPAIGN + "=" + StringUtils.arrayToCsv(query.datasets())); } if(query.shift() > 0) { queryParams.add(PARAM_SHIFT + "=" + query.shift()); } if(query.limit() > 0) { queryParams.add(PARAM_LIMIT + "=" + query.limit()); } if(query.countOnly()) { queryParams.add(PARAM_COUNT_ONLY); } if(query.subject() == null) { throw new IllegalArgumentException("subject must be specified"); } final StringBuilder sb = new StringBuilder(); sb.append("/"); switch (query.subject()) { case DATASETS: sb.append(CAMPAIGNS_SUBJECT); break; case PARAMETERS: sb.append(PARAMETERS_SUBJECT); break; case OBSERVATIONS: sb.append(OBSERVATIONS_SUBJECT); break; } sb.append("?"); for (int i = 0; i < queryParams.size(); i++) { String queryParam = queryParams.get(i); sb.append(queryParam); if(i < queryParams.size()-1) { sb.append("&"); } } return sb.toString(); }  static String format(InsituQuery query);  }
@Test public void testQueryCreation() throws Exception { final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); final InsituQuery query= new InsituQuery(); query.subject(InsituQuery.SUBJECT.PARAMETERS); query.datasets(new String[]{"Muscheln", "Robben"}); query.latMin(-10.943); query.latMax(46.12); query.lonMin(5.0); query.lonMax(15.36); query.startDate(dateFormat.parse("01-Jan-2014 00:00:00")); query.stopDate(dateFormat.parse("31-Dec-2015 00:00:00")); query.param(new String[]{"param1", "param2", "param3"}); query.limit(10); query.shift(5); query.countOnly(true); String queryString = MermaidQueryFormatter.format(query); assertTrue(queryString.startsWith("/parameters?")); assertTrue(queryString.contains("campaign=Muscheln,Robben")); assertTrue(queryString.contains("lat_min=-10.943")); assertTrue(queryString.contains("lon_min=5.0")); assertTrue(queryString.contains("lat_max=46.12")); assertTrue(queryString.contains("lon_max=15.36")); assertTrue(queryString.contains("start_date=2014-01-01+00%3A00%3A00")); assertTrue(queryString.contains("stop_date=2015-12-31+00%3A00%3A00")); assertTrue(queryString.contains("param=param1,param2,param3")); assertTrue(queryString.contains("limit=10")); assertTrue(queryString.contains("shift=5")); assertTrue(queryString.contains("count_only")); assertEquals(10, queryString.chars().filter(value -> value == '&').count()); final int lastIndex = queryString.length() - 1; assertTrue(queryString.charAt(lastIndex) != '&'); }