From d51fca6f06210bd41d6dfbfaab04eebd4b0c9492 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Wed, 15 Jan 2020 11:28:15 -0500 Subject: [PATCH 01/11] WIP1 --- settings.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.gradle b/settings.gradle index b3ab3e757..1b37654ef 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include 'backend', 'ui', 'pac4j-module' +include 'backend', 'ui', 'pac4j-module', 'beacon', 'beacon:core', 'beacon:spring' From b9d69f7ff29bf63e2b624f8a7329ac58c6a65eca Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Wed, 15 Jan 2020 11:55:39 -0500 Subject: [PATCH 02/11] WIP2 --- beacon/beacon-core/build.gradle | 16 +++++ .../java/edu/internet2/tap/beacon/Beacon.java | 22 +++++++ .../internet2/tap/beacon/BeaconPublisher.java | 10 +++ .../tap/beacon/DefaultBeaconPublisher.java | 65 +++++++++++++++++++ .../beacon/DefaultBeaconPublisherTests.java | 13 ++++ beacon/beacon-spring/build.gradle | 41 ++++++++++++ settings.gradle | 2 +- 7 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 beacon/beacon-core/build.gradle create mode 100644 beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java create mode 100644 beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java create mode 100644 beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java create mode 100644 beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java create mode 100644 beacon/beacon-spring/build.gradle diff --git a/beacon/beacon-core/build.gradle b/beacon/beacon-core/build.gradle new file mode 100644 index 000000000..94f9ab994 --- /dev/null +++ b/beacon/beacon-core/build.gradle @@ -0,0 +1,16 @@ +apply plugin: 'java' +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + jcenter() +} + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2' +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java new file mode 100644 index 000000000..7e7dec5a5 --- /dev/null +++ b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java @@ -0,0 +1,22 @@ +package edu.internet2.tap.beacon; + +/** + * Exposes expected names of environment variables holding beacon config data. + */ +public final class Beacon { + + private Beacon() { + } + + public static final String LOG_HOST = "LOGHOST"; + + public static final String LOG_PORT = "LOGPORT"; + + public static final String IMAGE = "IMAGE"; + + public static final String VERSION = "VERSION"; + + public static final String TIERVERSION = "TIERVERSION"; + + public static final String MAINTAINER = "MAINTAINER"; +} diff --git a/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java new file mode 100644 index 000000000..80f4f77e4 --- /dev/null +++ b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java @@ -0,0 +1,10 @@ +package edu.internet2.tap.beacon; + +/** + * Simple SPI allowing implementations to publish to beacon service utilizing Runnable API + * so that publishing code could run in separate threads of execution. + * + * @author Dmitriy Kopylenko + */ +public interface BeaconPublisher extends Runnable { +} diff --git a/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java new file mode 100644 index 000000000..d7a4e9837 --- /dev/null +++ b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java @@ -0,0 +1,65 @@ +package edu.internet2.tap.beacon; + +import java.util.HashMap; +import java.util.Map; + +import static edu.internet2.tap.beacon.Beacon.IMAGE; +import static edu.internet2.tap.beacon.Beacon.LOG_HOST; +import static edu.internet2.tap.beacon.Beacon.LOG_PORT; +import static edu.internet2.tap.beacon.Beacon.MAINTAINER; +import static edu.internet2.tap.beacon.Beacon.TIERVERSION; +import static edu.internet2.tap.beacon.Beacon.VERSION; + +/** + * Default implementation that knows the details about payload structure with its data and beacon endpoint details + * gathered by upstream components and passed to this implementation at object construction site. + * + * @author Dmitriy Kopylenko + */ +public class DefaultBeaconPublisher implements BeaconPublisher { + + private String endpointUri; + + private Map dataToPublish; + + public DefaultBeaconPublisher(Map beaconDetails) { + + + //Do data validation checks here. If any of the necessary beacon data not available here, throw a Runtime exception + if (beaconDetails == null) { + throw new IllegalArgumentException("beaconDetails Map must not be null"); + } + if (beaconDetails.get(LOG_HOST) == null + || beaconDetails.get(LOG_PORT) == null + || beaconDetails.get(IMAGE) == null + || beaconDetails.get(VERSION) == null + || beaconDetails.get(TIERVERSION) == null + || beaconDetails.get(MAINTAINER) == null) { + throw new IllegalArgumentException("Not all the necessary beacon data is available to be able to publish to beacon"); + } + this.endpointUri = String.format("http://%s:%s", beaconDetails.get(LOG_HOST), beaconDetails.get(LOG_PORT)); + + this.dataToPublish = new HashMap<>(); + this.dataToPublish.put("msgType", "TIERBEACON"); + this.dataToPublish.put("msgName", "TIER"); + this.dataToPublish.put("msgVersion", "1.0"); + this.dataToPublish.put("tbProduct", beaconDetails.get(IMAGE)); + this.dataToPublish.put("tbProductVersion", beaconDetails.get(VERSION)); + this.dataToPublish.put("tbTIERRelease", beaconDetails.get(TIERVERSION)); + this.dataToPublish.put("tbMaintainer", beaconDetails.get(MAINTAINER)); + } + + @Override + public void run() { + //log.debug("Posting data {} to beacon endpoint {}", dataToPublish, endpointUri); + } + + //Below are package-private getters used in unit tests + String getEndpointUri() { + return this.endpointUri; + } + + Map getDataToPublish() { + return this.dataToPublish; + } +} diff --git a/beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java b/beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java new file mode 100644 index 000000000..9cc000625 --- /dev/null +++ b/beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java @@ -0,0 +1,13 @@ +package edu.internet2.tap.beacon; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class DefaultBeaconPublisherTests { + + @Test + public void checkCorrectInvariantsWithBeaconDataNull() { + Assertions.assertTrue(true); + //new DefaultBeaconPublisher(null); + } +} diff --git a/beacon/beacon-spring/build.gradle b/beacon/beacon-spring/build.gradle new file mode 100644 index 000000000..4b0ba4813 --- /dev/null +++ b/beacon/beacon-spring/build.gradle @@ -0,0 +1,41 @@ +/*plugins { + id 'groovy' + id 'jacoco' + id 'org.springframework.boot' version '2.0.0.RELEASE' apply false + id 'io.spring.dependency-management' version '1.0.6.RELEASE' + id 'io.franzbecker.gradle-lombok' version '1.13' +}*/ + +apply plugin: 'java' +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + jcenter() +} + +/*dependencyManagement { + imports { + mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES + } +}*/ + +/*lombok { + version = "1.18.4" + //TODO: get new sha256 + sha256 = "" +}*/ + +/*dependencies { + compileOnly project(':backend') + testCompile project(':backend') + testCompile "org.springframework.boot:spring-boot-starter-test" + testCompile "org.spockframework:spock-core:1.1-groovy-2.4" + testCompile "org.spockframework:spock-spring:1.1-groovy-2.4" + //annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" +}*/ + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2' +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 1b37654ef..f71bfae82 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include 'backend', 'ui', 'pac4j-module', 'beacon', 'beacon:core', 'beacon:spring' +include 'backend', 'ui', 'pac4j-module', 'beacon', 'beacon:beacon-core', 'beacon:beacon-spring' From 1b72b56fddeba83ed71021e73e72702d3415c58c Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Wed, 15 Jan 2020 13:18:53 -0500 Subject: [PATCH 03/11] Establishing modules structure --- beacon/beacon-spring/build.gradle | 41 ------------------- beacon/build.gradle | 4 ++ beacon/{beacon-core => core}/build.gradle | 4 ++ .../java/edu/internet2/tap/beacon/Beacon.java | 0 .../internet2/tap/beacon/BeaconPublisher.java | 0 .../tap/beacon/DefaultBeaconPublisher.java | 0 .../beacon/DefaultBeaconPublisherTests.java | 8 ++-- beacon/spring/build.gradle | 29 +++++++++++++ settings.gradle | 2 +- 9 files changed, 43 insertions(+), 45 deletions(-) delete mode 100644 beacon/beacon-spring/build.gradle create mode 100644 beacon/build.gradle rename beacon/{beacon-core => core}/build.gradle (83%) rename beacon/{beacon-core => core}/src/main/java/edu/internet2/tap/beacon/Beacon.java (100%) rename beacon/{beacon-core => core}/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java (100%) rename beacon/{beacon-core => core}/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java (100%) rename beacon/{beacon-core => core}/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java (51%) create mode 100644 beacon/spring/build.gradle diff --git a/beacon/beacon-spring/build.gradle b/beacon/beacon-spring/build.gradle deleted file mode 100644 index 4b0ba4813..000000000 --- a/beacon/beacon-spring/build.gradle +++ /dev/null @@ -1,41 +0,0 @@ -/*plugins { - id 'groovy' - id 'jacoco' - id 'org.springframework.boot' version '2.0.0.RELEASE' apply false - id 'io.spring.dependency-management' version '1.0.6.RELEASE' - id 'io.franzbecker.gradle-lombok' version '1.13' -}*/ - -apply plugin: 'java' -sourceCompatibility = 1.8 -targetCompatibility = 1.8 - -repositories { - jcenter() -} - -/*dependencyManagement { - imports { - mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES - } -}*/ - -/*lombok { - version = "1.18.4" - //TODO: get new sha256 - sha256 = "" -}*/ - -/*dependencies { - compileOnly project(':backend') - testCompile project(':backend') - testCompile "org.springframework.boot:spring-boot-starter-test" - testCompile "org.spockframework:spock-core:1.1-groovy-2.4" - testCompile "org.spockframework:spock-spring:1.1-groovy-2.4" - //annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" -}*/ - -dependencies { - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2' - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2' -} \ No newline at end of file diff --git a/beacon/build.gradle b/beacon/build.gradle new file mode 100644 index 000000000..a0d845ead --- /dev/null +++ b/beacon/build.gradle @@ -0,0 +1,4 @@ +allprojects { + group = 'edu.internet2.tap.beacon' + version = '1.0.0-SNAPSHOT' +} \ No newline at end of file diff --git a/beacon/beacon-core/build.gradle b/beacon/core/build.gradle similarity index 83% rename from beacon/beacon-core/build.gradle rename to beacon/core/build.gradle index 94f9ab994..39cef521f 100644 --- a/beacon/beacon-core/build.gradle +++ b/beacon/core/build.gradle @@ -11,6 +11,10 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2' } +jar { + archiveName = "beacon-core-${version}.jar" +} + test { useJUnitPlatform() } \ No newline at end of file diff --git a/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java b/beacon/core/src/main/java/edu/internet2/tap/beacon/Beacon.java similarity index 100% rename from beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java rename to beacon/core/src/main/java/edu/internet2/tap/beacon/Beacon.java diff --git a/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java b/beacon/core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java similarity index 100% rename from beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java rename to beacon/core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java diff --git a/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java similarity index 100% rename from beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java rename to beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java diff --git a/beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java b/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java similarity index 51% rename from beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java rename to beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java index 9cc000625..d4b76f7bb 100644 --- a/beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java +++ b/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java @@ -1,13 +1,15 @@ package edu.internet2.tap.beacon; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertThrows; + public class DefaultBeaconPublisherTests { @Test public void checkCorrectInvariantsWithBeaconDataNull() { - Assertions.assertTrue(true); - //new DefaultBeaconPublisher(null); + assertThrows(IllegalArgumentException.class, () -> { + new DefaultBeaconPublisher(null); + }); } } diff --git a/beacon/spring/build.gradle b/beacon/spring/build.gradle new file mode 100644 index 000000000..c527c05f1 --- /dev/null +++ b/beacon/spring/build.gradle @@ -0,0 +1,29 @@ +import org.springframework.boot.gradle.plugin.SpringBootPlugin + +plugins { + id 'groovy' + id 'jacoco' + id 'org.springframework.boot' version '2.0.0.RELEASE' apply false + id 'io.spring.dependency-management' version '1.0.6.RELEASE' + id 'io.franzbecker.gradle-lombok' version '1.13' +} + +repositories { + jcenter() +} + +jar { + archiveName = "beacon-spring-${version}.jar" +} + +dependencyManagement { + imports { + mavenBom SpringBootPlugin.BOM_COORDINATES + } +} + +lombok { + version = "1.18.4" + //TODO: get new sha256 + sha256 = "" +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index f71bfae82..1b37654ef 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include 'backend', 'ui', 'pac4j-module', 'beacon', 'beacon:beacon-core', 'beacon:beacon-spring' +include 'backend', 'ui', 'pac4j-module', 'beacon', 'beacon:core', 'beacon:spring' From db3d6734c1af44bcfe9e14c561811dcec6f2b291 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Wed, 15 Jan 2020 15:25:47 -0500 Subject: [PATCH 04/11] Reworking impl --- .../tap/beacon/DefaultBeaconPublisher.java | 59 ++++++++++++++----- .../beacon/DefaultBeaconPublisherTests.java | 28 +++++++++ 2 files changed, 72 insertions(+), 15 deletions(-) diff --git a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java index d7a4e9837..168fd01ac 100644 --- a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java +++ b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java @@ -1,7 +1,13 @@ package edu.internet2.tap.beacon; +import java.io.IOException; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; import static edu.internet2.tap.beacon.Beacon.IMAGE; import static edu.internet2.tap.beacon.Beacon.LOG_HOST; @@ -18,9 +24,9 @@ */ public class DefaultBeaconPublisher implements BeaconPublisher { - private String endpointUri; + private URL enpointUrl; - private Map dataToPublish; + private String jsonPayload; public DefaultBeaconPublisher(Map beaconDetails) { @@ -37,29 +43,52 @@ public DefaultBeaconPublisher(Map beaconDetails) { || beaconDetails.get(MAINTAINER) == null) { throw new IllegalArgumentException("Not all the necessary beacon data is available to be able to publish to beacon"); } - this.endpointUri = String.format("http://%s:%s", beaconDetails.get(LOG_HOST), beaconDetails.get(LOG_PORT)); + try { + this.enpointUrl = new URL(String.format("http://%s:%s", beaconDetails.get(LOG_HOST), beaconDetails.get(LOG_PORT))); + } catch (MalformedURLException ex) { + throw new IllegalArgumentException(ex.getMessage()); + } + + Map dataToPublish = new HashMap<>(); + dataToPublish.put("msgType", "TIERBEACON"); + dataToPublish.put("msgName", "TIER"); + dataToPublish.put("msgVersion", "1.0"); + dataToPublish.put("tbProduct", beaconDetails.get(IMAGE)); + dataToPublish.put("tbProductVersion", beaconDetails.get(VERSION)); + dataToPublish.put("tbTIERRelease", beaconDetails.get(TIERVERSION)); + dataToPublish.put("tbMaintainer", beaconDetails.get(MAINTAINER)); - this.dataToPublish = new HashMap<>(); - this.dataToPublish.put("msgType", "TIERBEACON"); - this.dataToPublish.put("msgName", "TIER"); - this.dataToPublish.put("msgVersion", "1.0"); - this.dataToPublish.put("tbProduct", beaconDetails.get(IMAGE)); - this.dataToPublish.put("tbProductVersion", beaconDetails.get(VERSION)); - this.dataToPublish.put("tbTIERRelease", beaconDetails.get(TIERVERSION)); - this.dataToPublish.put("tbMaintainer", beaconDetails.get(MAINTAINER)); + //Create JSON payload without any 3-rd party library + this.jsonPayload = "{" + dataToPublish.entrySet().stream() + .map(e -> "\"" + e.getKey() + "\"" + ":\"" + e.getValue() + "\"") + .collect(Collectors.joining(", ")) + "}"; } @Override public void run() { - //log.debug("Posting data {} to beacon endpoint {}", dataToPublish, endpointUri); + try { + HttpURLConnection con = (HttpURLConnection) this.enpointUrl.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/json; utf-8"); + con.setRequestProperty("Accept", "application/json"); + con.setDoOutput(true); + try(OutputStream os = con.getOutputStream()){ + byte[] input = jsonPayload.getBytes("utf-8"); + os.write(input, 0, input.length); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } //Below are package-private getters used in unit tests String getEndpointUri() { - return this.endpointUri; + return enpointUrl.toString(); } - Map getDataToPublish() { - return this.dataToPublish; + String getJsonPayload() { + return jsonPayload; } } diff --git a/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java b/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java index d4b76f7bb..fcc405cb7 100644 --- a/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java +++ b/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java @@ -2,6 +2,10 @@ import org.junit.jupiter.api.Test; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; public class DefaultBeaconPublisherTests { @@ -12,4 +16,28 @@ public void checkCorrectInvariantsWithBeaconDataNull() { new DefaultBeaconPublisher(null); }); } + + @Test + public void checkCorrectInvariantsWithBeaconDataEmpty() { + assertThrows(IllegalArgumentException.class, () -> { + new DefaultBeaconPublisher(new HashMap<>()); + }); + } + + @Test + public void checkCorrectInvariantsWithValidBeaconData() { + String expectedJsonPayload = "{\"msgType\":\"TIERBEACON\", \"tbMaintainer\":\"unittest_maintainer\", \"msgName\":\"TIER\", \"tbProduct\":\"image\", \"msgVersion\":\"1.0\", \"tbProductVersion\":\"v1\", \"tbTIERRelease\":\"tv1\"}"; + + Map beaconData = new HashMap<>(); + beaconData.put("LOGHOST", "collector.testbed.tier.internet2.edu"); + beaconData.put("LOGPORT", "5001"); + beaconData.put("IMAGE", "image"); + beaconData.put("VERSION", "v1"); + beaconData.put("TIERVERSION", "tv1"); + beaconData.put("MAINTAINER", "unittest_maintainer"); + DefaultBeaconPublisher p = new DefaultBeaconPublisher(beaconData); + + assertEquals("http://collector.testbed.tier.internet2.edu:5001", p.getEndpointUri()); + assertEquals(expectedJsonPayload, p.getJsonPayload()); + } } From 003b63e1ce949bd8e19a40b5b219feeb44dd9f8c Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Wed, 15 Jan 2020 16:49:25 -0500 Subject: [PATCH 05/11] Reworking impl --- backend/build.gradle | 3 + beacon/core/build.gradle | 4 +- .../tap/beacon/DefaultBeaconPublisher.java | 2 - beacon/spring/build.gradle | 14 ++--- .../BeaconPublishingConfiguration.java | 60 +++++++++++++++++++ .../BeaconEnvironmentVariablesCondition.java | 48 +++++++++++++++ ...alOnBeaconEnvironmentVariablesPresent.java | 21 +++++++ .../main/resources/META-INF/spring.factories | 1 + 8 files changed, 142 insertions(+), 11 deletions(-) create mode 100644 beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/BeaconPublishingConfiguration.java create mode 100644 beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/condition/BeaconEnvironmentVariablesCondition.java create mode 100644 beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/condition/ConditionalOnBeaconEnvironmentVariablesPresent.java create mode 100644 beacon/spring/src/main/resources/META-INF/spring.factories diff --git a/backend/build.gradle b/backend/build.gradle index 423d9c450..c716b1ed1 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -213,6 +213,9 @@ dependencies { //Pacj4 sub-project runtimeOnly project(':pac4j-module') + //Beacon + runtimeOnly project(':beacon:spring') + enversTestCompile sourceSets.main.output enversTestCompile sourceSets.test.output enversTestCompile configurations.compile diff --git a/beacon/core/build.gradle b/beacon/core/build.gradle index 39cef521f..f2cd35c58 100644 --- a/beacon/core/build.gradle +++ b/beacon/core/build.gradle @@ -7,8 +7,8 @@ repositories { } dependencies { - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2' - testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2' + testCompile 'org.junit.jupiter:junit-jupiter-api:5.5.2' + testCompile 'org.junit.jupiter:junit-jupiter-engine:5.5.2' } jar { diff --git a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java index 168fd01ac..610baf827 100644 --- a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java +++ b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java @@ -29,8 +29,6 @@ public class DefaultBeaconPublisher implements BeaconPublisher { private String jsonPayload; public DefaultBeaconPublisher(Map beaconDetails) { - - //Do data validation checks here. If any of the necessary beacon data not available here, throw a Runtime exception if (beaconDetails == null) { throw new IllegalArgumentException("beaconDetails Map must not be null"); diff --git a/beacon/spring/build.gradle b/beacon/spring/build.gradle index c527c05f1..bb895f0e6 100644 --- a/beacon/spring/build.gradle +++ b/beacon/spring/build.gradle @@ -1,13 +1,14 @@ import org.springframework.boot.gradle.plugin.SpringBootPlugin plugins { - id 'groovy' - id 'jacoco' id 'org.springframework.boot' version '2.0.0.RELEASE' apply false id 'io.spring.dependency-management' version '1.0.6.RELEASE' - id 'io.franzbecker.gradle-lombok' version '1.13' } +apply plugin: 'java' +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + repositories { jcenter() } @@ -22,8 +23,7 @@ dependencyManagement { } } -lombok { - version = "1.18.4" - //TODO: get new sha256 - sha256 = "" +dependencies { + compile project(':beacon:core') + compile "org.springframework.boot:spring-boot-starter" } \ No newline at end of file diff --git a/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/BeaconPublishingConfiguration.java b/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/BeaconPublishingConfiguration.java new file mode 100644 index 000000000..e7ea6b2ba --- /dev/null +++ b/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/BeaconPublishingConfiguration.java @@ -0,0 +1,60 @@ +package edu.internet2.tap.beacon.configuration; + +import edu.internet2.tap.beacon.BeaconPublisher; +import edu.internet2.tap.beacon.DefaultBeaconPublisher; +import edu.internet2.tap.beacon.configuration.condition.ConditionalOnBeaconEnvironmentVariablesPresent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.Scheduled; + +import java.util.HashMap; +import java.util.Map; + +import static edu.internet2.tap.beacon.Beacon.IMAGE; +import static edu.internet2.tap.beacon.Beacon.LOG_HOST; +import static edu.internet2.tap.beacon.Beacon.LOG_PORT; +import static edu.internet2.tap.beacon.Beacon.MAINTAINER; +import static edu.internet2.tap.beacon.Beacon.TIERVERSION; +import static edu.internet2.tap.beacon.Beacon.VERSION; + +@Configuration +@ConditionalOnProperty(name = "shibui.beacon-enabled", havingValue = "true") +public class BeaconPublishingConfiguration { + + private static final Logger logger = LoggerFactory.getLogger(BeaconPublishingConfiguration.class); + + @Bean + @ConditionalOnBeaconEnvironmentVariablesPresent + public BeaconPublishingTask beaconPublisher(Environment env) { + logger.debug("Creating BeaconPublishingTask..."); + Map beaconData = new HashMap<>(); + beaconData.put(LOG_HOST, env.getProperty(LOG_HOST)); + beaconData.put(LOG_PORT, env.getProperty(LOG_PORT)); + beaconData.put(IMAGE, env.getProperty(IMAGE)); + beaconData.put(VERSION, env.getProperty(VERSION)); + beaconData.put(TIERVERSION, env.getProperty(TIERVERSION)); + beaconData.put(MAINTAINER, env.getProperty(MAINTAINER)); + return new BeaconPublishingTask(new DefaultBeaconPublisher(beaconData)); + } + + public static class BeaconPublishingTask { + + private BeaconPublisher beaconPublisher; + + public BeaconPublishingTask(BeaconPublisher beaconPublisher) { + this.beaconPublisher = beaconPublisher; + } + + //Cron is based on the spec defined here: https://spaces.at.internet2.edu/display/TWGH/TIER+Instrumentation+-+The+TIER+Beacon + @Scheduled(cron = "0 ${random.int[0,59]} ${random.int[0,3]} ? * *}") + @Async + void publish() { + beaconPublisher.run(); + } + } +} diff --git a/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/condition/BeaconEnvironmentVariablesCondition.java b/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/condition/BeaconEnvironmentVariablesCondition.java new file mode 100644 index 000000000..84f8857fc --- /dev/null +++ b/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/condition/BeaconEnvironmentVariablesCondition.java @@ -0,0 +1,48 @@ +package edu.internet2.tap.beacon.configuration.condition; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.env.Environment; +import org.springframework.core.type.AnnotatedTypeMetadata; + +import static edu.internet2.tap.beacon.Beacon.IMAGE; +import static edu.internet2.tap.beacon.Beacon.LOG_HOST; +import static edu.internet2.tap.beacon.Beacon.LOG_PORT; +import static edu.internet2.tap.beacon.Beacon.MAINTAINER; +import static edu.internet2.tap.beacon.Beacon.TIERVERSION; +import static edu.internet2.tap.beacon.Beacon.VERSION; + +/** + * {@link Condition} that checks for required beacon environment variables. + * + * @author Dmitriy Kopylenko + * @see ConditionalOnBeaconEnvironmentVariablesPresent + */ +public class BeaconEnvironmentVariablesCondition extends SpringBootCondition { + + private static final String MATCHED_MSG = "Beacon properties are present. Beacon activation condition is matched."; + + private static final String NOT_MATCHED_MSG = "Beacon properties are not present. Beacon activation condition is not matched."; + + private static final Logger logger = LoggerFactory.getLogger(BeaconEnvironmentVariablesCondition.class); + + @Override + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + Environment env = context.getEnvironment(); + if (env.containsProperty(LOG_HOST) + && env.containsProperty(LOG_PORT) + && env.containsProperty(IMAGE) + && env.containsProperty(VERSION) + && env.containsProperty(TIERVERSION) + && env.containsProperty(MAINTAINER)) { + logger.debug(MATCHED_MSG); + return ConditionOutcome.match(MATCHED_MSG); + } + logger.debug(NOT_MATCHED_MSG); + return ConditionOutcome.noMatch(NOT_MATCHED_MSG); + } +} diff --git a/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/condition/ConditionalOnBeaconEnvironmentVariablesPresent.java b/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/condition/ConditionalOnBeaconEnvironmentVariablesPresent.java new file mode 100644 index 000000000..66049923b --- /dev/null +++ b/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/condition/ConditionalOnBeaconEnvironmentVariablesPresent.java @@ -0,0 +1,21 @@ +package edu.internet2.tap.beacon.configuration.condition; + +import org.springframework.context.annotation.Conditional; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * {@link Conditional} that matches specific beacon environment variables are all present. + * + * @author Dmitriy Kopylenko + */ +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Conditional(BeaconEnvironmentVariablesCondition.class) +public @interface ConditionalOnBeaconEnvironmentVariablesPresent { +} diff --git a/beacon/spring/src/main/resources/META-INF/spring.factories b/beacon/spring/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..ae9c29c00 --- /dev/null +++ b/beacon/spring/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=edu.internet2.tap.beacon.configuration.BeaconPublishingConfiguration \ No newline at end of file From 8453a69cc219d6d1f6a7373582cf69e8f0071770 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Wed, 15 Jan 2020 17:27:18 -0500 Subject: [PATCH 06/11] Beacon implementation --- .../edu/internet2/tap/beacon/DefaultBeaconPublisher.java | 6 +++--- .../configuration/BeaconPublishingConfiguration.java | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java index 610baf827..74e49d930 100644 --- a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java +++ b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java @@ -81,12 +81,12 @@ public void run() { } - //Below are package-private getters used in unit tests - String getEndpointUri() { + //getters used in unit tests and calling components for debugging purposes + public String getEndpointUri() { return enpointUrl.toString(); } - String getJsonPayload() { + public String getJsonPayload() { return jsonPayload; } } diff --git a/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/BeaconPublishingConfiguration.java b/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/BeaconPublishingConfiguration.java index e7ea6b2ba..79e073641 100644 --- a/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/BeaconPublishingConfiguration.java +++ b/beacon/spring/src/main/java/edu/internet2/tap/beacon/configuration/BeaconPublishingConfiguration.java @@ -1,6 +1,5 @@ package edu.internet2.tap.beacon.configuration; -import edu.internet2.tap.beacon.BeaconPublisher; import edu.internet2.tap.beacon.DefaultBeaconPublisher; import edu.internet2.tap.beacon.configuration.condition.ConditionalOnBeaconEnvironmentVariablesPresent; import org.slf4j.Logger; @@ -43,10 +42,9 @@ public BeaconPublishingTask beaconPublisher(Environment env) { } public static class BeaconPublishingTask { + private DefaultBeaconPublisher beaconPublisher; - private BeaconPublisher beaconPublisher; - - public BeaconPublishingTask(BeaconPublisher beaconPublisher) { + public BeaconPublishingTask(DefaultBeaconPublisher beaconPublisher) { this.beaconPublisher = beaconPublisher; } @@ -54,6 +52,9 @@ public BeaconPublishingTask(BeaconPublisher beaconPublisher) { @Scheduled(cron = "0 ${random.int[0,59]} ${random.int[0,3]} ? * *}") @Async void publish() { + logger.debug("Publishing payload: {} to beacon endpoint: {}", + beaconPublisher.getJsonPayload(), + beaconPublisher.getEndpointUri()); beaconPublisher.run(); } } From c895f1925031cf8bdbe380378365b01ba08d4735 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Wed, 15 Jan 2020 17:38:45 -0500 Subject: [PATCH 07/11] Polishing --- .../internet2/tap/beacon/DefaultBeaconPublisher.java | 10 ++++------ .../tap/beacon/DefaultBeaconPublisherTests.java | 8 ++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java index 74e49d930..e8061268c 100644 --- a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java +++ b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java @@ -24,7 +24,7 @@ */ public class DefaultBeaconPublisher implements BeaconPublisher { - private URL enpointUrl; + private URL endpointUrl; private String jsonPayload; @@ -42,7 +42,7 @@ public DefaultBeaconPublisher(Map beaconDetails) { throw new IllegalArgumentException("Not all the necessary beacon data is available to be able to publish to beacon"); } try { - this.enpointUrl = new URL(String.format("http://%s:%s", beaconDetails.get(LOG_HOST), beaconDetails.get(LOG_PORT))); + this.endpointUrl = new URL(String.format("http://%s:%s", beaconDetails.get(LOG_HOST), beaconDetails.get(LOG_PORT))); } catch (MalformedURLException ex) { throw new IllegalArgumentException(ex.getMessage()); } @@ -65,7 +65,7 @@ public DefaultBeaconPublisher(Map beaconDetails) { @Override public void run() { try { - HttpURLConnection con = (HttpURLConnection) this.enpointUrl.openConnection(); + HttpURLConnection con = (HttpURLConnection) this.endpointUrl.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json; utf-8"); con.setRequestProperty("Accept", "application/json"); @@ -77,13 +77,11 @@ public void run() { } catch (IOException e) { e.printStackTrace(); } - - } //getters used in unit tests and calling components for debugging purposes public String getEndpointUri() { - return enpointUrl.toString(); + return endpointUrl.toString(); } public String getJsonPayload() { diff --git a/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java b/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java index fcc405cb7..86c8b2edd 100644 --- a/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java +++ b/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java @@ -12,16 +12,12 @@ public class DefaultBeaconPublisherTests { @Test public void checkCorrectInvariantsWithBeaconDataNull() { - assertThrows(IllegalArgumentException.class, () -> { - new DefaultBeaconPublisher(null); - }); + assertThrows(IllegalArgumentException.class, () -> new DefaultBeaconPublisher(null)); } @Test public void checkCorrectInvariantsWithBeaconDataEmpty() { - assertThrows(IllegalArgumentException.class, () -> { - new DefaultBeaconPublisher(new HashMap<>()); - }); + assertThrows(IllegalArgumentException.class, () -> new DefaultBeaconPublisher(new HashMap<>())); } @Test From ebee1d0ac34c02a3fa6c49d92ff63732934ff141 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Thu, 16 Jan 2020 08:59:08 -0500 Subject: [PATCH 08/11] Add Async facility --- .../tier/shibboleth/admin/ui/ShibbolethUiApplication.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/ShibbolethUiApplication.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/ShibbolethUiApplication.java index b2470b8c5..d539ad025 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/ShibbolethUiApplication.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/ShibbolethUiApplication.java @@ -16,6 +16,7 @@ import org.springframework.context.annotation.Profile; import org.springframework.context.event.EventListener; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.stereotype.Component; @@ -29,6 +30,7 @@ @EnableJpaAuditing @EnableScheduling @EnableWebSecurity +@EnableAsync public class ShibbolethUiApplication extends SpringBootServletInitializer { private static final Logger logger = LoggerFactory.getLogger(ShibbolethUiApplication.class); From e403ef678eac389d0d0901abc47fe6e9aa99a191 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Thu, 16 Jan 2020 09:31:10 -0500 Subject: [PATCH 09/11] Add beacon property --- backend/src/main/resources/application.properties | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index c55eeca2a..3202f95cd 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -89,3 +89,8 @@ shibui.roles=ROLE_ADMIN,ROLE_USER,ROLE_NONE #This property must be set to true and pac4j properties configured. For sample pac4j properties, see application.yml #for an example pac4j configuration #shibui.pac4j-enabled=true + +#This property must be set to true in order to enable posting stats to beacon endpoint. Furthermore, appropriate +#environment variables must be set for beacon publisher to be used (the ones that are set when running shib-ui in +#docker container +#shibui.beacon-enabled=true \ No newline at end of file From 70dc84c98a4731a01659838b64a848057ce539dc Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Tue, 21 Jan 2020 13:41:10 -0500 Subject: [PATCH 10/11] Address code review --- beacon/core/build.gradle | 23 ++++++++-- .../tap/beacon/DefaultBeaconPublisher.java | 4 +- .../beacon/DefaultBeaconPublisherTests.groovy | 45 +++++++++++++++++++ .../beacon/DefaultBeaconPublisherTests.java | 39 ---------------- 4 files changed, 67 insertions(+), 44 deletions(-) create mode 100644 beacon/core/src/test/groovy/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.groovy delete mode 100644 beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java diff --git a/beacon/core/build.gradle b/beacon/core/build.gradle index f2cd35c58..778c9ec97 100644 --- a/beacon/core/build.gradle +++ b/beacon/core/build.gradle @@ -1,4 +1,11 @@ -apply plugin: 'java' +import org.springframework.boot.gradle.plugin.SpringBootPlugin + +plugins { + id 'org.springframework.boot' version '2.0.0.RELEASE' apply false + id 'io.spring.dependency-management' version '1.0.6.RELEASE' +} + +apply plugin: 'groovy' sourceCompatibility = 1.8 targetCompatibility = 1.8 @@ -6,7 +13,17 @@ repositories { jcenter() } +dependencyManagement { + imports { + mavenBom SpringBootPlugin.BOM_COORDINATES + } +} + dependencies { + testCompile "org.springframework.boot:spring-boot-starter-test" + testCompile "org.spockframework:spock-core:1.1-groovy-2.4" + testCompile "org.spockframework:spock-spring:1.1-groovy-2.4" + testCompile 'org.junit.jupiter:junit-jupiter-api:5.5.2' testCompile 'org.junit.jupiter:junit-jupiter-engine:5.5.2' } @@ -15,6 +32,6 @@ jar { archiveName = "beacon-core-${version}.jar" } -test { +/*test { useJUnitPlatform() -} \ No newline at end of file +}*/ \ No newline at end of file diff --git a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java index e8061268c..eb077ddc9 100644 --- a/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java +++ b/beacon/core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java @@ -24,9 +24,9 @@ */ public class DefaultBeaconPublisher implements BeaconPublisher { - private URL endpointUrl; + private final URL endpointUrl; - private String jsonPayload; + private final String jsonPayload; public DefaultBeaconPublisher(Map beaconDetails) { //Do data validation checks here. If any of the necessary beacon data not available here, throw a Runtime exception diff --git a/beacon/core/src/test/groovy/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.groovy b/beacon/core/src/test/groovy/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.groovy new file mode 100644 index 000000000..d3004ef13 --- /dev/null +++ b/beacon/core/src/test/groovy/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.groovy @@ -0,0 +1,45 @@ +package edu.internet2.tap.beacon + +import spock.lang.Specification +import sun.security.x509.OtherName + +class DefaultBeaconPublisherTests extends Specification { + + def "DefaultBeaconPublisher invariants are enforced during object creation - null Map is passed"() { + when: + new DefaultBeaconPublisher(null) + + then: + thrown IllegalArgumentException + + } + + def "DefaultBeaconPublisher invariants are enforced during object creation - empty Map is passed"() { + when: + new DefaultBeaconPublisher([:]) + + then: + thrown IllegalArgumentException + } + + def "DefaultBeaconPublisher invariants are enforced during object creation - valid Beacon data Map is passed"() { + when: + def expectedJsonPaylaod = """{"msgType":"TIERBEACON", "tbMaintainer":"unittest_maintainer", "msgName":"TIER", "tbProduct":"image", "msgVersion":"1.0", "tbProductVersion":"v1", "tbTIERRelease":"tv1"}""" + + def configuredBeaconData = [LOGHOST : 'collector.testbed.tier.internet2.edu', + LOGPORT : '5001', + IMAGE : 'image', + VERSION : 'v1', + TIERVERSION: 'tv1', + MAINTAINER : 'unittest_maintainer'] + def p = new DefaultBeaconPublisher(configuredBeaconData) + println p.jsonPayload + + then: + noExceptionThrown() + p.endpointUri == 'http://collector.testbed.tier.internet2.edu:5001' + p.jsonPayload == expectedJsonPaylaod + + } + +} diff --git a/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java b/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java deleted file mode 100644 index 86c8b2edd..000000000 --- a/beacon/core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java +++ /dev/null @@ -1,39 +0,0 @@ -package edu.internet2.tap.beacon; - -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -public class DefaultBeaconPublisherTests { - - @Test - public void checkCorrectInvariantsWithBeaconDataNull() { - assertThrows(IllegalArgumentException.class, () -> new DefaultBeaconPublisher(null)); - } - - @Test - public void checkCorrectInvariantsWithBeaconDataEmpty() { - assertThrows(IllegalArgumentException.class, () -> new DefaultBeaconPublisher(new HashMap<>())); - } - - @Test - public void checkCorrectInvariantsWithValidBeaconData() { - String expectedJsonPayload = "{\"msgType\":\"TIERBEACON\", \"tbMaintainer\":\"unittest_maintainer\", \"msgName\":\"TIER\", \"tbProduct\":\"image\", \"msgVersion\":\"1.0\", \"tbProductVersion\":\"v1\", \"tbTIERRelease\":\"tv1\"}"; - - Map beaconData = new HashMap<>(); - beaconData.put("LOGHOST", "collector.testbed.tier.internet2.edu"); - beaconData.put("LOGPORT", "5001"); - beaconData.put("IMAGE", "image"); - beaconData.put("VERSION", "v1"); - beaconData.put("TIERVERSION", "tv1"); - beaconData.put("MAINTAINER", "unittest_maintainer"); - DefaultBeaconPublisher p = new DefaultBeaconPublisher(beaconData); - - assertEquals("http://collector.testbed.tier.internet2.edu:5001", p.getEndpointUri()); - assertEquals(expectedJsonPayload, p.getJsonPayload()); - } -} From c8343d69d7f2a90776488e495d281c9f44e19d53 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Tue, 21 Jan 2020 15:33:11 -0500 Subject: [PATCH 11/11] Cleanup --- beacon/core/build.gradle | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/beacon/core/build.gradle b/beacon/core/build.gradle index 778c9ec97..5c9689ebd 100644 --- a/beacon/core/build.gradle +++ b/beacon/core/build.gradle @@ -30,8 +30,4 @@ dependencies { jar { archiveName = "beacon-core-${version}.jar" -} - -/*test { - useJUnitPlatform() -}*/ \ No newline at end of file +} \ No newline at end of file