-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
133 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
core/src/main/java/com/github/stcarolas/enki/core/util/Spy4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.github.stcarolas.enki.core.util; | ||
|
||
import static io.vavr.API.*; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.vavr.Function4; | ||
|
||
public class Spy4<T1,T2,T3, T4, RETURN> { | ||
private Function4<T1,T2,T3, T4, RETURN> fn; | ||
private Boolean isCalled = false; | ||
private AtomicInteger counter = new AtomicInteger(0); | ||
private T1 arg1; | ||
private T2 arg2; | ||
private T3 arg3; | ||
private T4 arg4; | ||
|
||
public static <T1,T2,T3,T4,RETURN>Spy4<T1,T2,T3,T4,RETURN> spy(Function4<T1,T2,T3,T4,RETURN> fn){ | ||
var spy = new Spy4<T1,T2,T3,T4,RETURN>(); | ||
spy.fn = fn; | ||
return spy; | ||
} | ||
|
||
public Function4<T1,T2,T3,T4,RETURN> fn(){ | ||
return Function((arg1, arg2, arg3, arg4) -> { | ||
isCalled = true; | ||
this.arg1 = arg1; | ||
this.arg2 = arg2; | ||
this.arg3 = arg3; | ||
this.arg4 = arg4; | ||
counter.incrementAndGet(); | ||
return fn.apply(arg1,arg2,arg3,arg4); | ||
}); | ||
} | ||
|
||
public Boolean isCalled(){ | ||
return isCalled; | ||
} | ||
|
||
public T1 arg1(){ | ||
return arg1; | ||
} | ||
|
||
public T2 arg2(){ | ||
return arg2; | ||
} | ||
|
||
public T3 arg3(){ | ||
return arg3; | ||
} | ||
|
||
public T4 arg4(){ | ||
return arg4; | ||
} | ||
|
||
} |
92 changes: 74 additions & 18 deletions
92
...github/stcarolas/enki/core/provider/strategies/download/GitCloneDownloadStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,103 @@ | ||
package com.github.stcarolas.enki.core.provider.strategies.download; | ||
|
||
import static io.vavr.API.Function; | ||
import static io.vavr.API.None; | ||
import static io.vavr.API.Some; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.File; | ||
import java.util.function.Supplier; | ||
|
||
import com.github.stcarolas.enki.core.Repo; | ||
import com.github.stcarolas.enki.core.TestRepo; | ||
import com.github.stcarolas.enki.core.transport.DefaultTransportConfigCallback; | ||
import com.github.stcarolas.enki.core.util.Spy4; | ||
|
||
import org.eclipse.jgit.api.CloneCommand; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import static org.mockito.Mockito.*; | ||
import static io.vavr.API.*; | ||
|
||
// todo test git clone on subfunctions errors | ||
public class GitCloneDownloadStrategyTest { | ||
|
||
Repo repo = new TestRepo(); | ||
String url = "url"; | ||
GitCloneDownloadStrategy<Repo> workingStrategy = | ||
(GitCloneDownloadStrategy<Repo>) GitCloneDownloadStrategy.GitSshClone(repo, url); | ||
File dirMock = mock(File.class); | ||
DefaultTransportConfigCallback transportMock = mock(DefaultTransportConfigCallback.class); | ||
CloneCommand cloneCommandMock = mock(CloneCommand.class); | ||
|
||
@Test | ||
public void test_cloneFn() { | ||
var cloneCommandMock = mock(CloneCommand.class); | ||
when(cloneCommandMock.setURI(any())).thenReturn(cloneCommandMock); | ||
when(cloneCommandMock.setDirectory(any())).thenReturn(cloneCommandMock); | ||
var url = "url"; | ||
var dir = mock(File.class); | ||
var transport = mock(DefaultTransportConfigCallback.class); | ||
var command = GitCloneDownloadStrategy.cloneFn | ||
.apply( ()-> transport, () -> cloneCommandMock, url, dir ); | ||
when(cloneCommandMock.setTransportConfigCallback(any())).thenReturn(cloneCommandMock); | ||
var command = GitCloneDownloadStrategy.cloneFn.apply( | ||
()-> transportMock, () -> cloneCommandMock, url, dirMock | ||
); | ||
Assertions.assertEquals(cloneCommandMock, command); | ||
verify(cloneCommandMock).setTransportConfigCallback(transportMock); | ||
verify(cloneCommandMock).setURI(url); | ||
verify(cloneCommandMock).setDirectory(dir); | ||
verify(cloneCommandMock).setTransportConfigCallback(transport); | ||
verify(cloneCommandMock).setDirectory(dirMock); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
var strategy = GitCloneDownloadStrategy.builder().build() | ||
public void test_get_for_none_url() { | ||
var clonedRepo = GitCloneDownloadStrategy.builder().build() | ||
.withSshUrl(None()) | ||
.withRepository(Some(repo)) | ||
.withCloneCommand( () -> mock(CloneCommand.class) ) | ||
.withClone( ($1,$2,$3,$4) -> mock(CloneCommand.class) ) | ||
.withTransport( () -> mock(DefaultTransportConfigCallback.class) ); | ||
.withCloneCommand(() -> cloneCommandMock) | ||
.withClone(($1,$2,$3,$4) -> cloneCommandMock) | ||
.withTransport(() -> transportMock) | ||
.get(); | ||
|
||
assertNull(clonedRepo); | ||
verifyNoInteractions(cloneCommandMock); | ||
verifyNoInteractions(transportMock); | ||
} | ||
|
||
@Test | ||
public void test_get_for_none_repo() { | ||
var clonedRepo = GitCloneDownloadStrategy.builder().build() | ||
.withSshUrl(Some(url)) | ||
.withRepository(None()) | ||
.withCloneCommand(() -> cloneCommandMock) | ||
.withClone(($1,$2,$3,$4) -> cloneCommandMock) | ||
.withTransport(() -> transportMock) | ||
.get(); | ||
|
||
assertNull(clonedRepo); | ||
verifyNoInteractions(cloneCommandMock); | ||
verifyNoInteractions(transportMock); | ||
} | ||
|
||
@Test | ||
public void test_get_using_right_args() { | ||
var targetDir = mock(File.class); | ||
var repoMock = mock(Repo.class); | ||
when(repoMock.directory()).thenReturn(targetDir); | ||
|
||
var cloneFnSpy = Spy4.spy(($1,$2,$3,$4) -> cloneCommandMock); | ||
var cloneCommandSupplier = Function(() -> cloneCommandMock); | ||
var transportSupplier = Function(() -> transportMock); | ||
|
||
var cloneRepo = GitCloneDownloadStrategy.builder().build() | ||
.withSshUrl(Some(url)) | ||
.withRepository(Some(repoMock)) | ||
.withCloneCommand(cloneCommandSupplier) | ||
.withClone(($1,$2,$3,$4)-> cloneFnSpy.fn().apply($1,$2,$3,$4)) | ||
.withTransport(transportSupplier) | ||
.get(); | ||
|
||
assertEquals(transportSupplier, cloneFnSpy.arg1()); | ||
assertEquals(cloneCommandSupplier, cloneFnSpy.arg2()); | ||
assertEquals(url, cloneFnSpy.arg3()); | ||
assertEquals(targetDir, cloneFnSpy.arg4()); | ||
assertEquals(targetDir, cloneRepo); | ||
} | ||
|
||
} |