diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c3795fcb..33ba8186 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,7 +14,9 @@ stages: - ProductClient.jar - ProductClient.zip reports: - cobertura: build/reports/cobertura/cobertura.xml + coverage_report: + coverage_format: cobertura + path: build/reports/cobertura/cobertura.xml junit: build/test-results/test/TEST-*.xml cache: paths: diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e95c59c0..00000000 --- a/.travis.yml +++ /dev/null @@ -1,42 +0,0 @@ -language: java -# local ssh seems to require sudo -sudo: required - -jdk: - - openjdk8 - - openjdk11 - # - oraclejdk8 - not available in travis anymore - - oraclejdk11 - -# gradle caching -before_cache: - - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock - - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ -cache: - directories: - - "$HOME/.gradle/caches/" - - "$HOME/.gradle/wrapper/" - -# tests depend on local ssh -install: - - ssh-keygen -f ~/.ssh/id_rsa -t rsa -N '' -b 4096 - - cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys - - chmod 700 ~/.ssh/authorized_keys -addons: - ssh_known_hosts: - - 127.0.0.1 - - localhost - -script: - - ./gradlew --info build - # use --scan to post output online - # - ./gradlew build --scan - -# upload coverage -after_success: - - bash <(curl -s https://codecov.io/bash) - - "./gradlew sendCoverageToCodacy" - -env: - # CODACY_PROJECT_TOKEN - secure: PO6Zdc7e/x021brgk55kG1zTUPSNnvQ8BKNDJJK8cZJ4dgLgzbvxW5eoNPSn2CW5/C5YqkLgUVPkVy15zUVNpdM/RXYgzPiFTyg2GFXLQL9DbfQOz2zD8/iSySxTjUGfd7VNJZPG8XlFmzUIvY+4Eykj4UBsTFknZmro1kC/PS5E/bTtOrYGUKg9mfQz3t4/Tx7n8mw5CO5IVaHvklpbUTgaW0d8Et0qxQZ/Ks2xnlEP/4OcmSLMDeeyeYKVJc9KajgU27r1qtSoWtqL5InyW+L/8MU0soFDPO71KCphB0dbLJ6sdkzCWPIWfA/zeVlf+54K9zveXdbtdJs1Btzw73sTruoq2z/YTUZVIUdLG5yenMLPX0ayWpWc7AT4dDkSNXrwy+wWvjcF7mMddffvvGH//38FtkQ8fvm2o2tq/PIua2mQb8otsS7IKhOgmDFV+C2ygsMtTq/Wn7ny/0Yb+vUswNZUEZiK5IMEGF73OMIriDFKIvc4tJnJ2xKw2SY5cnratxeWzpsPH3IRIIG+bIfAGvN+g6/f97+xemR5YVO9FuZKR8IEBPl0jM+s8G2T1PpSG9LsfDPYSLIYC95GUEi9dFcFX7FP5y3a0rxyCyCcu+YoSBQXTsb/mCcb+IKoMugSh88hdUhqbDWbHA252ryBCmW7lTQI8C4S3B0NKBA= diff --git a/build.gradle b/build.gradle index 1f1feaab..8ce487a1 100644 --- a/build.gradle +++ b/build.gradle @@ -8,8 +8,8 @@ plugins { id "java" id "jacoco" // code coverage id "pmd" // static code analysis - id "org.ajoberstar.grgit" version "4.1.0" // git repo information - id "org.ajoberstar.git-publish" version "3.0.0" // publish gh-pages branch + id "org.ajoberstar.grgit" version "4.1.1" // git repo information + id "org.ajoberstar.git-publish" version "3.0.1" // publish gh-pages branch id "com.kageiit.jacobo" version "2.1.0" } @@ -25,8 +25,6 @@ configurations { } dependencies { - codacy "com.codacy:codacy-coverage-reporter:4.0+" - // for explanation of dependency types, see // https://docs.gradle.org/current/userguide/building_java_projects.html @@ -189,9 +187,6 @@ gitPublish { } } - -// Tasks for TravisCI - // convert jacoco to cobertura import com.kageiit.jacobo.JacoboTask tasks.create("jacobo", JacoboTask, { @@ -200,18 +195,3 @@ tasks.create("jacobo", JacoboTask, { it.srcDirs = sourceSets.main.java.srcDirs }).dependsOn(jacocoTestReport) check.dependsOn jacobo - - -// .travis.yml uses this to upload coverage -task sendCoverageToCodacy(type: JavaExec, dependsOn: jacocoTestReport) { - description = "Upload coverage to codacy (used by TravisCI)" - main = "com.codacy.CodacyCoverageReporter" - classpath = configurations.codacy - args = [ - "report", - "-l", - "Java", - "-r", - "${buildDir}/reports/jacoco/test/jacocoTestReport.xml" - ] -} diff --git a/code.json b/code.json index 6d1e9644..e455fd80 100644 --- a/code.json +++ b/code.json @@ -3,7 +3,7 @@ "name": "Product Distribution Layer", "organization": "U.S. Geological Survey", "description": "Distribution system used for derived earthquake information", - "version": "v2.7.11", + "version": "v2.7.12", "status": "Production", "permissions": { "usageType": "openSource", @@ -27,7 +27,7 @@ "email": "jmfee@usgs.gov" }, "date": { - "metadataLastUpdated": "2021-12-16" + "metadataLastUpdated": "2022-09-16" } } ] diff --git a/src/main/java/gov/usgs/earthquake/distribution/ProductClient.java b/src/main/java/gov/usgs/earthquake/distribution/ProductClient.java index 43be1bae..e2763f09 100644 --- a/src/main/java/gov/usgs/earthquake/distribution/ProductClient.java +++ b/src/main/java/gov/usgs/earthquake/distribution/ProductClient.java @@ -64,7 +64,7 @@ public class ProductClient extends DefaultConfigurable implements ProductClientMBean, Bootstrappable { /** The "release" version number. */ - public static final String RELEASE_VERSION = "Version 2.7.11 2021-12-16"; + public static final String RELEASE_VERSION = "Version 2.7.12 2022-09-16"; /** Property name used on products for current RELEASE_VERSION. */ public static final String PDL_CLIENT_VERSION_PROPERTY = "pdl-client-version"; diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java index 35505b9e..814a374e 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java @@ -9,6 +9,7 @@ import java.nio.charset.StandardCharsets; import javax.json.Json; +import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonReader; @@ -98,19 +99,55 @@ public JsonObject getEventPlaces(BigDecimal latitude, BigDecimal longitude) } } + /** * Get nearest place to a latitude and longitude * @param latitude of place * @param longitude of place * @return JSONObject of place + * @throws IndexOutOfBoundsException on no places returned * @throws IOException on IO error * @throws MalformedURLException on URL error + * @deprecated */ - public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude) throws IOException, MalformedURLException { - JsonObject places = this.getEventPlaces(latitude, longitude); - JsonObject feature = places.getJsonArray("features").getJsonObject(0); + public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude) + throws IndexOutOfBoundsException, IOException, MalformedURLException { + return this.getNearestPlace(latitude, longitude, 300); + } - return feature; + /** + * Get nearest place to a latitude and longitude + * @param latitude of place + * @param longitude of place + * @param maxradiuskm around place + * @return JSONObject of place + * @throws IOException on IO error + * @throws MalformedURLException on URL error + */ + public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, + int maxradiuskm) throws IOException, MalformedURLException { + final URL url = new URL(this.endpointUrl + + "?type=geonames" + + "&latitude=" + URLEncoder.encode(latitude.toString(), StandardCharsets.UTF_8.toString()) + + "&longitude=" + URLEncoder.encode(longitude.toString(), StandardCharsets.UTF_8.toString()) + + "&maxradiuskm=" + URLEncoder.encode(String.valueOf(maxradiuskm), StandardCharsets.UTF_8.toString()) + + "&limit=1" + ); + + try ( + InputStream in = StreamUtils.getURLInputStream(url, this.connectTimeout, this.readTimeout); + JsonReader reader = Json.createReader(in) + ) { + JsonObject json = reader.readObject(); + JsonObject places = json.getJsonObject("geonames"); + JsonArray features = places.getJsonArray("features"); + + if (features.size() > 0) { + return features.getJsonObject(0); + } else { + return null; + } + } } /** @return readTimeout */ @@ -134,4 +171,4 @@ public void setReadTimeout(final int readTimeout) { } // as needed, implement full GeoServe places API options -} \ No newline at end of file +} diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoserveRegionsService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoserveRegionsService.java index e6a4d8c4..e5be1590 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoserveRegionsService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoserveRegionsService.java @@ -138,4 +138,4 @@ public void setReadTimeout(final int readTimeout) { } // as needed, implement full GeoServe places API options -} \ No newline at end of file +} diff --git a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java index 7ccfb35b..10522eb7 100644 --- a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java +++ b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java @@ -111,7 +111,7 @@ public ProductSummary getProductSummary(Product product) throws Exception { summaryProperties.put("title", StringUtils.encodeAsUtf8(title)); } catch (Exception ex) { LOGGER - .fine(String.format("[%s] %s for product %s", this.getName(), ex.getMessage(), product.getId().toString())); + .warning(String.format("[%s] %s for product %s", this.getName(), ex.getMessage(), product.getId().toString())); // Do nothing, value-added failed. Move on. } } @@ -250,19 +250,44 @@ public void configure(Config config) throws Exception { * * @throws IOException if IO error occurs */ - public String getEventTitle(BigDecimal latitude, BigDecimal longitude) throws IOException { + public String getEventTitle(BigDecimal latitude, BigDecimal longitude) throws Exception, IOException { + StringBuffer messages = new StringBuffer(); + String message = null; + try { - JsonObject feature = this.geoservePlaces.getNearestPlace(latitude, longitude); - double distance = feature.getJsonObject("properties").getJsonNumber("distance").doubleValue(); + final JsonObject feature = this.geoservePlaces.getNearestPlace( + latitude, + longitude, + this.distanceThreshold + ); - if (distance <= (double) this.distanceThreshold) { + if (feature != null) { return this.formatEventTitle(feature); + } else { + message = "Places service returned no places within distance threshold"; + messages.append(message + ". "); + LOGGER.log(Level.INFO, "[" + this.getName() + "] " + message); } } catch (Exception e) { - LOGGER.log(Level.WARNING, "[" + this.getName() + "] failed to get nearest place from geoserve places service."); + message = "Failed to get nearest place from geoserve places service"; + messages.append(message + ". "); + messages.append(e.getMessage() + ". "); + LOGGER.log(Level.INFO, "[" + this.getName() + "] " + message); + } + + try { + return this.geoserveRegions.getFeRegionName(latitude, longitude); + } catch (Exception e) { + message = "Failed to get FE region name"; + messages.append(message + ". "); + messages.append(e.getMessage() + ". "); + LOGGER.log(Level.INFO, "[" + this.getName() + "] ."); } - return this.geoserveRegions.getFeRegionName(latitude, longitude); + // If we get this far, things failed spectacularly, report the error + Exception e = new Exception(messages.toString()); + e.fillInStackTrace(); + throw e; } /** @@ -311,4 +336,31 @@ public String azimuthToDirection(double azimuth) { return directions[(int) Math.round((azimuth % 360.0) / fullwind)]; } -} \ No newline at end of file + public static void main(String[] args) throws Exception { + BigDecimal latitude = new BigDecimal("0.0"); + BigDecimal longitude = new BigDecimal("0.0"); + int maxradiuskm = DEFAULT_GEOSERVE_DISTANCE_THRESHOLD; + final OriginIndexerModule module = new OriginIndexerModule( + new GeoservePlacesService(), + new GeoserveRegionsService() + ); + module.setName("TestModule"); + + for (String arg : args) { + if (arg.startsWith("--latitude=")) { + latitude = new BigDecimal(arg.replace("--latitude=", "")); + } else if (arg.startsWith("--longitude=")) { + longitude = new BigDecimal(arg.replace("--longitude=", "")); + } else if (arg.startsWith("--maxradiuskm=")) { + maxradiuskm = Integer.parseInt(arg.replace("--maxradiuskm=", "")); + } + } + + module.setDistanceThreshold(maxradiuskm); + + System.out.printf("Title[%s, %s] = `%s`\n", + latitude.doubleValue(), + longitude.doubleValue(), + module.getEventTitle(latitude, longitude)); + } +} diff --git a/src/test/java/gov/usgs/earthquake/geoserve/GeoservePlacesServiceTest.java b/src/test/java/gov/usgs/earthquake/geoserve/GeoservePlacesServiceTest.java index fea3fed5..7829f4a0 100644 --- a/src/test/java/gov/usgs/earthquake/geoserve/GeoservePlacesServiceTest.java +++ b/src/test/java/gov/usgs/earthquake/geoserve/GeoservePlacesServiceTest.java @@ -2,15 +2,12 @@ import java.io.IOException; import java.math.BigDecimal; +import java.math.BigInteger; import java.net.MalformedURLException; import javax.json.Json; import javax.json.JsonObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - public class GeoservePlacesServiceTest { private GeoservePlacesService service = null; @@ -25,29 +22,5 @@ public class GeoservePlacesServiceTest { ) ).build(); - @Before - public void setUpTestEnvironment() { - this.service = new DummyPlacesService(); - } - - @Test - public void getNearestPlace() throws Exception { - BigDecimal latitude = new BigDecimal("0.0"); - BigDecimal longitude = new BigDecimal("0.0"); - - // nearest feature from feature collection - JsonObject expectation = this.feature; - Assert.assertEquals(expectation, this.service.getNearestPlace(latitude, longitude)); - } - - protected class DummyPlacesService extends GeoservePlacesService { - @Override - public JsonObject getEventPlaces(BigDecimal latitude, BigDecimal longitude) throws IOException, MalformedURLException { - JsonObject eventCollection = Json.createObjectBuilder().add("features", - Json.createArrayBuilder().add(0, feature) - ).build(); - - return eventCollection; - } - } -} \ No newline at end of file + // TODO Test this class, consider Mockito +} diff --git a/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java b/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java index 7dc74fe2..4e7c5805 100644 --- a/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java +++ b/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.math.BigDecimal; +import java.math.BigInteger; import java.net.MalformedURLException; import javax.json.Json; @@ -140,7 +141,7 @@ public void formatEventTitleTest() { @Test - public void getEventTitleTest() throws IOException { + public void getEventTitleTest() throws Exception, IOException { BigDecimal latitude = new BigDecimal("0.0"); BigDecimal longitude = new BigDecimal("0.0"); String title = ""; @@ -171,16 +172,21 @@ public void azimuthToDirectionTest() { protected class DummyPlacesService extends GeoservePlacesService { @Override - public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude) throws IOException, MalformedURLException { - return Json.createObjectBuilder().add("properties", - Json.createObjectBuilder() - .add("admin1_name", "admin1_name") - .add("azimuth", 60.1) - .add("country_code", "country_code") - .add("country_name", "country_name") - .add("distance", distance) - .add("name", "name") - ).build(); + public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, int maxradiuskm) + throws IOException, MalformedURLException { + if (distance <= maxradiuskm) { + return Json.createObjectBuilder().add("properties", + Json.createObjectBuilder() + .add("admin1_name", "admin1_name") + .add("azimuth", 60.1) + .add("country_code", "country_code") + .add("country_name", "country_name") + .add("distance", distance) + .add("name", "name")) + .build(); + } else { + return null; + } } } protected class DummyRegionsService extends GeoserveRegionsService {