test that ArtifactEntity properties created, lastUpdated, lastUsed retain time when saved and read to/from db
@@ -10,6 +10,8 @@ import org.carlspring.strongbox.domain.ArtifactEntry;
import org.carlspring.strongbox.services.ArtifactEntryService;
import javax.inject.Inject;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@@ -362,6 +364,80 @@ public class ArtifactEntryServiceTest
assertThat(c).isEqualTo(Long.valueOf(1));
}
+ @Test
+ public void saveEntityCreationDateShouldBeGeneratedAutomaticallyAndRemainUnchanged(TestInfo testInfo)
+ {
+ final String groupId = getGroupId(GROUP_ID, testInfo);
+
+ final ArtifactEntry artifactEntry = createArtifactEntry(groupId);
+
+ assertThat(artifactEntry.getCreated()).isNull();
+ assertThat(artifactEntry.getLastUpdated()).isNull();
+
+ final ArtifactEntry firstTimeSavedArtifactEntry = save(artifactEntry);
+ final String artifactEntryId = firstTimeSavedArtifactEntry.getObjectId();
+ final Date creationDate = firstTimeSavedArtifactEntry.getCreated();
+
+ final ArtifactEntry firstTimeReadFromDatabase = artifactEntryService.findOne(artifactEntryId)
+ .orElse(null);
+
+ assertThat(firstTimeReadFromDatabase).isNotNull();
+ assertThat(firstTimeReadFromDatabase.getCreated()).isEqualTo(creationDate);
+
+ artifactEntry.setDownloadCount(1);
+ save(firstTimeReadFromDatabase);
+
+ final ArtifactEntry secondTimeReadFromDatabase = artifactEntryService.findOne(artifactEntryId)
+ .orElse(null);
+
+ assertThat(secondTimeReadFromDatabase).isNotNull();
+ assertThat(secondTimeReadFromDatabase.getCreated()).isEqualTo(creationDate);
+ }
+
+ @Test
+ public void saveEntityCreatedLastUsedLastUpdatedPropertiesShouldRetainTime(TestInfo testInfo) throws ParseException {
+ final String groupId = getGroupId(GROUP_ID, testInfo);
+
+ final ArtifactEntry artifactEntry = createArtifactEntry(groupId);
+ final ArtifactEntry firstTimeSavedArtifactEntry = save(artifactEntry);
+ final String artifactEntryId = firstTimeSavedArtifactEntry.getObjectId();
+
+ final ArtifactEntry firstTimeReadFromDatabase = artifactEntryService.findOne(artifactEntryId)
+ .orElse(null);
+ assertThat(firstTimeReadFromDatabase).isNotNull();
+
+ final Date sampleDate = createSampleDate();
+
+ firstTimeReadFromDatabase.setCreated(sampleDate);
+ firstTimeReadFromDatabase.setLastUpdated(sampleDate);
+ firstTimeReadFromDatabase.setLastUsed(sampleDate);
+
+ save(firstTimeReadFromDatabase);
+ final ArtifactEntry secondTimeReadFromDatabase = artifactEntryService.findOne(artifactEntryId)
+ .orElse(null);
+
+ assertThat(secondTimeReadFromDatabase).isNotNull();
+
+ assertThat(secondTimeReadFromDatabase.getCreated()).isEqualTo(sampleDate);
+ assertThat(secondTimeReadFromDatabase.getLastUpdated()).isEqualTo(sampleDate);
+ assertThat(secondTimeReadFromDatabase.getLastUsed()).isEqualTo(sampleDate);
+ }
+
+ private Date createSampleDate() throws ParseException {
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-10-31 13:15:50");
+ }
+
+ private ArtifactEntry createArtifactEntry(String groupId)
+ {
+ final ArtifactEntry artifactEntry = new ArtifactEntry();
+
+ artifactEntry.setStorageId(STORAGE_ID);
+ artifactEntry.setRepositoryId(REPOSITORY_ID);
+ artifactEntry.setArtifactCoordinates(createArtifactCoordinates(groupId, ARTIFACT_ID + "1234", "1.2.3", "jar"));
+
+ return artifactEntry;
+ }
+
private void displayAllEntries(final String groupId)
{
List<ArtifactEntry> result = findAll(groupId);
