Skip to content

Commit

Permalink
Add sonar scanner and build workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbllmnn committed Aug 31, 2023
1 parent f085148 commit eadd7a2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 49 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
</snapshotRepository>
</distributionManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar.host.url>https://sq.terrestris.de</sonar.host.url>
<sonar.login>terrestris</sonar.login>
<sonar.sources>src/main/</sonar.sources>
</properties>

<profiles>
<profile>
<id>reporting</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Integer> 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")) {
Expand All @@ -38,12 +42,12 @@ public static byte[] migrateApplication(ObjectNode node, Map<Integer, Integer> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -94,12 +97,12 @@ public static byte[] migrateApplication(JsonNode node, Map<Integer, Integer> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -218,7 +221,7 @@ public Map<Integer, Integer> migrateLayers() {
try {
JsonNode node = fetch(source, "rest/projectlayers");
Map<Integer, Integer> layerIdMap = new HashMap<>();
int i = 0;
// int i = 0;
for (JsonNode layer : node) {
log.info("Migrating layer...");
byte[] bs = migrateLayer(layer);
Expand All @@ -243,7 +246,7 @@ public Map<Integer, Integer> migrateLayers() {
public void migrateApplications(Map<Integer, Integer> 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);
Expand Down
47 changes: 17 additions & 30 deletions src/main/java/de/terrestris/shogun/migrator/util/ApiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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");
}

}
6 changes: 3 additions & 3 deletions src/test/java/de/terrestris/shogun/migrator/MigratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down

0 comments on commit eadd7a2

Please sign in to comment.