View Javadoc
1   package emissary.place;
2   
3   import emissary.core.DataObjectFactory;
4   import emissary.core.IBaseDataObject;
5   import emissary.core.ResourceException;
6   import emissary.test.core.junit5.UnitTest;
7   import emissary.util.GitRepositoryState;
8   import emissary.util.io.ResourceReader;
9   
10  import jakarta.annotation.Nullable;
11  import org.junit.jupiter.api.AfterEach;
12  import org.junit.jupiter.api.BeforeEach;
13  import org.junit.jupiter.api.Test;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.net.URISyntaxException;
18  import java.nio.file.Path;
19  import java.nio.file.Paths;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  
24  class VersionPlaceTest extends UnitTest {
25      @Nullable
26      private IBaseDataObject payload;
27      @Nullable
28      private VersionPlace place;
29      private Path gitRepositoryFile;
30      private GitRepositoryState testGitRepoState;
31  
32      @Override
33      @BeforeEach
34      public void setUp() throws Exception {
35          payload = DataObjectFactory.getInstance();
36          gitRepositoryFile = Paths.get(new ResourceReader().getResource("emissary/util/test.git.properties").toURI());
37          testGitRepoState = GitRepositoryState.getRepositoryState(gitRepositoryFile);
38      }
39  
40      @Override
41      @AfterEach
42      public void tearDown() throws Exception {
43          super.tearDown();
44          place.shutDown();
45          place = null;
46          payload = null;
47      }
48  
49      @Test
50      void testAddVersionToPayload() throws ResourceException, IOException {
51          // create the place, using the normal class cfg
52          place = new MyVersionPlace();
53  
54          place.process(payload);
55          assertEquals(testGitRepoState.getBuildVersion() + "-20240828141716", payload.getStringParameter("EMISSARY_VERSION"),
56                  "added version should contain the date.");
57      }
58  
59      @Test
60      void testAddVersionWithoutDate() throws ResourceException, IOException {
61          // create the place with the test cfg, having INCLUDE_DATE = "false"
62          InputStream is = new ResourceReader().getConfigDataAsStream(this.getClass());
63          place = new MyVersionPlace(is);
64  
65          place.process(payload);
66          assertFalse(payload.getStringParameter("EMISSARY_VERSION").contains("-20240828141716"), "the date should not be added to the version");
67          assertEquals(testGitRepoState.getBuildVersion(), payload.getStringParameter("EMISSARY_VERSION"), "the version should be added");
68      }
69  
70      @Test
71      void testAddVersionHash() throws ResourceException, IOException {
72          // create the place, using the normal class cfg
73          place = new MyVersionPlace();
74  
75          place.process(payload);
76          assertEquals(testGitRepoState.getCommitIdAbbrev(), payload.getStringParameter("EMISSARY_VERSION_HASH").substring(0, 7),
77                  "EMISSARY_VERSION_HASH should contain (at least) the abbreviated hash");
78      }
79  
80      @Test
81      void testVersionRegex() throws IOException, URISyntaxException {
82          // uses different test gitRepoState properties file with version number that matches default regex
83          // passes this file to getVersion() to verify version returned has no date
84          Path regexTestFile = Paths.get(new ResourceReader().getResource("emissary/util/test.version.regex.git.properties").toURI());
85          GitRepositoryState regexRepoState = GitRepositoryState.getRepositoryState(regexTestFile);
86  
87          // create the place, using the normal class cfg
88          place = new MyVersionPlace();
89          assertFalse(place.getVersion(regexRepoState).contains("-20240828141716"),
90                  "the date should not be added to the version due to the matching release (default) regex");
91      }
92  
93      class MyVersionPlace extends VersionPlace {
94          MyVersionPlace() throws IOException {
95              super(new ResourceReader().getConfigDataAsStream(VersionPlace.class));
96          }
97  
98          MyVersionPlace(InputStream is) throws IOException {
99              super(is);
100         }
101 
102         @Override
103         GitRepositoryState initGitRepositoryState() {
104             return GitRepositoryState.getRepositoryState(gitRepositoryFile);
105         }
106     }
107 }