CameraUrlGenerator { @VisibleForTesting static String addHttpParams(String input, Map<String, String> commands) { if (commands.isEmpty()) { return input; } URI uri; try { uri = new URI(input); } catch (URISyntaxException ex) { return input; } String query = uri.getQuery(); if (query != null) { String niName = commands.get("name"); String[] existingCommands = query.split("&"); for (String command : existingCommands) { String[] commandSplit = command.split("="); if (commandSplit.length != 2) { continue; } commands.put(URLDecoder.decode(commandSplit[0], StandardCharsets.UTF_8), URLDecoder.decode(commandSplit[1], StandardCharsets.UTF_8)); } if (niName != null) { commands.put("name", niName); } } var queryStr = commands.entrySet().stream() .map(CameraUrlGenerator::httpUrlEncode) .collect(Collectors.joining("&")); return encodeUri(uri, queryStr); } CameraUrlGenerator(CameraServerSource source); String[] generateUrls(String[] baseUrls);  }
@Test public void testGenerateHttpParams() { Map<String, String> commands = new HashMap<>(); String baseUrl = "http: String params = CameraUrlGenerator.addHttpParams(baseUrl, commands); assertEquals(baseUrl, params); commands.put("foo", "bar"); params = CameraUrlGenerator.addHttpParams(baseUrl, commands); assertEquals(baseUrl + "?foo=bar", params); commands.put("baz", "buq"); params = CameraUrlGenerator.addHttpParams(baseUrl, commands); assertEquals(baseUrl + "?foo=bar&baz=buq", params); }