Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ public class CreateAccountCmdTest {
private Integer accountType = 1;
private Long domainId = 1L;

private AutoCloseable closeable;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
closeable = MockitoAnnotations.openMocks(this);
ReflectionTestUtils.setField(createAccountCmd, "domainId", domainId);
ReflectionTestUtils.setField(createAccountCmd, "accountType", accountType);
CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class));
}

@After
public void tearDown() throws Exception {
closeable.close();
CallContext.unregister();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.cloudstack.api.command.admin.annotation;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
Expand All @@ -26,9 +27,16 @@ public class AddAnnotationCmdTest {

private AddAnnotationCmd addAnnotationCmd = new AddAnnotationCmd();

private AutoCloseable closeable;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
closeable = MockitoAnnotations.openMocks(this);
}

@After
public void tearDown() throws Exception {
closeable.close();
}

@Test (expected = IllegalStateException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@
import com.cloud.user.Account;
import com.cloud.utils.Pair;
import org.apache.cloudstack.context.CallContext;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

@RunWith(PowerMockRunner.class)
@PrepareForTest(CallContext.class)
@PowerMockIgnore({"javax.xml.*", "org.w3c.dom.*", "org.apache.xerces.*", "org.xml.*"})
@RunWith(MockitoJUnitRunner.class)
public class PatchSystemVMCmdTest {

@Mock
Expand All @@ -45,9 +42,16 @@ public class PatchSystemVMCmdTest {
@InjectMocks
PatchSystemVMCmd cmd = new PatchSystemVMCmd();

private AutoCloseable closeable;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
closeable = MockitoAnnotations.openMocks(this);
}

@After
public void tearDown() throws Exception {
closeable.close();
}

@Test
Expand Down Expand Up @@ -76,17 +80,16 @@ public void patchInvalidSystemVM() {

@Test
public void validateArgsForPatchSystemVMApi() {
PowerMockito.mockStatic(CallContext.class);
CallContext callContextMock = PowerMockito.mock(CallContext.class);
PowerMockito.when(CallContext.current()).thenReturn(callContextMock);
Account accountMock = PowerMockito.mock(Account.class);
PowerMockito.when(callContextMock.getCallingAccount()).thenReturn(accountMock);
Mockito.when(accountMock.getId()).thenReturn(2L);
ReflectionTestUtils.setField(cmd, "id", 1L);
Assert.assertEquals((long)cmd.getId(), 1L);
Assert.assertFalse(cmd.isForced());
Assert.assertEquals(cmd.getEntityOwnerId(), 2L);


try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
CallContext callContextMock = Mockito.mock(CallContext.class);
callContextMocked.when(CallContext::current).thenReturn(callContextMock);
Account accountMock = Mockito.mock(Account.class);
Mockito.when(callContextMock.getCallingAccount()).thenReturn(accountMock);
Mockito.when(accountMock.getId()).thenReturn(2L);
ReflectionTestUtils.setField(cmd, "id", 1L);
Assert.assertEquals((long) cmd.getId(), 1L);
Assert.assertFalse(cmd.isForced());
Assert.assertEquals(cmd.getEntityOwnerId(), 2L);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ public class CreateUserCmdTest {
@InjectMocks
private CreateUserCmd createUserCmd = new CreateUserCmd();

private AutoCloseable closeable;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
closeable = MockitoAnnotations.openMocks(this);
CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class));
}

@After
public void tearDown() throws Exception {
closeable.close();
CallContext.unregister();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@
import org.apache.cloudstack.api.command.user.vm.ResetVMUserDataCmd;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.context.CallContext;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.ArrayList;
Expand All @@ -45,9 +44,7 @@

import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(CallContext.class)
@PowerMockIgnore({"javax.xml.*", "org.w3c.dom.*", "org.apache.xerces.*", "org.xml.*"})
@RunWith(MockitoJUnitRunner.class)
public class ResetVMUserDataCmdTest {

@InjectMocks
Expand All @@ -66,14 +63,22 @@ public class ResetVMUserDataCmdTest {
private static final long PROJECT_ID = 10L;
private static final String ACCOUNT_NAME = "user";

private AutoCloseable closeable;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
closeable = MockitoAnnotations.openMocks(this);
ReflectionTestUtils.setField(cmd, "accountName", ACCOUNT_NAME);
ReflectionTestUtils.setField(cmd, "domainId", DOMAIN_ID);
ReflectionTestUtils.setField(cmd, "projectId", PROJECT_ID);
}

@After
public void tearDown() throws Exception {
closeable.close();
}


@Test
public void testValidResetVMUserDataExecute() {
UserVm result = Mockito.mock(UserVm.class);
Expand All @@ -95,22 +100,23 @@ public void testValidResetVMUserDataExecute() {

@Test
public void validateArgsCmd() {
PowerMockito.mockStatic(CallContext.class);
CallContext callContextMock = PowerMockito.mock(CallContext.class);
PowerMockito.when(CallContext.current()).thenReturn(callContextMock);

ReflectionTestUtils.setField(cmd, "id", 1L);
ReflectionTestUtils.setField(cmd, "userdataId", 2L);
ReflectionTestUtils.setField(cmd, "userData", "testUserdata");

UserVm vm = Mockito.mock(UserVm.class);
when(_responseGenerator.findUserVmById(1L)).thenReturn(vm);
when(vm.getAccountId()).thenReturn(200L);

Assert.assertEquals(1L, (long)cmd.getId());
Assert.assertEquals(2L, (long)cmd.getUserdataId());
Assert.assertEquals("testUserdata", cmd.getUserData());
Assert.assertEquals(200L, cmd.getEntityOwnerId());
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
CallContext callContextMock = Mockito.mock(CallContext.class);
callContextMocked.when(CallContext::current).thenReturn(callContextMock);

ReflectionTestUtils.setField(cmd, "id", 1L);
ReflectionTestUtils.setField(cmd, "userdataId", 2L);
ReflectionTestUtils.setField(cmd, "userData", "testUserdata");

UserVm vm = Mockito.mock(UserVm.class);
when(_responseGenerator.findUserVmById(1L)).thenReturn(vm);
when(vm.getAccountId()).thenReturn(200L);

Assert.assertEquals(1L, (long) cmd.getId());
Assert.assertEquals(2L, (long) cmd.getUserdataId());
Assert.assertEquals("testUserdata", cmd.getUserData());
Assert.assertEquals(200L, cmd.getEntityOwnerId());
}
}

@Test
Expand Down
Loading