Search in sources :

Example 6 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project che by eclipse.

the class MachineProviderImplTest method shouldAddCommonsSystemVolumesOnlyOnNonDevInstanceCreationFromSnapshot.

@Test
public void shouldAddCommonsSystemVolumesOnlyOnNonDevInstanceCreationFromSnapshot() throws Exception {
    String[] bindMountVolumesFromMachine = new String[] { "/my/bind/mount1:/from/host1", "/my/bind/mount2:/from/host2:ro", "/my/bind/mount3:/from/host3:ro,Z" };
    String[] volumesFromMachine = new String[] { "/projects", "/something", "/something/else" };
    String[] allMachinesSystemVolumes = new String[] { "/some/thing/else:/home/some/thing/else", "/other/path:/home/other/path", "/home/other/path2" };
    String[] devMachinesSystemVolumes = new String[] { "/etc:/tmp/etc:ro", "/some/thing:/home/some/thing", "/some/thing2:/home/some/thing2:ro,z", "/home/some/thing3" };
    String[] expectedBindMountVolumes = new String[] { "/my/bind/mount1:/from/host1", "/my/bind/mount2:/from/host2:ro", "/my/bind/mount3:/from/host3:ro,Z", "/some/thing/else:/home/some/thing/else", "/other/path:/home/other/path" };
    Map<String, Volume> expectedVolumes = Stream.of("/projects", "/something", "/something/else", "/home/other/path2").collect(toMap(Function.identity(), v -> new Volume()));
    provider = new MachineProviderBuilder().setDevMachineVolumes(new HashSet<>(asList(devMachinesSystemVolumes))).setAllMachineVolumes(new HashSet<>(asList(allMachinesSystemVolumes))).build();
    CheServiceImpl service = createService();
    service.setVolumes(Stream.concat(Stream.of(bindMountVolumesFromMachine), Stream.of(volumesFromMachine)).collect(Collectors.toList()));
    createInstanceFromSnapshot(service, false);
    ArgumentCaptor<CreateContainerParams> argumentCaptor = ArgumentCaptor.forClass(CreateContainerParams.class);
    verify(dockerConnector).createContainer(argumentCaptor.capture());
    String[] actualBindMountVolumes = argumentCaptor.getValue().getContainerConfig().getHostConfig().getBinds();
    Map<String, Volume> actualVolumes = argumentCaptor.getValue().getContainerConfig().getVolumes();
    assertEquals(actualVolumes, expectedVolumes);
    assertEqualsNoOrder(actualBindMountVolumes, expectedBindMountVolumes);
}
Also used : RemoveContainerParams(org.eclipse.che.plugin.docker.client.params.RemoveContainerParams) Arrays(java.util.Arrays) Listeners(org.testng.annotations.Listeners) InspectContainerParams(org.eclipse.che.plugin.docker.client.params.InspectContainerParams) DockerConnectorProvider(org.eclipse.che.plugin.docker.client.DockerConnectorProvider) Volume(org.eclipse.che.plugin.docker.client.json.Volume) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) DOCKER_FILE_TYPE(org.eclipse.che.plugin.docker.machine.DockerInstanceProvider.DOCKER_FILE_TYPE) Mockito.doThrow(org.mockito.Mockito.doThrow) Collections.singleton(java.util.Collections.singleton) Collectors.toMap(java.util.stream.Collectors.toMap) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Assert.assertFalse(org.testng.Assert.assertFalse) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) MachineConfig(org.eclipse.che.api.core.model.machine.MachineConfig) BeforeMethod(org.testng.annotations.BeforeMethod) RecipeRetriever(org.eclipse.che.api.machine.server.util.RecipeRetriever) Set(java.util.Set) Collectors(java.util.stream.Collectors) WindowsPathEscaper(org.eclipse.che.commons.lang.os.WindowsPathEscaper) MACHINE_SNAPSHOT_PREFIX(org.eclipse.che.plugin.docker.machine.DockerInstanceProvider.MACHINE_SNAPSHOT_PREFIX) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Stream(java.util.stream.Stream) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) TagParams(org.eclipse.che.plugin.docker.client.params.TagParams) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) UserSpecificDockerRegistryCredentialsProvider(org.eclipse.che.plugin.docker.client.UserSpecificDockerRegistryCredentialsProvider) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) DockerConnectorConfiguration(org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration) DataProvider(org.testng.annotations.DataProvider) Mock(org.mockito.Mock) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) ContainerCreated(org.eclipse.che.plugin.docker.client.json.ContainerCreated) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) StartContainerParams(org.eclipse.che.plugin.docker.client.params.StartContainerParams) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) RemoveImageParams(org.eclipse.che.plugin.docker.client.params.RemoveImageParams) HashSet(java.util.HashSet) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor) ArgumentCaptor(org.mockito.ArgumentCaptor) ContainerState(org.eclipse.che.plugin.docker.client.json.ContainerState) Collections.singletonMap(java.util.Collections.singletonMap) ContainerInfo(org.eclipse.che.plugin.docker.client.json.ContainerInfo) Collections.emptySet(java.util.Collections.emptySet) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Machine(org.eclipse.che.api.core.model.machine.Machine) Mockito.never(org.mockito.Mockito.never) ServerException(org.eclipse.che.api.core.ServerException) DockerConnector(org.eclipse.che.plugin.docker.client.DockerConnector) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) Assert.assertEqualsNoOrder(org.testng.Assert.assertEqualsNoOrder) Volume(org.eclipse.che.plugin.docker.client.json.Volume) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 7 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project che by eclipse.

