mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 08:55:44 +01:00
Correct instance mocks
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package sonia.scm.repository.update;
|
||||
|
||||
import com.google.inject.Injector;
|
||||
import sonia.scm.repository.update.MigrationStrategy.Instance;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -12,12 +13,12 @@ import static org.mockito.Mockito.when;
|
||||
class MigrationStrategyMock {
|
||||
|
||||
static Injector init() {
|
||||
Map<Class, MigrationStrategy.Instance> mocks = new HashMap<>();
|
||||
Map<Class, Instance> mocks = new HashMap<>();
|
||||
Injector mock = mock(Injector.class);
|
||||
when(
|
||||
mock.getInstance(any(Class.class)))
|
||||
.thenAnswer(
|
||||
invocationOnMock -> mocks.getOrDefault(invocationOnMock.getArgument(0), mock(invocationOnMock.getArgument(0)))
|
||||
invocationOnMock -> mocks.computeIfAbsent(invocationOnMock.getArgument(0), key -> mock((Class<Instance>) key))
|
||||
);
|
||||
return mock;
|
||||
}
|
||||
|
||||
@@ -49,6 +49,8 @@ class XmlRepositoryV1UpdateStepTest {
|
||||
|
||||
@Captor
|
||||
ArgumentCaptor<Repository> storeCaptor;
|
||||
@Captor
|
||||
ArgumentCaptor<Path> locationCaptor;
|
||||
|
||||
@InjectMocks
|
||||
XmlRepositoryV1UpdateStep updateStep;
|
||||
@@ -61,6 +63,61 @@ class XmlRepositoryV1UpdateStepTest {
|
||||
@Nested
|
||||
class WithExistingDatabase {
|
||||
|
||||
/**
|
||||
* Creates the following v1 repositories in the temp dir:
|
||||
* <pre>
|
||||
* <repository>
|
||||
* <properties/>
|
||||
* <contact>arthur@dent.uk</contact>
|
||||
* <creationDate>1558423492071</creationDate>
|
||||
* <description>A repository with two folders.</description>
|
||||
* <id>3b91caa5-59c3-448f-920b-769aaa56b761</id>
|
||||
* <name>one/directory</name>
|
||||
* <public>false</public>
|
||||
* <archived>false</archived>
|
||||
* <type>git</type>
|
||||
* </repository>
|
||||
* <repository>
|
||||
* <properties/>
|
||||
* <contact>arthur@dent.uk</contact>
|
||||
* <creationDate>1558423543716</creationDate>
|
||||
* <description>A repository in deeply nested folders.</description>
|
||||
* <id>c1597b4f-a9f0-49f7-ad1f-37d3aae1c55f</id>
|
||||
* <name>some/more/directories/than/one</name>
|
||||
* <public>false</public>
|
||||
* <archived>false</archived>
|
||||
* <type>git</type>
|
||||
* </repository>
|
||||
* <repository>
|
||||
* <properties/>
|
||||
* <contact>arthur@dent.uk</contact>
|
||||
* <creationDate>1558423440258</creationDate>
|
||||
* <description>A simple repository without directories.</description>
|
||||
* <id>454972da-faf9-4437-b682-dc4a4e0aa8eb</id>
|
||||
* <lastModified>1558425918578</lastModified>
|
||||
* <name>simple</name>
|
||||
* <permissions>
|
||||
* <groupPermission>true</groupPermission>
|
||||
* <name>mice</name>
|
||||
* <type>WRITE</type>
|
||||
* </permissions>
|
||||
* <permissions>
|
||||
* <groupPermission>false</groupPermission>
|
||||
* <name>dent</name>
|
||||
* <type>OWNER</type>
|
||||
* </permissions>
|
||||
* <permissions>
|
||||
* <groupPermission>false</groupPermission>
|
||||
* <name>trillian</name>
|
||||
* <type>READ</type>
|
||||
* </permissions>
|
||||
* <public>false</public>
|
||||
* <archived>false</archived>
|
||||
* <type>git</type>
|
||||
* <url>http://localhost:8081/scm/git/simple</url>
|
||||
* </repository>
|
||||
* </pre>
|
||||
*/
|
||||
@BeforeEach
|
||||
void createV1Home(@TempDirectory.TempDir Path tempDir) throws IOException {
|
||||
ZippedRepositoryTestBase.extract(tempDir.toFile(), "sonia/scm/repository/update/scm-home.v1.zip");
|
||||
@@ -68,11 +125,11 @@ class XmlRepositoryV1UpdateStepTest {
|
||||
|
||||
@BeforeEach
|
||||
void captureStoredRepositories() {
|
||||
doNothing().when(repositoryDAO).add(storeCaptor.capture());
|
||||
doNothing().when(repositoryDAO).add(storeCaptor.capture(), locationCaptor.capture());
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createMigrationPlan(@TempDirectory.TempDir Path tempDir) {
|
||||
void createMigrationPlan() {
|
||||
lenient().when(migrationStrategyDao.get("3b91caa5-59c3-448f-920b-769aaa56b761")).thenReturn(of(MOVE));
|
||||
lenient().when(migrationStrategyDao.get("c1597b4f-a9f0-49f7-ad1f-37d3aae1c55f")).thenReturn(of(COPY));
|
||||
lenient().when(migrationStrategyDao.get("454972da-faf9-4437-b682-dc4a4e0aa8eb")).thenReturn(of(INLINE));
|
||||
@@ -81,7 +138,7 @@ class XmlRepositoryV1UpdateStepTest {
|
||||
@Test
|
||||
void shouldCreateNewRepositories() throws JAXBException {
|
||||
updateStep.doUpdate();
|
||||
verify(repositoryDAO, times(3)).add(any());
|
||||
verify(repositoryDAO, times(3)).add(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,6 +204,17 @@ class XmlRepositoryV1UpdateStepTest {
|
||||
new RepositoryPermission("trillian", "READ", false)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldUseDirectoryFromStrategy(@TempDirectory.TempDir Path tempDir) throws JAXBException {
|
||||
Path targetDir = tempDir.resolve("someDir");
|
||||
MigrationStrategy.Instance strategyMock = injectorMock.getInstance(InlineMigrationStrategy.class);
|
||||
when(strategyMock.migrate("454972da-faf9-4437-b682-dc4a4e0aa8eb", "simple", "git")).thenReturn(targetDir);
|
||||
|
||||
updateStep.doUpdate();
|
||||
|
||||
assertThat(locationCaptor.getAllValues()).contains(targetDir);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user