Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

building out VersionPlace to add emissary version info to metadata #887

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/config/emissary.admin.ClassNameInventory.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ ToLowerPlace = "emissary.place.sample.ToLowerPlace"
ToUpperPlace = "emissary.place.sample.ToUpperPlace"
UnixCommandPlace = "emissary.place.UnixCommandPlace"
UnixFilePlace = "emissary.id.UnixFilePlace"
VersionPlace = "emissary.place.VersionPlace"
1 change: 1 addition & 0 deletions src/main/config/places.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ PLACE = "@{URL}/ToLowerPlace"
PLACE = "@{URL}/DropOffPlace"
PLACE = "@{URL}/UnixFilePlace"
PLACE = "@{URL}/JsonEscapePlace"
PLACE = "@{URL}/VersionPlace"
78 changes: 78 additions & 0 deletions src/main/java/emissary/place/VersionPlace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package emissary.place;

import emissary.core.IBaseDataObject;
import emissary.core.ResourceException;
import emissary.util.Version;

import java.io.IOException;
import java.io.InputStream;

public class VersionPlace extends ServiceProviderPlace {

private static final String EMISSARY_VERSION = "EMISSARY_VERSION";
private boolean includeDate;
private final Version version = new Version();
private String formattedVersion;

/**
* Create the place from the specified config file or resource
*
* @param configInfo the config file or resource to use
* @param dir the name of the controlling directory to register with
* @param placeLoc string name of this place
*/
public VersionPlace(String configInfo, String dir, String placeLoc) throws IOException {
super(configInfo, dir, placeLoc);
configurePlace();
}

/**
* Create the place from the specified config stream data
*
* @param configInfo the config file or resource to use
* @param dir the name of the controlling directory to register with
* @param placeLoc string name of this place
*/
public VersionPlace(InputStream configInfo, String dir, String placeLoc) throws IOException {
super(configInfo, dir, placeLoc);
configurePlace();
}

/**
* Create the place from the specified config stream data
*
* @param configInfo the config file or resource to use
*/
public VersionPlace(InputStream configInfo) throws IOException {
super(configInfo);
configurePlace();
}

/**
* Create with the default configuration
*/
public VersionPlace() throws IOException {
super();
configurePlace();
}

private void configurePlace() {
includeDate = configG.findBooleanEntry("INCLUDE_DATE", true);
formattedVersion = getEmissaryVersion();
cfkoehler marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void process(IBaseDataObject payload) throws ResourceException {
payload.putParameter(EMISSARY_VERSION, formattedVersion);
}

private String getEmissaryVersion() {
sambish5 marked this conversation as resolved.
Show resolved Hide resolved
if (includeDate) {
// version with date & time information
return version.getVersion() + "-" + version.getTimestamp().replaceAll("\\D", "");
} else {
// adds just version
return version.getVersion();
}
}
}
8 changes: 8 additions & 0 deletions src/main/resources/emissary/place/VersionPlace.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PLACE_NAME = "VersionPlace"
SERVICE_NAME = "VERSION"
SERVICE_TYPE = "VERIFY"
SERVICE_DESCRIPTION = "add emissary version to payload"
SERVICE_COST = 50
SERVICE_QUALITY = 50

SERVICE_PROXY = "UNKNOWN"
jpdahlke marked this conversation as resolved.
Show resolved Hide resolved
64 changes: 64 additions & 0 deletions src/test/java/emissary/place/VersionPlaceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package emissary.place;

import emissary.core.DataObjectFactory;
import emissary.core.IBaseDataObject;
import emissary.core.ResourceException;
import emissary.test.core.junit5.UnitTest;
import emissary.util.Version;
import emissary.util.io.ResourceReader;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

class VersionPlaceTest extends UnitTest {
private IBaseDataObject payload;
private VersionPlace place;
private Version version;
private String versionDate;

@Override
@BeforeEach
public void setUp() throws IOException {
payload = DataObjectFactory.getInstance();
version = new Version();
versionDate = version.getTimestamp().replaceAll("\\D", "");
}

@Override
@AfterEach
public void tearDown() throws Exception {
super.tearDown();
place.shutDown();
place = null;
payload = null;
versionDate = null;
}

@Test
void testAddVersionToPayload() throws ResourceException, IOException {
// create the place, using the normal class cfg
place = new VersionPlace();

place.process(payload);
assertEquals(payload.getStringParameter("EMISSARY_VERSION"), version.getVersion() + "-" + versionDate,
"added version should contain the date.");
}

@Test
void testAddVersionWithoutDate() throws ResourceException, IOException {
// create the place with the test cfg, having INCLUDE_DATE = "false"
InputStream is = new ResourceReader().getConfigDataAsStream(this.getClass());
place = new VersionPlace(is);

place.process(payload);
assertFalse(payload.getStringParameter("EMISSARY_VERSION").contains(versionDate), "the date should not be added to the version");
assertEquals(payload.getStringParameter("EMISSARY_VERSION"), version.getVersion(), "the version should be added");
}
}
10 changes: 10 additions & 0 deletions src/test/resources/emissary/place/VersionPlaceTest.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PLACE_NAME = "VersionPlaceTest"
SERVICE_NAME = "VERSION_TEST"
SERVICE_TYPE = "VERIFY"
SERVICE_DESCRIPTION = "add emissary version to payload"
SERVICE_COST = 50
SERVICE_QUALITY = 50

SERVICE_PROXY = "TEST"

INCLUDE_DATE = "false"