Skip to content

Commit

Permalink
Reworking impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dima767 committed Jan 15, 2020
1 parent 1b72b56 commit db3d673
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,9 +24,9 @@
*/
public class DefaultBeaconPublisher implements BeaconPublisher {

private String endpointUri;
private URL enpointUrl;

private Map<String, String> dataToPublish;
private String jsonPayload;

public DefaultBeaconPublisher(Map<String, String> beaconDetails) {

Expand All @@ -37,29 +43,52 @@ public DefaultBeaconPublisher(Map<String, String> 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<String, String> 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<String, String> getDataToPublish() {
return this.dataToPublish;
String getJsonPayload() {
return jsonPayload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String, String> 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());
}
}

0 comments on commit db3d673

Please sign in to comment.