LayerValidator { public static void validateAndSanitizeLayerInput(MapLayer input) throws IllegalArgumentException { if (input == null) { throw new IllegalArgumentException("Layer data missing"); } if (!hasValue(input.getType())) { throw new IllegalArgumentException("Required field missing 'type'"); } Set<String> recognizedTypes = getRecognizedLayerTypes(); if (!recognizedTypes.contains(input.getType())) { throw new IllegalArgumentException("Layer type is not recognized: " + input.getType() + ". Recognized types are: " + LOG.getAsString(recognizedTypes)); } Set<String> mandatoryFields = getMandatoryFields(input.getType()); for (String field: mandatoryFields) { if (!hasValue(input, field)) { throw new IllegalArgumentException("Required field missing '" + field + "'"); } } if (mandatoryFields.contains("url")) { input.setUrl(LayerValidator.sanitizeUrl(input.getUrl())); } if (input.getVersion() == null) { input.setVersion(""); } input.setLocale(LayerValidator.validateLocale(input.getLocale())); input.setGfi_content(LayerValidator.sanitizeGFIContent(input.getGfi_content())); }  static Map<String, Map<String, String>> validateLocale(Map<String, Map<String, String>> locale); static String sanitizeUrl(final String url); static String sanitizeUrl(final String url, Set<String> paramsToRemove); static String sanitizeGFIContent(String gfiContent); static Set<String> getMandatoryFields(String type); static Set<String> getRecognizedLayerTypes(); static void validateAndSanitizeLayerInput(MapLayer input);  }
@Test public void validateWMSLayer() { MapLayer input = new MapLayer(); try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Required field missing 'type'", e.getMessage()); } input.setType(OskariLayer.TYPE_WMS); try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Required field missing 'name'", e.getMessage()); } input.setName("testing"); try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Required field missing 'url'", e.getMessage()); } input.setUrl("http: try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Required field missing 'version'", e.getMessage()); } input.setVersion("1.1.0"); try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Localization for layer names missing", e.getMessage()); } input.setLocale(new HashMap<>()); try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Name missing for default language: en", e.getMessage()); } input.setLocale(createValidLocaleForLayer()); LayerValidator.validateAndSanitizeLayerInput(input); }
@Test public void validateBingLayer() { MapLayer input = new MapLayer(); input.setType(OskariLayer.TYPE_BINGLAYER); try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Required field missing 'options.apiKey'", e.getMessage()); } input.setOptions(new HashMap<>()); try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Required field missing 'options.apiKey'", e.getMessage()); } input.setLocale(createValidLocaleForLayer()); HashMap jee = new HashMap<>(); jee.put("apiKey", "testing"); input.setOptions(jee); LayerValidator.validateAndSanitizeLayerInput(input); }