diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..7c10be4 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,22 @@ +name: build and analyse + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + java-version: [ 11, 17 ] + + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: ${{ matrix.java-version }} + cache: 'maven' + - run: mvn -B install --no-transfer-progress + - name: Analyse code and publish to SonarQube 📊 + run: mvn -B -Dsonar.password=${{ secrets.SONAR_PASSWORD }} sonar:sonar diff --git a/pom.xml b/pom.xml index 292508f..a603ece 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,13 @@ + + UTF-8 + https://sq.terrestris.de + terrestris + src/main/ + + reporting diff --git a/src/main/java/de/terrestris/shogun/migrator/shogun2/BootMigrator.java b/src/main/java/de/terrestris/shogun/migrator/shogun2/BootMigrator.java index 914cec9..fba663f 100644 --- a/src/main/java/de/terrestris/shogun/migrator/shogun2/BootMigrator.java +++ b/src/main/java/de/terrestris/shogun/migrator/shogun2/BootMigrator.java @@ -17,13 +17,17 @@ @Log4j2 public class BootMigrator implements ShogunMigrator { + public static final String LAYER_ID = "layerId"; + + public static final String BACKGROUND_LAYERS = "backgroundLayers"; + private HostDto source; private HostDto target; private static void migrateLayerTree(ObjectNode node, Map idMap) { - if (node.has("layerId")) { - node.put("layerId", idMap.get(node.get("layerId").intValue())); + if (node.has(LAYER_ID)) { + node.put(LAYER_ID, idMap.get(node.get(LAYER_ID).intValue())); } if (node.has("children")) { for (JsonNode jsonNode : node.get("children")) { @@ -38,12 +42,12 @@ public static byte[] migrateApplication(ObjectNode node, Map i log.info("Migrating application {}", node.get("name").asText()); JsonNode clientConfig = node.get("clientConfig"); - if (clientConfig.has("backgroundLayers")) { + if (clientConfig.has(BACKGROUND_LAYERS)) { ArrayNode backgroundLayers = mapper.createArrayNode(); - for (JsonNode jsonNode : clientConfig.get("backgroundLayers")) { + for (JsonNode jsonNode : clientConfig.get(BACKGROUND_LAYERS)) { backgroundLayers.add(idMap.get(jsonNode.asInt())); } - ((ObjectNode) clientConfig).set("backgroundLayers", backgroundLayers); + ((ObjectNode) clientConfig).set(BACKGROUND_LAYERS, backgroundLayers); } JsonNode layerTree = node.get("layerTree"); migrateLayerTree((ObjectNode) layerTree, idMap); diff --git a/src/main/java/de/terrestris/shogun/migrator/shogun2/Shogun2Migrator.java b/src/main/java/de/terrestris/shogun/migrator/shogun2/Shogun2Migrator.java index cd18c8d..5226174 100644 --- a/src/main/java/de/terrestris/shogun/migrator/shogun2/Shogun2Migrator.java +++ b/src/main/java/de/terrestris/shogun/migrator/shogun2/Shogun2Migrator.java @@ -19,6 +19,9 @@ @Log4j2 public class Shogun2Migrator implements ShogunMigrator { + public static final String CHILDREN = "children"; + public static final String RESOLUTIONS = "resolutions"; + public static final String SEARCHABLE = "searchable"; private HostDto source; private HostDto target; @@ -55,10 +58,10 @@ private static JsonNode migrateLayerTree(JsonNode node, ObjectMapper mapper, Map } folder.put("layerId", idMap.get(id)); } - if (node.has("children")) { + if (node.has(CHILDREN)) { ArrayNode children = mapper.createArrayNode(); - folder.set("children", children); - for (JsonNode child : node.get("children")) { + folder.set(CHILDREN, children); + for (JsonNode child : node.get(CHILDREN)) { children.add(migrateLayerTree(child, mapper, idMap)); } } @@ -94,12 +97,12 @@ public static byte[] migrateApplication(JsonNode node, Map idM mapView.set("mapExtent", extent); String oldProjection = mapConfig.get("projection").asText(); mapView.put("projection", oldProjection.startsWith("EPSG:") ? oldProjection : ("EPSG:" + oldProjection)); - JsonNode resolutions = mapConfig.get("resolutions"); + JsonNode resolutions = mapConfig.get(RESOLUTIONS); ArrayNode newResolutions = mapper.createArrayNode(); for (JsonNode res : resolutions) { newResolutions.add(res.asDouble()); } - mapView.set("resolutions", newResolutions); + mapView.set(RESOLUTIONS, newResolutions); clientConfig.set("mapView", mapView); } JsonNode layerTree = migrateLayerTree(node.get("layerTree"), mapper, idMap); @@ -137,9 +140,9 @@ private static void migrateClientConfig(JsonNode node, ObjectNode config) { JsonNode appearance = node.get("appearance"); config.put("minResolution", appearance.get("minResolution").textValue()); config.put("maxResolution", appearance.get("maxResolution").textValue()); - JsonNode searchable = node.get("searchable"); + JsonNode searchable = node.get(SEARCHABLE); if (searchable != null && searchable.booleanValue()) { - config.put("searchable", searchable.booleanValue()); + config.put(SEARCHABLE, searchable.booleanValue()); JsonNode oldConfig = node.get("searchConfig"); ObjectNode searchConfig = mapper.createObjectNode(); config.set("searchConfig", searchConfig); @@ -152,7 +155,7 @@ private static void migrateClientConfig(JsonNode node, ObjectNode config) { oldConfig.get("attributes").forEach(attribute -> attributes.add(attribute.textValue())); searchConfig.set("attributes", attributes); } else { - config.put("searchable", false); + config.put(SEARCHABLE, false); } } @@ -172,7 +175,7 @@ private static void migrateSourceConfig(JsonNode node, ObjectNode config) { JsonNode oldResolutions = tileGrid.get("tileGridResolutions"); ArrayNode resolutions = mapper.createArrayNode(); oldResolutions.forEach(resolution -> resolutions.add(resolution.doubleValue())); - config.set("resolutions", resolutions); + config.set(RESOLUTIONS, resolutions); } } @@ -218,7 +221,7 @@ public Map migrateLayers() { try { JsonNode node = fetch(source, "rest/projectlayers"); Map layerIdMap = new HashMap<>(); - int i = 0; +// int i = 0; for (JsonNode layer : node) { log.info("Migrating layer..."); byte[] bs = migrateLayer(layer); @@ -243,7 +246,7 @@ public Map migrateLayers() { public void migrateApplications(Map idMap) { try { JsonNode node = fetch(source, "rest/projectapps"); - int i = 0; +// int i = 0; for (JsonNode app : node) { log.info("Migrating application..."); byte[] bs = migrateApplication(app, idMap); diff --git a/src/main/java/de/terrestris/shogun/migrator/util/ApiUtil.java b/src/main/java/de/terrestris/shogun/migrator/util/ApiUtil.java index a253d72..daa1b13 100644 --- a/src/main/java/de/terrestris/shogun/migrator/util/ApiUtil.java +++ b/src/main/java/de/terrestris/shogun/migrator/util/ApiUtil.java @@ -18,10 +18,8 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; -import org.apache.logging.log4j.core.util.IOUtils; import java.io.IOException; -import java.io.InputStreamReader; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -32,6 +30,10 @@ @Log4j2 public class ApiUtil { + private ApiUtil() { + // prevent instantiation + } + public static JsonNode fetch(HostDto host, String resource) throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException { ObjectMapper mapper = new ObjectMapper(); @@ -81,11 +83,10 @@ public static void delete(HostDto host, String resource) throws IOException, Key } } - public static int saveLayer(byte[] bs, HostDto host) - throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, IOException { + private static JsonNode saveEntity(HostDto host, byte[] bs, String entity) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException { ObjectMapper mapper = new ObjectMapper(); - HttpPost post = new HttpPost(host.getHostname() + "layers"); - log.info("Saving layer..."); + HttpPost post = new HttpPost(host.getHostname() + entity + "s"); + log.info("Saving {}...", entity); try (CloseableHttpClient client = HttpClients.custom() .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build()) .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) @@ -100,35 +101,21 @@ public static int saveLayer(byte[] bs, HostDto host) } post.addHeader(header); post.setEntity(new ByteArrayEntity(bs, APPLICATION_JSON)); - CloseableHttpResponse response = client.execute(post); - JsonNode result = mapper.readTree(response.getEntity().getContent()); - response.close(); - return result.get("id").intValue(); + try (CloseableHttpResponse response = client.execute(post)) { + return mapper.readTree(response.getEntity().getContent()); + } } } + public static int saveLayer(byte[] bs, HostDto host) + throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, IOException { + JsonNode result = saveEntity(host, bs, "layer"); + return result.get("id").intValue(); + } + public static void saveApplication(byte[] bs, HostDto host) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, IOException { - HttpPost post = new HttpPost(host.getHostname() + "applications"); - log.info("Saving application..."); - try (CloseableHttpClient client = HttpClients.custom() - .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build()) - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .build()) { - UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(host.getUsername(), host.getPassword()); - Header header = null; - try { - header = new BasicScheme(UTF_8).authenticate(credentials, post, null); - } catch (AuthenticationException e) { - log.error("Error creating authentication: {}", e.getMessage()); - log.trace("Stack trace:", e); - } - post.addHeader(header); - post.setEntity(new ByteArrayEntity(bs, APPLICATION_JSON)); - CloseableHttpResponse response = client.execute(post); - log.info("Response was {}", IOUtils.toString(new InputStreamReader(response.getEntity().getContent(), UTF_8))); - response.close(); - } + saveEntity(host, bs, "application"); } } diff --git a/src/test/java/de/terrestris/shogun/migrator/MigratorTest.java b/src/test/java/de/terrestris/shogun/migrator/MigratorTest.java index b220ed7..9db5c8d 100644 --- a/src/test/java/de/terrestris/shogun/migrator/MigratorTest.java +++ b/src/test/java/de/terrestris/shogun/migrator/MigratorTest.java @@ -11,13 +11,13 @@ import java.io.IOException; import java.util.HashMap; -public class MigratorTest { +class MigratorTest { private final ObjectMapper mapper = new ObjectMapper(); @ParameterizedTest @ValueSource(strings = {"/1.json", "/2.json", "/3.json"}) - public void testMigration(String file) throws IOException { + void testMigration(String file) throws IOException { JsonNode node = mapper.readTree(MigratorTest.class.getResource(file)); byte[] bs = Shogun2Migrator.migrateApplication(node, new HashMap<>()); byte[] expected = IOUtils.toByteArray(MigratorTest.class.getResource("/migrated" + file)); @@ -26,7 +26,7 @@ public void testMigration(String file) throws IOException { @ParameterizedTest @ValueSource(strings = {"/layer1.json", "/layer2.json", "/layer3.json", "/layer4.json", "/layer5.json", "/layer6.json", "/layer7.json", "/layer8.json"}) - public void testLayerMigration(String file) throws IOException { + void testLayerMigration(String file) throws IOException { JsonNode node = mapper.readTree(MigratorTest.class.getResource(file)); byte[] bs = Shogun2Migrator.migrateLayer(node); byte[] expected = IOUtils.toByteArray(MigratorTest.class.getResource("/migratedlayer" + file));