remove unused move-method from ModifyCommand / create interface for CommandRequests with Author

This commit is contained in:
Eduard Heimbuch
2019-10-07 14:59:46 +02:00
parent 120416c4d6
commit cb6f1bfa22
10 changed files with 41 additions and 65 deletions

View File

@@ -95,17 +95,6 @@ public class ModifyCommandBuilder {
return this; return this;
} }
/**
* Move an existing file.
* @param sourcePath The path and the name of the file that should be moved.
* @param targetPath The new path and name the file should be moved to.
* @return This builder instance.
*/
public ModifyCommandBuilder moveFile(String sourcePath, String targetPath) {
request.addRequest(new ModifyCommandRequest.MoveFileRequest(sourcePath, targetPath));
return this;
}
/** /**
* Apply the changes and create a new commit with the given message and author. * Apply the changes and create a new commit with the given message and author.
* @return The revision of the new commit. * @return The revision of the new commit.

View File

@@ -5,10 +5,11 @@ import com.google.common.base.Objects;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import sonia.scm.Validateable; import sonia.scm.Validateable;
import sonia.scm.repository.Person; import sonia.scm.repository.Person;
import sonia.scm.repository.util.AuthorUtil.CommandWithAuthor;
import java.io.Serializable; import java.io.Serializable;
public class MergeCommandRequest implements Validateable, Resetable, Serializable, Cloneable { public class MergeCommandRequest implements Validateable, Resetable, Serializable, Cloneable, CommandWithAuthor {
private static final long serialVersionUID = -2650236557922431528L; private static final long serialVersionUID = -2650236557922431528L;

View File

@@ -13,7 +13,5 @@ public interface ModifyCommand {
void create(String toBeCreated, File file, boolean overwrite) throws IOException; void create(String toBeCreated, File file, boolean overwrite) throws IOException;
void modify(String path, File file) throws IOException; void modify(String path, File file) throws IOException;
void move(String sourcePath, String targetPath);
} }
} }

View File

@@ -5,6 +5,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import sonia.scm.Validateable; import sonia.scm.Validateable;
import sonia.scm.repository.Person; import sonia.scm.repository.Person;
import sonia.scm.repository.util.AuthorUtil.CommandWithAuthor;
import sonia.scm.util.IOUtil; import sonia.scm.util.IOUtil;
import java.io.File; import java.io.File;
@@ -13,7 +14,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class ModifyCommandRequest implements Resetable, Validateable { public class ModifyCommandRequest implements Resetable, Validateable, CommandWithAuthor {
private static final Logger LOG = LoggerFactory.getLogger(ModifyCommandRequest.class); private static final Logger LOG = LoggerFactory.getLogger(ModifyCommandRequest.class);
@@ -94,21 +95,6 @@ public class ModifyCommandRequest implements Resetable, Validateable {
} }
} }
public static class MoveFileRequest implements PartialRequest {
private final String sourcePath;
private final String targetPath;
public MoveFileRequest(String sourcePath, String targetPath) {
this.sourcePath = sourcePath;
this.targetPath = targetPath;
}
@Override
public void execute(ModifyCommand.Worker worker) {
worker.move(sourcePath, targetPath);
}
}
private abstract static class ContentModificationRequest implements PartialRequest { private abstract static class ContentModificationRequest implements PartialRequest {
private final File content; private final File content;

View File

@@ -3,19 +3,11 @@ package sonia.scm.repository.util;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.Subject;
import sonia.scm.repository.Person; import sonia.scm.repository.Person;
import sonia.scm.repository.spi.MergeCommandRequest;
import sonia.scm.repository.spi.ModifyCommandRequest;
import sonia.scm.user.User; import sonia.scm.user.User;
public class AuthorUtil { public class AuthorUtil {
public static void setAuthorIfNotAvailable(ModifyCommandRequest request) { public static void setAuthorIfNotAvailable(CommandWithAuthor request) {
if (request.getAuthor() == null) {
request.setAuthor(createAuthorFromSubject());
}
}
public static void setAuthorIfNotAvailable(MergeCommandRequest request) {
if (request.getAuthor() == null) { if (request.getAuthor() == null) {
request.setAuthor(createAuthorFromSubject()); request.setAuthor(createAuthorFromSubject());
} }
@@ -28,4 +20,10 @@ public class AuthorUtil {
String email = user.getMail(); String email = user.getMail();
return new Person(name, email); return new Person(name, email);
} }
public interface CommandWithAuthor {
Person getAuthor();
void setAuthor(Person person);
}
} }

View File

@@ -1,7 +1,6 @@
package sonia.scm.repository.api; package sonia.scm.repository.api;
import com.google.common.io.ByteSource; import com.google.common.io.ByteSource;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@@ -85,15 +84,6 @@ class ModifyCommandBuilderTest {
verify(worker).delete("toBeDeleted"); verify(worker).delete("toBeDeleted");
} }
@Test
void shouldExecuteMove() throws IOException {
initCommand()
.moveFile("source", "target")
.execute();
verify(worker).move("source", "target");
}
@Test @Test
void shouldExecuteCreateWithByteSourceContent() throws IOException { void shouldExecuteCreateWithByteSourceContent() throws IOException {
ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class);

View File

@@ -143,11 +143,6 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
contextBuilder.in(context.getRepository()); contextBuilder.in(context.getRepository());
return contextBuilder; return contextBuilder;
} }
@Override
public void move(String sourcePath, String targetPath) {
}
} }
private String throwInternalRepositoryException(String message, Exception e) { private String throwInternalRepositoryException(String message, Exception e) {

View File

@@ -84,10 +84,6 @@ public class HgModifyCommand implements ModifyCommand {
} }
} }
@Override
public void move(String sourcePath, String targetPath) {
}
private void createDirectories(Path targetFile) throws IOException { private void createDirectories(Path targetFile) throws IOException {
try { try {
Files.createDirectories(targetFile.getParent()); Files.createDirectories(targetFile.getParent());

View File

@@ -10,6 +10,7 @@ import sonia.scm.AlreadyExistsException;
import sonia.scm.NoChangesMadeException; import sonia.scm.NoChangesMadeException;
import sonia.scm.NotFoundException; import sonia.scm.NotFoundException;
import sonia.scm.repository.HgHookManager; import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgTestUtil;
import sonia.scm.repository.Person; import sonia.scm.repository.Person;
import sonia.scm.repository.util.WorkdirProvider; import sonia.scm.repository.util.WorkdirProvider;
import sonia.scm.web.HgRepositoryEnvironmentBuilder; import sonia.scm.web.HgRepositoryEnvironmentBuilder;
@@ -19,8 +20,6 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class HgModifyCommandTest extends AbstractHgCommandTestBase { public class HgModifyCommandTest extends AbstractHgCommandTestBase {
@@ -31,12 +30,16 @@ public class HgModifyCommandTest extends AbstractHgCommandTestBase {
@Before @Before
public void initHgModifyCommand() { public void initHgModifyCommand() {
HgHookManager hookManager = mock(HgHookManager.class); HgHookManager hookManager = HgTestUtil.createHookManager();
when(hookManager.getChallenge()).thenReturn("CHALLENGE");
when(hookManager.getCredentials()).thenReturn("SECRET:SECRET");
when(hookManager.createUrl()).thenReturn("http://localhost");
HgRepositoryEnvironmentBuilder environmentBuilder = new HgRepositoryEnvironmentBuilder(handler, hookManager); HgRepositoryEnvironmentBuilder environmentBuilder = new HgRepositoryEnvironmentBuilder(handler, hookManager);
hgModifyCommand = new HgModifyCommand(cmdContext, new SimpleHgWorkdirFactory(Providers.of(environmentBuilder), new WorkdirProvider())); SimpleHgWorkdirFactory workdirFactory = new SimpleHgWorkdirFactory(Providers.of(environmentBuilder), new WorkdirProvider()) {
@Override
public void configure(com.aragost.javahg.commands.PullCommand pullCommand) {
// we do not want to configure http hooks in this unit test
}
};
hgModifyCommand = new HgModifyCommand(cmdContext, workdirFactory
);
} }
@After @After

View File

@@ -3,6 +3,7 @@ package sonia.scm.api.v2.resources;
import com.github.sdorra.shiro.SubjectAware; import com.github.sdorra.shiro.SubjectAware;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import com.google.inject.util.Providers; import com.google.inject.util.Providers;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext; import org.apache.shiro.util.ThreadContext;
import org.jboss.resteasy.core.Dispatcher; import org.jboss.resteasy.core.Dispatcher;
@@ -24,6 +25,7 @@ import sonia.scm.repository.api.MergeDryRunCommandResult;
import sonia.scm.repository.api.RepositoryService; import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory; import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.repository.spi.MergeCommand; import sonia.scm.repository.spi.MergeCommand;
import sonia.scm.user.User;
import sonia.scm.web.VndMediaType; import sonia.scm.web.VndMediaType;
import java.net.URL; import java.net.URL;
@@ -32,6 +34,7 @@ import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static sonia.scm.repository.RepositoryTestData.createHeartOfGold; import static sonia.scm.repository.RepositoryTestData.createHeartOfGold;
@@ -105,6 +108,10 @@ public class MergeResourceTest extends RepositoryTestBase {
@Test @Test
void shouldHandleSuccessfulMerge() throws Exception { void shouldHandleSuccessfulMerge() throws Exception {
when(mergeCommand.merge(any())).thenReturn(MergeCommandResult.success()); when(mergeCommand.merge(any())).thenReturn(MergeCommandResult.success());
User user = createDummyUser("dummy");
PrincipalCollection collection = mock(PrincipalCollection.class);
when(subject.getPrincipals()).thenReturn(collection);
when(collection.oneByType(User.class)).thenReturn(user);
URL url = Resources.getResource("sonia/scm/api/v2/mergeCommand.json"); URL url = Resources.getResource("sonia/scm/api/v2/mergeCommand.json");
byte[] mergeCommandJson = Resources.toByteArray(url); byte[] mergeCommandJson = Resources.toByteArray(url);
@@ -122,6 +129,10 @@ public class MergeResourceTest extends RepositoryTestBase {
@Test @Test
void shouldHandleFailedMerge() throws Exception { void shouldHandleFailedMerge() throws Exception {
when(mergeCommand.merge(any())).thenReturn(MergeCommandResult.failure(asList("file1", "file2"))); when(mergeCommand.merge(any())).thenReturn(MergeCommandResult.failure(asList("file1", "file2")));
User user = createDummyUser("dummy");
PrincipalCollection collection = mock(PrincipalCollection.class);
when(subject.getPrincipals()).thenReturn(collection);
when(collection.oneByType(User.class)).thenReturn(user);
URL url = Resources.getResource("sonia/scm/api/v2/mergeCommand.json"); URL url = Resources.getResource("sonia/scm/api/v2/mergeCommand.json");
byte[] mergeCommandJson = Resources.toByteArray(url); byte[] mergeCommandJson = Resources.toByteArray(url);
@@ -189,5 +200,14 @@ public class MergeResourceTest extends RepositoryTestBase {
assertThat(response.getStatus()).isEqualTo(204); assertThat(response.getStatus()).isEqualTo(204);
} }
private User createDummyUser(String name) {
User user = new User();
user.setName(name);
user.setType("xml");
user.setPassword("secret");
user.setCreationDate(System.currentTimeMillis());
return user;
}
} }
} }