the class MachineProviderImplTest method shouldAddBindMountAndRegularVolumesOnInstanceCreationFromSnapshot.

@Test
public void shouldAddBindMountAndRegularVolumesOnInstanceCreationFromSnapshot() throws Exception {
    String[] bindMountVolumesFromMachine = new String[] { "/my/bind/mount1:/from/host1", "/my/bind/mount2:/from/host2:ro", "/my/bind/mount3:/from/host3:ro,Z" };
    String[] volumesFromMachine = new String[] { "/projects", "/something", "/something/else" };
    String[] expectedBindMountVolumes = new String[] { "/my/bind/mount1:/from/host1", "/my/bind/mount2:/from/host2:ro", "/my/bind/mount3:/from/host3:ro,Z" };
    Map<String, Volume> expectedVolumes = Stream.of("/projects", "/something", "/something/else").collect(toMap(Function.identity(), v -> new Volume()));
    provider = new MachineProviderBuilder().setDevMachineVolumes(emptySet()).setAllMachineVolumes(emptySet()).build();
    CheServiceImpl service = createService();
    service.setVolumes(Stream.concat(Stream.of(bindMountVolumesFromMachine), Stream.of(volumesFromMachine)).collect(Collectors.toList()));
    createInstanceFromSnapshot(service, true);
    ArgumentCaptor<CreateContainerParams> argumentCaptor = ArgumentCaptor.forClass(CreateContainerParams.class);
    verify(dockerConnector).createContainer(argumentCaptor.capture());
    String[] actualBindMountVolumes = argumentCaptor.getValue().getContainerConfig().getHostConfig().getBinds();
    Map<String, Volume> actualVolumes = argumentCaptor.getValue().getContainerConfig().getVolumes();
    assertEquals(actualVolumes, expectedVolumes);
    assertEqualsNoOrder(actualBindMountVolumes, expectedBindMountVolumes);
}
Also used : RemoveContainerParams(org.eclipse.che.plugin.docker.client.params.RemoveContainerParams) Arrays(java.util.Arrays) Listeners(org.testng.annotations.Listeners) InspectContainerParams(org.eclipse.che.plugin.docker.client.params.InspectContainerParams) DockerConnectorProvider(org.eclipse.che.plugin.docker.client.DockerConnectorProvider) Volume(org.eclipse.che.plugin.docker.client.json.Volume) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) DOCKER_FILE_TYPE(org.eclipse.che.plugin.docker.machine.DockerInstanceProvider.DOCKER_FILE_TYPE) Mockito.doThrow(org.mockito.Mockito.doThrow) Collections.singleton(java.util.Collections.singleton) Collectors.toMap(java.util.stream.Collectors.toMap) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Assert.assertFalse(org.testng.Assert.assertFalse) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) MachineConfig(org.eclipse.che.api.core.model.machine.MachineConfig) BeforeMethod(org.testng.annotations.BeforeMethod) RecipeRetriever(org.eclipse.che.api.machine.server.util.RecipeRetriever) Set(java.util.Set) Collectors(java.util.stream.Collectors) WindowsPathEscaper(org.eclipse.che.commons.lang.os.WindowsPathEscaper) MACHINE_SNAPSHOT_PREFIX(org.eclipse.che.plugin.docker.machine.DockerInstanceProvider.MACHINE_SNAPSHOT_PREFIX) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Stream(java.util.stream.Stream) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) TagParams(org.eclipse.che.plugin.docker.client.params.TagParams) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) UserSpecificDockerRegistryCredentialsProvider(org.eclipse.che.plugin.docker.client.UserSpecificDockerRegistryCredentialsProvider) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) DockerConnectorConfiguration(org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration) DataProvider(org.testng.annotations.DataProvider) Mock(org.mockito.Mock) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) ContainerCreated(org.eclipse.che.plugin.docker.client.json.ContainerCreated) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) StartContainerParams(org.eclipse.che.plugin.docker.client.params.StartContainerParams) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) RemoveImageParams(org.eclipse.che.plugin.docker.client.params.RemoveImageParams) HashSet(java.util.HashSet) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor) ArgumentCaptor(org.mockito.ArgumentCaptor) ContainerState(org.eclipse.che.plugin.docker.client.json.ContainerState) Collections.singletonMap(java.util.Collections.singletonMap) ContainerInfo(org.eclipse.che.plugin.docker.client.json.ContainerInfo) Collections.emptySet(java.util.Collections.emptySet) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Machine(org.eclipse.che.api.core.model.machine.Machine) Mockito.never(org.mockito.Mockito.never) ServerException(org.eclipse.che.api.core.ServerException) DockerConnector(org.eclipse.che.plugin.docker.client.DockerConnector) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) Assert.assertEqualsNoOrder(org.testng.Assert.assertEqualsNoOrder) Volume(org.eclipse.che.plugin.docker.client.json.Volume) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 8 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project mockito by mockito.

