Skip to content

Commit

Permalink
updates to VersionPlace for less code-reusage downstream (NationalSec…
Browse files Browse the repository at this point in the history
  • Loading branch information
sambish5 authored Oct 18, 2024
1 parent 8075dc7 commit 16aac99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
38 changes: 27 additions & 11 deletions src/main/java/emissary/place/VersionPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import emissary.core.IBaseDataObject;
import emissary.core.ResourceException;
import emissary.util.GitRepositoryState;
import emissary.util.Version;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -14,12 +13,9 @@ public class VersionPlace extends ServiceProviderPlace {
private static final String EMISSARY_VERSION_HASH = "EMISSARY_VERSION_HASH";
private boolean includeDate;
private boolean useAbbrevHash;
private final Version version = new Version();
private String formattedVersion;
private String versionHash;

private GitRepositoryState gitRepositoryState;

/**
* Create the place from the specified config file or resource
*
Expand Down Expand Up @@ -63,12 +59,12 @@ public VersionPlace() throws IOException {
}

private void configurePlace() {
this.gitRepositoryState = initGitRepositoryState();
GitRepositoryState gitRepositoryState = initGitRepositoryState();

includeDate = configG.findBooleanEntry("INCLUDE_DATE", true);
useAbbrevHash = configG.findBooleanEntry("USE_ABBREV_HASH", true);
formattedVersion = getEmissaryVersion();
versionHash = getEmissaryVersionHash();
formattedVersion = getVersion(gitRepositoryState);
versionHash = getVersionHash(gitRepositoryState);
}

@Override
Expand All @@ -81,17 +77,21 @@ GitRepositoryState initGitRepositoryState() {
return GitRepositoryState.getRepositoryState();
}

private String getEmissaryVersion() {
protected String getVersion(GitRepositoryState gitRepositoryState) {
if (includeDate) {
// version with date & time information
return version.getVersion() + "-" + version.getTimestamp().replaceAll("\\D", "");
// changes format of date from 2024-09-23T10:41:18-0400, to 20240923104118
String buildTime = gitRepositoryState.getBuildTime();
int cutEndMark = buildTime.lastIndexOf(":") + 3;
String formattedDate = buildTime.substring(0, cutEndMark).replaceAll("\\D", "");
return gitRepositoryState.getBuildVersion() + "-" + formattedDate;
} else {
// adds just version
return version.getVersion();
return gitRepositoryState.getBuildVersion();
}
}

private String getEmissaryVersionHash() {
protected String getVersionHash(GitRepositoryState gitRepositoryState) {
if (useAbbrevHash) {
// first 7 chars of commit hash
return gitRepositoryState.getCommitIdAbbrev();
Expand All @@ -100,4 +100,20 @@ private String getEmissaryVersionHash() {
return gitRepositoryState.getCommitId();
}
}

public void setIncludeDate(Boolean includeDate) {
this.includeDate = includeDate;
}

public void setAbbrevHash(Boolean useAbbrevHash) {
this.useAbbrevHash = useAbbrevHash;
}

public boolean isIncludeDate() {
return includeDate;
}

public boolean isUseAbbrevHash() {
return useAbbrevHash;
}
}
18 changes: 6 additions & 12 deletions src/test/java/emissary/place/VersionPlaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import emissary.core.ResourceException;
import emissary.test.core.junit5.UnitTest;
import emissary.util.GitRepositoryState;
import emissary.util.Version;
import emissary.util.io.ResourceReader;

import org.junit.jupiter.api.AfterEach;
Expand All @@ -26,18 +25,15 @@ class VersionPlaceTest extends UnitTest {
private IBaseDataObject payload;
@Nullable
private VersionPlace place;
private Version version;
@Nullable
private String versionDate;
private Path gitRepositoryFile;
private GitRepositoryState testGitRepoState;

@Override
@BeforeEach
public void setUp() throws Exception {
payload = DataObjectFactory.getInstance();
version = new Version();
versionDate = version.getTimestamp().replaceAll("\\D", "");
gitRepositoryFile = Paths.get(new ResourceReader().getResource("emissary/util/test.git.properties").toURI());
testGitRepoState = GitRepositoryState.getRepositoryState(gitRepositoryFile);
}

@Override
Expand All @@ -47,7 +43,6 @@ public void tearDown() throws Exception {
place.shutDown();
place = null;
payload = null;
versionDate = null;
}

@Test
Expand All @@ -56,7 +51,7 @@ void testAddVersionToPayload() throws ResourceException, IOException {
place = new MyVersionPlace();

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

Expand All @@ -67,8 +62,8 @@ void testAddVersionWithoutDate() throws ResourceException, IOException {
place = new MyVersionPlace(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");
assertFalse(payload.getStringParameter("EMISSARY_VERSION").contains("-20240828141716"), "the date should not be added to the version");
assertEquals(testGitRepoState.getBuildVersion(), payload.getStringParameter("EMISSARY_VERSION"), "the version should be added");
}

@Test
Expand All @@ -77,8 +72,7 @@ void testAddVersionHash() throws ResourceException, IOException {
place = new MyVersionPlace();

place.process(payload);
assertEquals(payload.getStringParameter("EMISSARY_VERSION_HASH").substring(0, 7),
GitRepositoryState.getRepositoryState(gitRepositoryFile).getCommitIdAbbrev(),
assertEquals(testGitRepoState.getCommitIdAbbrev(), payload.getStringParameter("EMISSARY_VERSION_HASH").substring(0, 7),
"EMISSARY_VERSION_HASH should contain (at least) the abbreviated hash");
}

Expand Down

0 comments on commit 16aac99

Please sign in to comment.