Search in sources :

Example 71 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project hudson-2.x by hudson.

the class FunctionsTest method testIsAuthorTrue.

@Test
@PrepareForTest(User.class)
public void testIsAuthorTrue() throws Exception {
    mockStatic(User.class);
    User user = createMock(User.class);
    expect(user.getId()).andReturn(USER);
    expect(User.current()).andReturn(user);
    Job job = createMock(FreeStyleProject.class);
    expect(job.getCreatedBy()).andReturn(USER).times(2);
    replay(User.class, user, job);
    boolean result = Functions.isAuthor(job);
    verify(User.class, user, job);
    assertTrue(result);
}
Also used : User(hudson.model.User) Job(hudson.model.Job) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 72 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.

the class ConfigDriveBuilderTest method generateAndRetrieveIsoAsBase64IsoTestGenIsoFailure.

@SuppressWarnings("unchecked")
@Test(expected = CloudRuntimeException.class)
@PrepareForTest({ Script.class, ConfigDriveBuilder.class })
public void generateAndRetrieveIsoAsBase64IsoTestGenIsoFailure() throws Exception {
    PowerMockito.mockStatic(Script.class, ConfigDriveBuilder.class);
    Script scriptMock = Mockito.mock(Script.class);
    PowerMockito.whenNew(Script.class).withAnyArguments().thenReturn(scriptMock);
    Mockito.doReturn("scriptMessage").when(scriptMock).execute();
    Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
    PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(String.class), nullable(String.class), nullable(String.class)).thenCallRealMethod();
    Method getProgramToGenerateIsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("getProgramToGenerateIso")).iterator().next();
    PowerMockito.when(ConfigDriveBuilder.class, getProgramToGenerateIsoMethod).withNoArguments().thenReturn("/usr/bin/genisoimage");
    ConfigDriveBuilder.generateAndRetrieveIsoAsBase64Iso("isoFileName", "driveLabel", "tempDirName");
}
Also used : Script(com.cloud.utils.script.Script) Method(java.lang.reflect.Method) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 73 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.

the class ConfigDriveBuilderTest method buildConfigDriveTest.

@Test
@SuppressWarnings("unchecked")
@PrepareForTest({ ConfigDriveBuilder.class })
public void buildConfigDriveTest() throws Exception {
    PowerMockito.mockStatic(ConfigDriveBuilder.class);
    Method writeVendorAndNetworkEmptyJsonFileMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("writeVendorAndNetworkEmptyJsonFile")).iterator().next();
    PowerMockito.doNothing().when(ConfigDriveBuilder.class, writeVendorAndNetworkEmptyJsonFileMethod).withArguments(Mockito.any(File.class));
    Method writeVmMetadataMethod = getWriteVmMetadataMethod();
    PowerMockito.doNothing().when(ConfigDriveBuilder.class, writeVmMetadataMethod).withArguments(Mockito.anyListOf(String[].class), Mockito.anyString(), Mockito.any(File.class));
    Method linkUserDataMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("linkUserData")).iterator().next();
    PowerMockito.doNothing().when(ConfigDriveBuilder.class, linkUserDataMethod).withArguments(Mockito.anyString());
    Method generateAndRetrieveIsoAsBase64IsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
    PowerMockito.doReturn("mockIsoDataBase64").when(ConfigDriveBuilder.class, generateAndRetrieveIsoAsBase64IsoMethod).withArguments(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
    // force execution of real method
    PowerMockito.when(ConfigDriveBuilder.class, new ArrayList<>(), "teste", "C:").thenCallRealMethod();
    String returnedIsoData = ConfigDriveBuilder.buildConfigDrive(new ArrayList<>(), "teste", "C:");
    Assert.assertEquals("mockIsoDataBase64", returnedIsoData);
    PowerMockito.verifyStatic(ConfigDriveBuilder.class);
    ConfigDriveBuilder.writeVendorAndNetworkEmptyJsonFile(Mockito.any(File.class));
    ConfigDriveBuilder.writeVmMetadata(Mockito.anyListOf(String[].class), Mockito.anyString(), Mockito.any(File.class));
    ConfigDriveBuilder.linkUserData(Mockito.anyString());
    ConfigDriveBuilder.generateAndRetrieveIsoAsBase64Iso(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 74 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.

the class ConfigDriveBuilderTest method generateAndRetrieveIsoAsBase64IsoTest.

@Test
@SuppressWarnings("unchecked")
@PrepareForTest({ File.class, Script.class, ConfigDriveBuilder.class })
public void generateAndRetrieveIsoAsBase64IsoTest() throws Exception {
    PowerMockito.mockStatic(File.class, Script.class, ConfigDriveBuilder.class);
    File fileMock = Mockito.mock(File.class);
    PowerMockito.whenNew(File.class).withArguments("tempDirName", "isoFileName").thenReturn(fileMock);
    Script scriptMock = Mockito.mock(Script.class);
    PowerMockito.whenNew(Script.class).withAnyArguments().thenReturn(scriptMock);
    Mockito.when(fileMock.getAbsolutePath()).thenReturn("absolutePath");
    Mockito.doReturn(StringUtils.EMPTY).when(scriptMock).execute();
    Mockito.doReturn(64L * 1024L * 1024L).when(fileMock).length();
    Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
    PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(String.class), nullable(String.class), nullable(String.class)).thenCallRealMethod();
    Method getProgramToGenerateIsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("getProgramToGenerateIso")).iterator().next();
    PowerMockito.when(ConfigDriveBuilder.class, getProgramToGenerateIsoMethod).withNoArguments().thenReturn("/usr/bin/genisoimage");
    ConfigDriveBuilder.generateAndRetrieveIsoAsBase64Iso("isoFileName", "driveLabel", "tempDirName");
    InOrder inOrder = Mockito.inOrder(scriptMock);
    inOrder.verify(scriptMock).add("-o", "absolutePath");
    inOrder.verify(scriptMock).add("-ldots");
    inOrder.verify(scriptMock).add("-allow-lowercase");
    inOrder.verify(scriptMock).add("-allow-multidot");
    inOrder.verify(scriptMock).add("-cache-inodes");
    inOrder.verify(scriptMock).add("-l");
    inOrder.verify(scriptMock).add("-quiet");
    inOrder.verify(scriptMock).add("-J");
    inOrder.verify(scriptMock).add("-r");
    inOrder.verify(scriptMock).add("-V", "driveLabel");
    inOrder.verify(scriptMock).add("tempDirName");
    inOrder.verify(scriptMock).execute();
    PowerMockito.verifyStatic(ConfigDriveBuilder.class);
    ConfigDriveBuilder.fileToBase64String(nullable(File.class));
}
Also used : Script(com.cloud.utils.script.Script) InOrder(org.mockito.InOrder) Method(java.lang.reflect.Method) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 75 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.