the class CaptorAnnotationProcessor method process.

public Object process(Captor annotation, Field field) {
    Class<?> type = field.getType();
    if (!ArgumentCaptor.class.isAssignableFrom(type)) {
        throw new MockitoException("@Captor field must be of the type ArgumentCaptor.\n" + "Field: '" + field.getName() + "' has wrong type\n" + "For info how to use @Captor annotations see examples in javadoc for MockitoAnnotations class.");
    }
    Class<?> cls = new GenericMaster().getGenericType(field);
    return ArgumentCaptor.forClass(cls);
}
Also used : GenericMaster(org.mockito.internal.util.reflection.GenericMaster) ArgumentCaptor(org.mockito.ArgumentCaptor) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 9 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project stashbot by palantir.

the class MockGitCommandBuilderFactory method reset.

@SuppressWarnings("unchecked")
private void reset() {
    // list of changesets in order
    changesets = new ArrayList<String>();
    blacklistedChangesets = new HashSet<String>();
    // for each hash, list of branches that contain said hash
    branchMap = new HashMap<String, List<String>>();
    gcbf = Mockito.mock(GitCommandBuilderFactory.class);
    grlb = Mockito.mock(GitRevListBuilder.class);
    gscb = Mockito.mock(GitScmCommandBuilder.class);
    branchCommandBuilder = Mockito.mock(GitScmCommandBuilder.class);
    cmd = Mockito.mock(GitCommand.class);
    branchCommand = Mockito.mock(GitCommand.class);
    // RevList cmd
    Mockito.when(gcbf.builder()).thenReturn(gscb);
    Mockito.when(gcbf.builder(Mockito.any(Repository.class))).thenReturn(gscb);
    Mockito.when(gscb.revList()).thenReturn(grlb);
    final ArgumentCaptor<CommandOutputHandler<Object>> cohCaptor = (ArgumentCaptor<CommandOutputHandler<Object>>) (Object) ArgumentCaptor.forClass(CommandOutputHandler.class);
    Mockito.when(grlb.build(cohCaptor.capture())).thenReturn(cmd);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CommandOutputHandler<Object> coh = cohCaptor.getValue();
            List<String> finalCS = new ArrayList<String>();
            for (String cs : changesets) {
                if (!blacklistedChangesets.contains(cs)) {
                    finalCS.add(cs);
                }
            }
            String str = StringUtils.join(finalCS, "\n") + "\n";
            InputStream is = new ByteArrayInputStream(str.getBytes());
            coh.process(is);
            return null;
        }
    }).when(cmd).call();
    // Branch cmd - returns list of all branches
    final ArgumentCaptor<CommandOutputHandler<Object>> branchCOHCaptor = (ArgumentCaptor<CommandOutputHandler<Object>>) (Object) ArgumentCaptor.forClass(CommandOutputHandler.class);
    Mockito.when(gscb.command("branch")).thenReturn(branchCommandBuilder);
    Mockito.when(branchCommandBuilder.build(branchCOHCaptor.capture())).thenReturn(branchCommand);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CommandOutputHandler<Object> coh = branchCOHCaptor.getValue();
            String output = "";
            List<String> branches = new ArrayList<String>(branchMap.keySet());
            for (String branch : branches) {
                output = output + "  " + branch + "\n";
            }
            InputStream is = new ByteArrayInputStream(output.getBytes());
            coh.process(is);
            return null;
        }
    }).when(branchCommand).call();
}
Also used : ArgumentCaptor(org.mockito.ArgumentCaptor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GitScmCommandBuilder(com.atlassian.stash.scm.git.GitScmCommandBuilder) GitRevListBuilder(com.atlassian.stash.scm.git.revlist.GitRevListBuilder) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler) GitCommand(com.atlassian.stash.scm.git.GitCommand) Repository(com.atlassian.stash.repository.Repository) ByteArrayInputStream(java.io.ByteArrayInputStream) GitCommandBuilderFactory(com.atlassian.stash.scm.git.GitCommandBuilderFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project Shuttle by timusus.

the class ShuttleUtilsTest method testOpenShuttleLinkMarketLink.

@Test
public void testOpenShuttleLinkMarketLink() throws Exception {
    Activity mockActivity = mock(Activity.class);
    String fakePackage = "fake.package.name";
    // Call the method and capture the "fired" intent
    ShuttleUtils.openShuttleLink(mockActivity, fakePackage);
    ArgumentCaptor<Intent> intentCaptor = new ArgumentCaptor<>();
    verify(mockActivity).startActivity(intentCaptor.capture());
    Intent intent = intentCaptor.getValue();
    assertThat(intent.getAction()).isEqualTo(Intent.ACTION_VIEW);
    // Also, I guess ShuttleUtils.isAmazonBuild() needs to be tested or we can't trust this test
    if (ShuttleUtils.isAmazonBuild()) {
        assertThat(intent.getData()).isEqualTo(Uri.parse("amzn://apps/android?p=" + fakePackage));
    } else {
        assertThat(intent.getData()).isEqualTo(Uri.parse("market://details?id=" + fakePackage));
    }
}
Also used : ArgumentCaptor(org.mockito.ArgumentCaptor) Activity(android.app.Activity) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

ArgumentCaptor (org.mockito.ArgumentCaptor)33 List (java.util.List)16 Matchers.anyString (org.mockito.Matchers.anyString)15 Test (org.testng.annotations.Test)15 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)13 HashSet (java.util.HashSet)13 Map (java.util.Map)13 Set (java.util.Set)13 Collectors (java.util.stream.Collectors)13 Test (org.junit.Test)13 Mockito.verify (org.mockito.Mockito.verify)13 Collections (java.util.Collections)12 Arrays.asList (java.util.Arrays.asList)11 IOException (java.io.IOException)10 Arrays (java.util.Arrays)10 Collections.emptySet (java.util.Collections.emptySet)10 Collections.singleton (java.util.Collections.singleton)10 Collections.singletonMap (java.util.Collections.singletonMap)10 Function (java.util.function.Function)10