Search in sources :

Example 1 with EntityManager

use of com.cloud.utils.db.EntityManager in project cloudstack by apache.

the class IntegrationTestConfiguration method entityManager.

@Bean
public EntityManager entityManager() {
    EntityManager mock = Mockito.mock(EntityManager.class);
    try {
        Mockito.when(mock.findById(Matchers.same(Account.class), Matchers.anyLong())).thenReturn(_accountDao.findById(Account.ACCOUNT_ID_SYSTEM));
        Mockito.when(mock.findById(Matchers.same(User.class), Matchers.anyLong())).thenReturn(_userDao.findById(User.UID_SYSTEM));
        Mockito.when(mock.findById(Matchers.same(NetworkOffering.class), Matchers.any(Long.class))).thenAnswer(new Answer<NetworkOffering>() {

            @Override
            public NetworkOffering answer(final InvocationOnMock invocation) throws Throwable {
                Long id = (Long) invocation.getArguments()[1];
                return _networkOfferingDao.findById(id);
            }
        });
        Mockito.when(mock.findById(Matchers.same(IpAddress.class), Matchers.any(Long.class))).thenAnswer(new Answer<IpAddress>() {

            @Override
            public IpAddress answer(final InvocationOnMock invocation) throws Throwable {
                Long id = (Long) invocation.getArguments()[1];
                return _ipAddressDao.findById(id);
            }
        });
        Mockito.when(mock.findById(Matchers.same(DataCenter.class), Matchers.any(Long.class))).thenAnswer(new Answer<DataCenter>() {

            @Override
            public DataCenter answer(final InvocationOnMock invocation) throws Throwable {
                Long id = (Long) invocation.getArguments()[1];
                return _zoneDao.findById(id);
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    CallContext.init(mock);
    return mock;
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) NetworkOffering(com.cloud.offering.NetworkOffering) IOException(java.io.IOException) EntityManager(com.cloud.utils.db.EntityManager) DataCenter(com.cloud.dc.DataCenter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IpAddress(com.cloud.network.IpAddress) Bean(org.springframework.context.annotation.Bean)

Aggregations

DataCenter (com.cloud.dc.DataCenter)1 IpAddress (com.cloud.network.IpAddress)1 NetworkOffering (com.cloud.offering.NetworkOffering)1 Account (com.cloud.user.Account)1 User (com.cloud.user.User)1 EntityManager (com.cloud.utils.db.EntityManager)1 IOException (java.io.IOException)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Bean (org.springframework.context.annotation.Bean)1