the class ConfigDriveBuilderTest method generateAndRetrieveIsoAsBase64IsoTestIsoTooBig.

@SuppressWarnings("unchecked")
@Test(expected = CloudRuntimeException.class)
@PrepareForTest({ File.class, Script.class, ConfigDriveBuilder.class })
public void generateAndRetrieveIsoAsBase64IsoTestIsoTooBig() throws Exception {
    PowerMockito.mockStatic(File.class, Script.class, ConfigDriveBuilder.class);
    File fileMock = Mockito.mock(File.class);
    PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(fileMock);
    Script scriptMock = Mockito.mock(Script.class);
    PowerMockito.whenNew(Script.class).withAnyArguments().thenReturn(scriptMock);
    Mockito.doReturn(StringUtils.EMPTY).when(scriptMock).execute();
    Mockito.doReturn(64L * 1024L * 1024L + 1l).when(fileMock).length();
    Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
    PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(String.class), nullable(String.class), nullable(String.class)).thenCallRealMethod();
    Method getProgramToGenerateIsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("getProgramToGenerateIso")).iterator().next();
    PowerMockito.when(ConfigDriveBuilder.class, getProgramToGenerateIsoMethod).withNoArguments().thenReturn("/usr/bin/genisoimage");
    ConfigDriveBuilder.generateAndRetrieveIsoAsBase64Iso("isoFileName", "driveLabel", "tempDirName");
}
Also used : Script(com.cloud.utils.script.Script) Method(java.lang.reflect.Method) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)196 Test (org.junit.Test)194 HttpServletRequest (javax.servlet.http.HttpServletRequest)30 HttpServletResponse (javax.servlet.http.HttpServletResponse)30 StringWriter (java.io.StringWriter)28 PrintWriter (java.io.PrintWriter)27 File (java.io.File)24 ArrayList (java.util.ArrayList)16 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)14 Method (java.lang.reflect.Method)13 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)12 Config (com.twitter.heron.spi.common.Config)12 Matchers.anyString (org.mockito.Matchers.anyString)12 DialogInterface (android.content.DialogInterface)11 Intent (android.content.Intent)11 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)11 Job (hudson.model.Job)11 IOException (java.io.IOException)11 Date (java.util.Date)10 HashMap (java.util.HashMap)10