diff --git a/api/src/org/apache/cloudstack/api/command/admin/host/AddSecondaryStorageCmd.java b/api/src/org/apache/cloudstack/api/command/admin/host/AddSecondaryStorageCmd.java index 3f243703825..d663575895d 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/host/AddSecondaryStorageCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/host/AddSecondaryStorageCmd.java @@ -16,25 +16,22 @@ // under the License. package org.apache.cloudstack.api.command.admin.host; -import java.util.List; -import java.util.Map; - import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.Parameter; import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.response.HostResponse; -import org.apache.cloudstack.api.response.RegionResponse; +import org.apache.cloudstack.api.command.admin.storage.AddImageStoreCmd; +import org.apache.cloudstack.api.response.ImageStoreResponse; import org.apache.cloudstack.api.response.ZoneResponse; import org.apache.log4j.Logger; import com.cloud.exception.DiscoveryException; -import com.cloud.host.Host; +import com.cloud.storage.ImageStore; import com.cloud.user.Account; -@APICommand(name = "addSecondaryStorage", description="Adds secondary storage.", responseObject=HostResponse.class) +@APICommand(name = "addSecondaryStorage", description="Adds secondary storage.", responseObject=ImageStoreResponse.class) public class AddSecondaryStorageCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(AddSecondaryStorageCmd.class.getName()); private static final String s_name = "addsecondarystorageresponse"; @@ -51,6 +48,7 @@ public class AddSecondaryStorageCmd extends BaseCmd { private Long zoneId; + ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// ///////////////////////////////////////////////////// @@ -80,17 +78,19 @@ public class AddSecondaryStorageCmd extends BaseCmd { @Override public void execute(){ - try { - List result = _resourceService.discoverHosts(this); - HostResponse hostResponse = null; - if (result != null && result.size() > 0) { - for (Host host : result) { - // There should only be one secondary storage host per add - hostResponse = _responseGenerator.createHostResponse(host); - hostResponse.setResponseName(getCommandName()); - hostResponse.setObjectName("secondarystorage"); - this.setResponseObject(hostResponse); - } + AddImageStoreCmd cmd = new AddImageStoreCmd(); + cmd.setUrl(this.getUrl()); + cmd.setZoneId(this.getZoneId()); + cmd.setProviderName("CloudStack ImageStore Provider"); + + try{ + ImageStore result = _storageService.discoverImageStore(cmd); + ImageStoreResponse storeResponse = null; + if (result != null ) { + storeResponse = _responseGenerator.createImageStoreResponse(result); + storeResponse.setResponseName(getCommandName()); + storeResponse.setObjectName("secondarystorage"); + this.setResponseObject(storeResponse); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add secondary storage"); } @@ -98,6 +98,5 @@ public class AddSecondaryStorageCmd extends BaseCmd { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); } - } } diff --git a/api/src/org/apache/cloudstack/api/command/admin/storage/AddImageStoreCmd.java b/api/src/org/apache/cloudstack/api/command/admin/storage/AddImageStoreCmd.java index 7221f4ee90f..190b36a9234 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/storage/AddImageStoreCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/storage/AddImageStoreCmd.java @@ -78,11 +78,28 @@ public class AddImageStoreCmd extends BaseCmd { return this.providerName; } + public void setUrl(String url) { + this.url = url; + } + + public void setZoneId(Long zoneId) { + this.zoneId = zoneId; + } + + public void setProviderName(String providerName) { + this.providerName = providerName; + } + + public void setDetails(Map details) { + this.details = details; + } ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// + + @Override public String getCommandName() { return s_name; diff --git a/api/src/org/apache/cloudstack/api/command/admin/storage/AddS3Cmd.java b/api/src/org/apache/cloudstack/api/command/admin/storage/AddS3Cmd.java index dbd9bff632b..3ad84fd5a51 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/storage/AddS3Cmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/storage/AddS3Cmd.java @@ -31,12 +31,17 @@ import static org.apache.cloudstack.api.BaseCmd.CommandType.BOOLEAN; import static org.apache.cloudstack.api.BaseCmd.CommandType.INTEGER; import static org.apache.cloudstack.api.BaseCmd.CommandType.STRING; +import java.util.HashMap; +import java.util.Map; + import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.Parameter; import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.response.S3Response; +import org.apache.cloudstack.api.response.ImageStoreResponse; +import org.apache.log4j.Logger; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.DiscoveryException; @@ -44,10 +49,11 @@ import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.NetworkRuleConflictException; import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.ResourceUnavailableException; -import com.cloud.storage.S3; +import com.cloud.storage.ImageStore; -@APICommand(name = "addS3", description = "Adds S3", responseObject = S3Response.class, since = "4.0.0") +@APICommand(name = "addS3", description = "Adds S3", responseObject = ImageStoreResponse.class, since = "4.0.0") public final class AddS3Cmd extends BaseCmd { + public static final Logger s_logger = Logger.getLogger(AddS3Cmd.class.getName()); private static String COMMAND_NAME = "adds3response"; @@ -88,26 +94,33 @@ public final class AddS3Cmd extends BaseCmd { ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException { - final S3 result; + AddImageStoreCmd cmd = new AddImageStoreCmd(); + cmd.setProviderName("S3"); + Map details = new HashMap(); + details.put(ApiConstants.S3_ACCESS_KEY, this.getAccessKey()); + details.put(ApiConstants.S3_SECRET_KEY, this.getSecretKey()); + details.put(ApiConstants.S3_END_POINT, this.getEndPoint()); + details.put(ApiConstants.S3_BUCKET_NAME, this.getBucketName()); + details.put(ApiConstants.S3_HTTPS_FLAG, this.getHttpsFlag().toString()); + details.put(ApiConstants.S3_CONNECTION_TIMEOUT, this.getConnectionTimeout().toString()); + details.put(ApiConstants.S3_MAX_ERROR_RETRY, this.getMaxErrorRetry().toString()); + details.put(ApiConstants.S3_SOCKET_TIMEOUT, this.getSocketTimeout().toString()); - try { - - result = _resourceService.discoverS3(this); - - if (result == null) { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add S3."); + try{ + ImageStore result = _storageService.discoverImageStore(cmd); + ImageStoreResponse storeResponse = null; + if (result != null ) { + storeResponse = _responseGenerator.createImageStoreResponse(result); + storeResponse.setResponseName(getCommandName()); + storeResponse.setObjectName("secondarystorage"); + this.setResponseObject(storeResponse); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add secondary storage"); } - - } catch (DiscoveryException e) { - - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add S3 due to " + e.getMessage()); - + } catch (DiscoveryException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); } - - final S3Response response = _responseGenerator.createS3Response(result); - response.setResponseName(this.getCommandName()); - this.setResponseObject(response); - } @Override diff --git a/api/src/org/apache/cloudstack/api/command/admin/storage/ListImageStoresCmd.java b/api/src/org/apache/cloudstack/api/command/admin/storage/ListImageStoresCmd.java index 499cc041620..fb589af8e14 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/storage/ListImageStoresCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/storage/ListImageStoresCmd.java @@ -83,12 +83,17 @@ public class ListImageStoresCmd extends BaseListCmd { return provider; } + public void setProvider(String provider) { + this.provider = provider; + } ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// + + @Override public String getCommandName() { return s_name; diff --git a/api/src/org/apache/cloudstack/api/command/admin/storage/ListS3sCmd.java b/api/src/org/apache/cloudstack/api/command/admin/storage/ListS3sCmd.java index 4ab71de24d0..409f1607ce7 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/storage/ListS3sCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/storage/ListS3sCmd.java @@ -18,23 +18,18 @@ */ package org.apache.cloudstack.api.command.admin.storage; -import java.util.ArrayList; -import java.util.List; - import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.BaseListCmd; import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.response.ImageStoreResponse; import org.apache.cloudstack.api.response.ListResponse; -import org.apache.cloudstack.api.response.S3Response; - import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.NetworkRuleConflictException; import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.ResourceUnavailableException; -import com.cloud.storage.S3; -@APICommand(name = "listS3s", description = "Lists S3s", responseObject = S3Response.class, since = "4.0.0") +@APICommand(name = "listS3s", description = "Lists S3s", responseObject = ImageStoreResponse.class, since = "4.0.0") public class ListS3sCmd extends BaseListCmd { private static final String COMMAND_NAME = "lists3sresponse"; @@ -44,28 +39,11 @@ public class ListS3sCmd extends BaseListCmd { ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException { - final List result = _resourceService.listS3s(this); - final ListResponse response = new ListResponse(); - final List s3Responses = new ArrayList(); - - if (result != null) { - - for (S3 s3 : result) { - - S3Response s3Response = _responseGenerator.createS3Response(s3); - s3Response.setResponseName(this.getCommandName()); - s3Response.setObjectName("s3"); - s3Responses.add(s3Response); - - } - - } - - response.setResponses(s3Responses); - response.setResponseName(this.getCommandName()); - + ListImageStoresCmd cmd = new ListImageStoresCmd(); + cmd.setProvider("S3"); + ListResponse response = _queryService.searchForImageStores(cmd); + response.setResponseName(getCommandName()); this.setResponseObject(response); - } @Override diff --git a/api/src/org/apache/cloudstack/api/command/admin/swift/AddSwiftCmd.java b/api/src/org/apache/cloudstack/api/command/admin/swift/AddSwiftCmd.java index 364d916add6..462b5297ce5 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/swift/AddSwiftCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/swift/AddSwiftCmd.java @@ -16,21 +16,24 @@ // under the License. package org.apache.cloudstack.api.command.admin.swift; +import java.util.HashMap; +import java.util.Map; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.Parameter; import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.response.HostResponse; -import org.apache.cloudstack.api.response.SwiftResponse; +import org.apache.cloudstack.api.command.admin.storage.AddImageStoreCmd; +import org.apache.cloudstack.api.response.ImageStoreResponse; import org.apache.log4j.Logger; import com.cloud.exception.DiscoveryException; -import com.cloud.storage.Swift; +import com.cloud.storage.ImageStore; import com.cloud.user.Account; -@APICommand(name = "addSwift", description = "Adds Swift.", responseObject = HostResponse.class, since="3.0.0") +@APICommand(name = "addSwift", description = "Adds Swift.", responseObject = ImageStoreResponse.class, since="3.0.0") public class AddSwiftCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(AddSwiftCmd.class.getName()); private static final String s_name = "addswiftresponse"; @@ -87,21 +90,29 @@ public class AddSwiftCmd extends BaseCmd { @Override public void execute(){ - try { - Swift result = _resourceService.discoverSwift(this); - SwiftResponse swiftResponse = null; - if (result != null) { - swiftResponse = _responseGenerator.createSwiftResponse(result); - swiftResponse.setResponseName(getCommandName()); - swiftResponse.setObjectName("swift"); - this.setResponseObject(swiftResponse); + AddImageStoreCmd cmd = new AddImageStoreCmd(); + cmd.setProviderName("Swift"); + cmd.setUrl(this.getUrl()); + Map details = new HashMap(); + details.put(ApiConstants.ACCOUNT, this.getAccount()); + details.put(ApiConstants.USERNAME, this.getUsername()); + details.put(ApiConstants.KEY, this.getKey()); + + + try{ + ImageStore result = _storageService.discoverImageStore(cmd); + ImageStoreResponse storeResponse = null; + if (result != null ) { + storeResponse = _responseGenerator.createImageStoreResponse(result); + storeResponse.setResponseName(getCommandName()); + storeResponse.setObjectName("secondarystorage"); + this.setResponseObject(storeResponse); } else { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add Swift"); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add secondary storage"); } } catch (DiscoveryException ex) { - String errMsg = "Failed to add Swift due to " + ex.toString(); - s_logger.warn(errMsg, ex); - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, errMsg); + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); } } } diff --git a/api/src/org/apache/cloudstack/api/command/admin/swift/ListSwiftsCmd.java b/api/src/org/apache/cloudstack/api/command/admin/swift/ListSwiftsCmd.java index 7cfe6e1ab7f..b0408f43792 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/swift/ListSwiftsCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/swift/ListSwiftsCmd.java @@ -16,23 +16,18 @@ // under the License. package org.apache.cloudstack.api.command.admin.swift; -import java.util.ArrayList; -import java.util.List; - import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.BaseListCmd; import org.apache.cloudstack.api.Parameter; -import org.apache.cloudstack.api.response.HostResponse; +import org.apache.cloudstack.api.command.admin.storage.ListImageStoresCmd; +import org.apache.cloudstack.api.response.ImageStoreResponse; import org.apache.cloudstack.api.response.ListResponse; -import org.apache.cloudstack.api.response.SwiftResponse; import org.apache.log4j.Logger; -import com.cloud.storage.Swift; import com.cloud.user.Account; -import com.cloud.utils.Pair; -@APICommand(name = "listSwifts", description = "List Swift.", responseObject = HostResponse.class, since="3.0.0") +@APICommand(name = "listSwifts", description = "List Swift.", responseObject = ImageStoreResponse.class, since="3.0.0") public class ListSwiftsCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger(ListSwiftsCmd.class.getName()); private static final String s_name = "listswiftsresponse"; @@ -65,19 +60,10 @@ public class ListSwiftsCmd extends BaseListCmd { @Override public void execute(){ - Pair, Integer> result = _resourceService.listSwifts(this); - ListResponse response = new ListResponse(); - List swiftResponses = new ArrayList(); - if (result != null) { - for (Swift swift : result.first()) { - SwiftResponse swiftResponse = _responseGenerator.createSwiftResponse(swift); - swiftResponse.setResponseName(getCommandName()); - swiftResponse.setObjectName("swift"); - swiftResponses.add(swiftResponse); - } - } - response.setResponses(swiftResponses, result.second()); + ListImageStoresCmd cmd = new ListImageStoresCmd(); + cmd.setProvider("Swift"); + ListResponse response = _queryService.searchForImageStores(cmd); response.setResponseName(getCommandName()); this.setResponseObject(response); } diff --git a/api/test/org/apache/cloudstack/api/command/test/AddSecondaryStorageCmdTest.java b/api/test/org/apache/cloudstack/api/command/test/AddSecondaryStorageCmdTest.java index d6de94dd033..c221ace62cc 100644 --- a/api/test/org/apache/cloudstack/api/command/test/AddSecondaryStorageCmdTest.java +++ b/api/test/org/apache/cloudstack/api/command/test/AddSecondaryStorageCmdTest.java @@ -21,29 +21,29 @@ import junit.framework.TestCase; import org.apache.cloudstack.api.ResponseGenerator; import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.command.admin.host.AddSecondaryStorageCmd; +import org.apache.cloudstack.api.command.admin.storage.AddImageStoreCmd; import org.apache.cloudstack.api.response.HostResponse; +import org.apache.cloudstack.api.response.ImageStoreResponse; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mockito; -import com.cloud.host.Host; -import com.cloud.resource.ResourceService; - -import edu.emory.mathcs.backport.java.util.Arrays; +import com.cloud.storage.ImageStore; +import com.cloud.storage.StorageService; public class AddSecondaryStorageCmdTest extends TestCase { - private AddSecondaryStorageCmd addSecondaryStorageCmd; + private AddImageStoreCmd addImageStoreCmd; @Rule public ExpectedException expectedException = ExpectedException.none(); + @Override @Before public void setUp() { - addSecondaryStorageCmd = new AddSecondaryStorageCmd() { + addImageStoreCmd = new AddImageStoreCmd() { }; } @@ -51,69 +51,48 @@ public class AddSecondaryStorageCmdTest extends TestCase { @Test public void testExecuteForResult() throws Exception { - ResourceService resourceService = Mockito.mock(ResourceService.class); - addSecondaryStorageCmd._resourceService = resourceService; + StorageService resourceService = Mockito.mock(StorageService.class); + addImageStoreCmd._storageService = resourceService; - Host host = Mockito.mock(Host.class); - Host[] mockHosts = new Host[] { host }; + ImageStore store = Mockito.mock(ImageStore.class); - Mockito.when(resourceService.discoverHosts(addSecondaryStorageCmd)) - .thenReturn(Arrays.asList(mockHosts)); + Mockito.when(resourceService.discoverImageStore(addImageStoreCmd)) + .thenReturn(store); ResponseGenerator responseGenerator = Mockito .mock(ResponseGenerator.class); - addSecondaryStorageCmd._responseGenerator = responseGenerator; + addImageStoreCmd._responseGenerator = responseGenerator; - HostResponse responseHost = new HostResponse(); + ImageStoreResponse responseHost = new ImageStoreResponse(); responseHost.setName("Test"); - Mockito.when(responseGenerator.createHostResponse(host)).thenReturn( + Mockito.when(responseGenerator.createImageStoreResponse(store)).thenReturn( responseHost); - addSecondaryStorageCmd.execute(); + addImageStoreCmd.execute(); - Mockito.verify(responseGenerator).createHostResponse(host); + Mockito.verify(responseGenerator).createImageStoreResponse(store); - HostResponse actualResponse = (HostResponse) addSecondaryStorageCmd + ImageStoreResponse actualResponse = (ImageStoreResponse) addImageStoreCmd .getResponseObject(); Assert.assertEquals(responseHost, actualResponse); - Assert.assertEquals("addsecondarystorageresponse", + Assert.assertEquals("addimagestoreresponse", actualResponse.getResponseName()); } - @Test - public void testExecuteForEmptyResult() throws Exception { - - ResourceService resourceService = Mockito.mock(ResourceService.class); - addSecondaryStorageCmd._resourceService = resourceService; - - Host[] mockHosts = new Host[] {}; - - Mockito.when(resourceService.discoverHosts(addSecondaryStorageCmd)) - .thenReturn(Arrays.asList(mockHosts)); - - try { - addSecondaryStorageCmd.execute(); - } catch (ServerApiException exception) { - Assert.assertEquals("Failed to add secondary storage", - exception.getDescription()); - } - - } - @Test public void testExecuteForNullResult() throws Exception { - ResourceService resourceService = Mockito.mock(ResourceService.class); - addSecondaryStorageCmd._resourceService = resourceService; + StorageService resourceService = Mockito.mock(StorageService.class); + addImageStoreCmd._storageService = resourceService; - Mockito.when(resourceService.discoverHosts(addSecondaryStorageCmd)) + Mockito.when(resourceService.discoverImageStore(addImageStoreCmd)) .thenReturn(null); try { - addSecondaryStorageCmd.execute(); + addImageStoreCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to add secondary storage", exception.getDescription()); diff --git a/api/test/org/apache/cloudstack/api/command/test/AddSwiftCmdTest.java b/api/test/org/apache/cloudstack/api/command/test/AddSwiftCmdTest.java deleted file mode 100644 index 141a2368d78..00000000000 --- a/api/test/org/apache/cloudstack/api/command/test/AddSwiftCmdTest.java +++ /dev/null @@ -1,97 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -package org.apache.cloudstack.api.command.test; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import org.apache.cloudstack.api.ResponseGenerator; -import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.command.admin.swift.AddSwiftCmd; -import org.apache.cloudstack.api.response.SwiftResponse; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.mockito.Mockito; - -import com.cloud.exception.DiscoveryException; -import com.cloud.resource.ResourceService; -import com.cloud.storage.Swift; - -public class AddSwiftCmdTest extends TestCase { - - private AddSwiftCmd addSwiftCmd; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Before - public void setUp() { - addSwiftCmd = new AddSwiftCmd(); - } - - @Test - public void testExecuteSuccess() { - - ResourceService resourceService = Mockito.mock(ResourceService.class); - addSwiftCmd._resourceService = resourceService; - - Swift swift = Mockito.mock(Swift.class); - - try { - Mockito.when(resourceService.discoverSwift(addSwiftCmd)) - .thenReturn(swift); - } catch (DiscoveryException e) { - e.printStackTrace(); - } - - ResponseGenerator responseGenerator = Mockito - .mock(ResponseGenerator.class); - addSwiftCmd._responseGenerator = responseGenerator; - - SwiftResponse swiftResponse = Mockito.mock(SwiftResponse.class); - - Mockito.when(responseGenerator.createSwiftResponse(swift)).thenReturn( - swiftResponse); - - addSwiftCmd.execute(); - - } - - @Test - public void testExecuteFailure() { - - ResourceService resourceService = Mockito.mock(ResourceService.class); - addSwiftCmd._resourceService = resourceService; - try { - Mockito.when(resourceService.discoverSwift(addSwiftCmd)) - .thenReturn(null); - } catch (DiscoveryException e) { - e.printStackTrace(); - } - - try { - addSwiftCmd.execute(); - } catch (ServerApiException exception) { - Assert.assertEquals("Failed to add Swift", - exception.getDescription()); - } - - } - -}