mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-12-24 01:09:48 +01:00
wip refactoring
This commit is contained in:
@@ -28,6 +28,8 @@ import lombok.EqualsAndHashCode;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a tag in a repository.
|
* Represents a tag in a repository.
|
||||||
*
|
*
|
||||||
@@ -75,7 +77,7 @@ public final class Tag {
|
|||||||
*
|
*
|
||||||
* @since 2.5.0
|
* @since 2.5.0
|
||||||
*/
|
*/
|
||||||
public Long getDate() {
|
public Optional<Long> getDate() {
|
||||||
return date;
|
return Optional.ofNullable(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,21 +21,16 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package sonia.scm.repository.api;
|
package sonia.scm.repository.api;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.eclipse.jgit.lib.ObjectId;
|
import org.eclipse.jgit.lib.ObjectId;
|
||||||
import org.eclipse.jgit.lib.ObjectLoader;
|
|
||||||
import org.eclipse.jgit.lib.ObjectReader;
|
|
||||||
import org.eclipse.jgit.lib.Ref;
|
import org.eclipse.jgit.lib.Ref;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
import org.eclipse.jgit.revwalk.RevTag;
|
|
||||||
import org.eclipse.jgit.transport.ReceiveCommand;
|
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.TemporaryFolder;
|
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockedStatic;
|
import org.mockito.MockedStatic;
|
||||||
@@ -47,19 +42,19 @@ import sonia.scm.repository.Tag;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.empty;
|
import static org.hamcrest.Matchers.empty;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link GitHookTagProvider}.
|
* Unit tests for {@link GitHookTagProvider}.
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class GitHookTagProviderTest{
|
public class GitHookTagProviderTest {
|
||||||
|
|
||||||
private static final String ZERO = ObjectId.zeroId().getName();
|
private static final String ZERO = ObjectId.zeroId().getName();
|
||||||
|
|
||||||
@@ -76,12 +71,12 @@ public class GitHookTagProviderTest{
|
|||||||
private ObjectId objectId;
|
private ObjectId objectId;
|
||||||
|
|
||||||
private List<ReceiveCommand> commands;
|
private List<ReceiveCommand> commands;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up mocks for upcoming tests.
|
* Set up mocks for upcoming tests.
|
||||||
*/
|
*/
|
||||||
@Before
|
@Before
|
||||||
public void setUpMocks(){
|
public void setUpMocks() {
|
||||||
commands = Lists.newArrayList(command);
|
commands = Lists.newArrayList(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +88,7 @@ public class GitHookTagProviderTest{
|
|||||||
try (MockedStatic<GitUtil> dummy = Mockito.mockStatic(GitUtil.class)) {
|
try (MockedStatic<GitUtil> dummy = Mockito.mockStatic(GitUtil.class)) {
|
||||||
String revision = "86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1";
|
String revision = "86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1";
|
||||||
Long timestamp = 1339416344000L;
|
Long timestamp = 1339416344000L;
|
||||||
String tagName = "test-tag";
|
String tagName = "1.0.0";
|
||||||
String ref = "refs/tags/" + tagName;
|
String ref = "refs/tags/" + tagName;
|
||||||
|
|
||||||
dummy.when(() -> GitUtil.getTagTime(repository, gitRef)).thenReturn(timestamp);
|
dummy.when(() -> GitUtil.getTagTime(repository, gitRef)).thenReturn(timestamp);
|
||||||
@@ -106,19 +101,29 @@ public class GitHookTagProviderTest{
|
|||||||
assertThat(provider.getDeletedTags(), empty());
|
assertThat(provider.getDeletedTags(), empty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests {@link GitHookTagProvider#getDeletedTags()}.
|
* Tests {@link GitHookTagProvider#getDeletedTags()}.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGetDeletedTags() {
|
public void testGetDeletedTags() {
|
||||||
String revision = "b2002b64013e54b78eac251df0672bd5d6a83aa7";
|
try (MockedStatic<GitUtil> dummy = Mockito.mockStatic(GitUtil.class)) {
|
||||||
GitHookTagProvider provider = createProvider(ReceiveCommand.Type.DELETE, "refs/tags/1.0.0", ZERO, revision);
|
String revision = "b2002b64013e54b78eac251df0672bd5d6a83aa7";
|
||||||
|
Long timestamp = 1339416344000L;
|
||||||
assertThat(provider.getCreatedTags(), empty());
|
String tagName = "1.0.0";
|
||||||
assertTag("1.0.0", revision, null, provider.getDeletedTags());
|
String ref = "refs/tags/" + tagName;
|
||||||
|
|
||||||
|
dummy.when(() -> GitUtil.getTagTime(repository, gitRef)).thenReturn(timestamp);
|
||||||
|
dummy.when(() -> GitUtil.getTagName(ref)).thenReturn(tagName);
|
||||||
|
dummy.when(() -> GitUtil.getId(ObjectId.fromString(revision))).thenReturn(revision);
|
||||||
|
|
||||||
|
GitHookTagProvider provider = createProvider(ReceiveCommand.Type.DELETE, ref, ZERO, revision);
|
||||||
|
|
||||||
|
assertThat(provider.getCreatedTags(), empty());
|
||||||
|
assertTag("1.0.0", revision, 1339416344000L, provider.getDeletedTags());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests {@link GitHookTagProvider} with a branch ref instead of a tag.
|
* Tests {@link GitHookTagProvider} with a branch ref instead of a tag.
|
||||||
*/
|
*/
|
||||||
@@ -126,7 +131,7 @@ public class GitHookTagProviderTest{
|
|||||||
public void testWithBranch() {
|
public void testWithBranch() {
|
||||||
String revision = "b2002b64013e54b78eac251df0672bd5d6a83aa7";
|
String revision = "b2002b64013e54b78eac251df0672bd5d6a83aa7";
|
||||||
GitHookTagProvider provider = createProvider(ReceiveCommand.Type.CREATE, "refs/heads/1.0.0", revision, revision);
|
GitHookTagProvider provider = createProvider(ReceiveCommand.Type.CREATE, "refs/heads/1.0.0", revision, revision);
|
||||||
|
|
||||||
assertThat(provider.getCreatedTags(), empty());
|
assertThat(provider.getCreatedTags(), empty());
|
||||||
assertThat(provider.getDeletedTags(), empty());
|
assertThat(provider.getDeletedTags(), empty());
|
||||||
}
|
}
|
||||||
@@ -136,24 +141,38 @@ public class GitHookTagProviderTest{
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateTags() {
|
public void testUpdateTags() {
|
||||||
String newId = "b2002b64013e54b78eac251df0672bd5d6a83aa7";
|
try (MockedStatic<GitUtil> dummy = Mockito.mockStatic(GitUtil.class)) {
|
||||||
String oldId = "e0f2be968b147ff7043684a7715d2fe852553db4";
|
String newRevision = "b2002b64013e54b78eac251df0672bd5d6a83aa7";
|
||||||
|
Long newTimestamp = 1339416344000L;
|
||||||
|
String newTagName = "1.0.0";
|
||||||
|
String newRef = "refs/tags/" + newTagName;
|
||||||
|
|
||||||
GitHookTagProvider provider = createProvider(ReceiveCommand.Type.UPDATE, "refs/tags/1.0.0", newId, oldId);
|
String oldRevision = "e0f2be968b147ff7043684a7715d2fe852553db4";
|
||||||
assertTag("1.0.0", newId, null, provider.getCreatedTags());
|
String oldTagName = "0.9.0";
|
||||||
assertTag("1.0.0", oldId, null, provider.getDeletedTags());
|
|
||||||
|
dummy.when(() -> GitUtil.getTagTime(repository, gitRef)).thenReturn(newTimestamp);
|
||||||
|
dummy.when(() -> GitUtil.getTagName(newRef)).thenReturn(newTagName);
|
||||||
|
dummy.when(() -> GitUtil.getId(ObjectId.fromString(newRevision))).thenReturn(newRevision);
|
||||||
|
|
||||||
|
dummy.when(() -> GitUtil.getId(ObjectId.fromString(oldRevision))).thenReturn(oldRevision);
|
||||||
|
|
||||||
|
GitHookTagProvider provider = createProvider(ReceiveCommand.Type.UPDATE, newRef, newRevision, oldRevision);
|
||||||
|
|
||||||
|
assertTag(newTagName, newRevision, newTimestamp, provider.getCreatedTags());
|
||||||
|
assertTag(oldTagName, oldRevision, null, provider.getDeletedTags());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertTag(String name, String revision, Long date, List<Tag> tags){
|
private void assertTag(String name, String revision, Long date, List<Tag> tags) {
|
||||||
assertNotNull(tags);
|
assertNotNull(tags);
|
||||||
assertFalse(tags.isEmpty());
|
assertFalse(tags.isEmpty());
|
||||||
assertEquals(1, tags.size());
|
assertEquals(1, tags.size());
|
||||||
Tag tag = tags.get(0);
|
Tag tag = tags.get(0);
|
||||||
assertEquals(name, tag.getName());
|
assertEquals(name, tag.getName());
|
||||||
assertEquals(revision, tag.getRevision());
|
assertEquals(revision, tag.getRevision());
|
||||||
assertEquals(date, tag.getDate());
|
assertEquals(date, tag.getDate().orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
private GitHookTagProvider createProvider(ReceiveCommand.Type type, String ref, String newId, String oldId) {
|
private GitHookTagProvider createProvider(ReceiveCommand.Type type, String ref, String newId, String oldId) {
|
||||||
when(command.getNewId()).thenReturn(ObjectId.fromString(newId));
|
when(command.getNewId()).thenReturn(ObjectId.fromString(newId));
|
||||||
when(command.getOldId()).thenReturn(ObjectId.fromString(oldId));
|
when(command.getOldId()).thenReturn(ObjectId.fromString(oldId));
|
||||||
|
|||||||
@@ -55,9 +55,9 @@ public class GitTagsCommandTest extends AbstractGitCommandTestBase {
|
|||||||
final List<Tag> tags = tagsCommand.getTags();
|
final List<Tag> tags = tagsCommand.getTags();
|
||||||
assertThat(tags).hasSize(2);
|
assertThat(tags).hasSize(2);
|
||||||
assertThat(tags.get(0).getName()).isEqualTo("1.0.0");
|
assertThat(tags.get(0).getName()).isEqualTo("1.0.0");
|
||||||
assertThat(tags.get(0).getDate()).isEqualTo(1598348105000L); // Annotated - Take tag date
|
assertThat(tags.get(0).getDate()).contains(1598348105000L); // Annotated - Take tag date
|
||||||
assertThat(tags.get(1).getName()).isEqualTo("test-tag");
|
assertThat(tags.get(1).getName()).isEqualTo("test-tag");
|
||||||
assertThat(tags.get(1).getDate()).isEqualTo(1339416344000L); // Lightweight - Take commit date
|
assertThat(tags.get(1).getDate()).contains(1339416344000L); // Lightweight - Take commit date
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -85,12 +85,12 @@ public class HgHookTagProviderTest {
|
|||||||
Tag t1 = tags.get(0);
|
Tag t1 = tags.get(0);
|
||||||
assertEquals("1", t1.getRevision());
|
assertEquals("1", t1.getRevision());
|
||||||
assertEquals("1.0.0", t1.getName());
|
assertEquals("1.0.0", t1.getName());
|
||||||
Assertions.assertThat(t1.getDate()).isEqualTo(Long.MIN_VALUE);
|
Assertions.assertThat(t1.getDate()).contains(Long.MIN_VALUE);
|
||||||
|
|
||||||
Tag t2 = tags.get(1);
|
Tag t2 = tags.get(1);
|
||||||
assertEquals("2", t2.getRevision());
|
assertEquals("2", t2.getRevision());
|
||||||
assertEquals("2.0.0", t2.getName());
|
assertEquals("2.0.0", t2.getName());
|
||||||
Assertions.assertThat(t2.getDate()).isEqualTo(Long.MAX_VALUE);
|
Assertions.assertThat(t2.getDate()).contains(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareChangesets(Changeset... changesets){
|
private void prepareChangesets(Changeset... changesets){
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class HgTagsCommandTest extends AbstractHgCommandTestBase {
|
|||||||
final List<Tag> tags = hgTagsCommand.getTags();
|
final List<Tag> tags = hgTagsCommand.getTags();
|
||||||
assertThat(tags).hasSize(1);
|
assertThat(tags).hasSize(1);
|
||||||
assertThat(tags.get(0).getName()).isEqualTo("tip");
|
assertThat(tags.get(0).getName()).isEqualTo("tip");
|
||||||
assertThat(tags.get(0).getDate()).isEqualTo(1339586381000L);
|
assertThat(tags.get(0).getDate()).contains(1339586381000L);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import sonia.scm.web.EdisonHalAppender;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static de.otto.edison.hal.Embedded.embeddedBuilder;
|
import static de.otto.edison.hal.Embedded.embeddedBuilder;
|
||||||
import static de.otto.edison.hal.Link.link;
|
import static de.otto.edison.hal.Link.link;
|
||||||
@@ -67,7 +68,7 @@ public abstract class TagToTagDtoMapper extends HalAppenderMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Named("mapDate")
|
@Named("mapDate")
|
||||||
Instant map(Long value) {
|
Instant map(Optional<Long> value) {
|
||||||
return value == null ? null : Instant.ofEpochMilli(value);
|
return value.map(Instant::ofEpochMilli).orElse(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user