Skip to content

Instantly share code, notes, and snippets.

@enricruiz
Created May 31, 2012 07:51
Show Gist options
  • Save enricruiz/2841734 to your computer and use it in GitHub Desktop.
Save enricruiz/2841734 to your computer and use it in GitHub Desktop.
/**
* Abiquo community edition
* cloud management application for hybrid clouds
* Copyright (C) 2008-2010 - Abiquo Holdings S.L.
*
* This application is free software; you can redistribute it and/or
* modify it under the terms of the GNU LESSER GENERAL PUBLIC
* LICENSE as published by the Free Software Foundation under
* version 3 of the License
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* LESSER GENERAL PUBLIC LICENSE v.3 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.abiquo.api.services.stub;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.abiquo.api.exceptions.APIError;
import com.abiquo.api.pools.RemoteServiceClientPool;
import com.abiquo.server.core.infrastructure.RemoteService;
import com.abiquo.ssm.client.SSMClient;
import com.abiquo.ssm.client.SSMClientException;
import com.abiquo.ssm.model.Device;
import com.abiquo.ssm.model.Volume;
import com.abiquo.vsm.client.MonitorManagerClient;
import com.abiquo.vsm.client.MonitorManagerClientException;
import com.abiquo.vsm.model.transport.MonitorDto;
import com.abiquo.vsm.model.transport.SubscriptionDto;
/**
* Mock class for the {@link RemoteServiceClientPool}
*
* @author Ignasi Barrera
*/
@Service
public class RemoteServiceClientPoolMock extends RemoteServiceClientPool
{
/** The logger. */
private final static Logger LOGGER = LoggerFactory.getLogger(RemoteServiceClientPoolMock.class);
public static final String IQN = "iqn.2001-04.com.acme";
public static final String LUN = "0";
public static final String IQN_AND_LUN = IQN + "-lun-" + LUN;
@Override
public Object getClientFor(final RemoteService remoteService)
{
Object client = null;
switch (remoteService.getType())
{
case STORAGE_SYSTEM_MONITOR:
client = mockSSM();
break;
case VIRTUAL_SYSTEM_MONITOR:
client = mockMonitorManagerClient();
break;
default:
LOGGER.error(APIError.REMOTE_SERVICE_NON_POOLABLE.getMessage());
addUnexpectedErrors(APIError.REMOTE_SERVICE_NON_POOLABLE);
flushErrors();
break;
}
return client;
}
@Override
public void releaseClientFor(final RemoteService remoteService, final Object client)
{
// Do nothing
}
private SSMClient mockSSM()
{
SSMClient mock = mock(SSMClient.class);
try
{
Volume mockVolume = mockVolume();
when(
mock.createVolume(any(Device.class), any(com.abiquo.ssm.model.Pool.class),
any(Volume.class))).thenReturn(mockVolume);
when(
mock.updateVolume(any(Device.class), any(com.abiquo.ssm.model.Pool.class),
any(Volume.class))).thenReturn(mockVolume);
when(
mock.addInitiatorToPool(any(Device.class), any(com.abiquo.ssm.model.Pool.class),
anyString(), anyString(), anyString())).thenReturn(IQN_AND_LUN);
}
catch (SSMClientException e)
{
// Do nothing
}
return mock;
}
private MonitorManagerClient mockMonitorManagerClient()
{
MonitorManagerClient mock = mock(MonitorManagerClient.class);
try
{
MonitorDto monitorMock = new MonitorDto();
monitorMock.setAddress(UUID.randomUUID().toString());
monitorMock.setId(0);
monitorMock.setUser(UUID.randomUUID().toString());
monitorMock.setPassword(UUID.randomUUID().toString());
monitorMock.setType("KVM");
monitorMock.setState(StringUtils.EMPTY);
SubscriptionDto subscriptionMock = new SubscriptionDto();
subscriptionMock.setId(1);
subscriptionMock.setMonitor(monitorMock);
subscriptionMock.setNotified("UNKNOWN");
subscriptionMock.setUuid(UUID.randomUUID().toString());
List<MonitorDto> monitorsMock = new ArrayList<MonitorDto>();
List<SubscriptionDto> subscriptionsMock = new ArrayList<SubscriptionDto>();
when(mock.getSubscription(anyInt())).thenReturn(subscriptionMock);
when(mock.findSubscriptionByUuid(anyString())).thenReturn(subscriptionMock);
when(mock.createSubscription(any(SubscriptionDto.class))).thenReturn(subscriptionMock);
when(mock.findAllSubscriptions()).thenReturn(subscriptionsMock);
when(mock.getMonitor(anyInt())).thenReturn(monitorMock);
when(mock.findMonitorByUrl(anyString())).thenReturn(monitorMock);
when(mock.createMonitor(any(MonitorDto.class))).thenReturn(monitorMock);
when(mock.findAllMonitors()).thenReturn(monitorsMock);
}
catch (MonitorManagerClientException e)
{
// Ignore
}
return mock;
}
private static Volume mockVolume()
{
Volume Volume = new Volume();
Volume.setUuid(UUID.randomUUID().toString());
Volume.setSizeInMB(1024L);
Volume.setUsedInMB(0L);
Volume.setAvailableInMB(1024L);
Volume.setIqn("iqn.2001-04.com.acme");
Volume.setLun(0);
return Volume;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment