summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkjaniak <kornel.janiak@nokia.com>2018-01-31 14:35:09 +0100
committerTakamune Cho <tc012c@att.com>2018-02-02 15:08:58 +0000
commit72931ce2fe5005f7855f1e2ce35fc67d6b10f4dc (patch)
tree896bcbb70eec28b3b4c24b96aa91ad5e8832500b
parent36ca66b20d827cac5a6a2199e31369f6ad9e6a9c (diff)
Extraction of nested try catch in migrateServer
Try catch block placed in new private method conductServerMigration, UT for class added. Issue-ID: APPC-557 Change-Id: I7f968dbc87ee3be4125da6a8a3e201af99aa420e Signed-off-by: kjaniak <kornel.janiak@nokia.com>
-rw-r--r--appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/MigrateServer.java46
-rw-r--r--appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/TestMigrateServer.java37
2 files changed, 61 insertions, 22 deletions
diff --git a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/MigrateServer.java b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/MigrateServer.java
index e4ec73470..b93511ae2 100644
--- a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/MigrateServer.java
+++ b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/MigrateServer.java
@@ -166,7 +166,6 @@ public class MigrateServer extends ProviderServerOperation {
setTimeForMetricsLogger();
- String msg;
try {
validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
ProviderAdapter.PROPERTY_PROVIDER_NAME);
@@ -174,29 +173,15 @@ public class MigrateServer extends ProviderServerOperation {
String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
VMURL vm = VMURL.parseURL(vm_url);
+
if (validateVM(rc, appName, vm_url, vm))
return null;
IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
String identStr = (ident == null) ? null : ident.toString();
- Context context = getContext(rc, vm_url, identStr);
- try {
- if (context != null) {
- server = lookupServer(rc, context, vm.getServerId());
- logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
- migrateServer(rc, server, ctx);
- server.refreshStatus();
- context.close();
- doSuccess(rc);
- }
- } catch (IOException | ZoneException e1) {
- msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
- MIGRATE_SERVICE.toString(), vm_url, context.getTenantName());
- logger.error(msg, e1);
- metricsLogger.error(msg);
- doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
- }
+ server = conductServerMigration(rc, vm_url, identStr, ctx);
+
} catch (RequestFailedException e) {
doFailure(rc, e.getStatus(), e.getMessage());
}
@@ -232,4 +217,29 @@ public class MigrateServer extends ProviderServerOperation {
MDC.put("TargetServiceName", "migrate server");
MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.MigrateServer");
}
+
+ private Server conductServerMigration(RequestContext rc, String vm_url, String identStr, SvcLogicContext ctx) throws RequestFailedException {
+ String msg;
+ Context context = getContext(rc, vm_url, identStr);
+ VMURL vm = VMURL.parseURL(vm_url);
+ Server server = null;
+
+ try {
+ if (context != null) {
+ server = lookupServer(rc, context, vm.getServerId());
+ logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
+ migrateServer(rc, server, ctx);
+ server.refreshStatus();
+ context.close();
+ doSuccess(rc);
+ }
+ } catch (IOException | ZoneException e1) {
+ msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
+ MIGRATE_SERVICE.toString(), vm_url, context.getTenantName());
+ logger.error(msg, e1);
+ metricsLogger.error(msg);
+ doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
+ }
+ return server;
+ }
}
diff --git a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/TestMigrateServer.java b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/TestMigrateServer.java
index e6d9f21a6..f1682c22d 100644
--- a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/TestMigrateServer.java
+++ b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/TestMigrateServer.java
@@ -23,21 +23,27 @@
*/
package org.onap.appc.adapter.iaas.provider.operation.impl;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.atLeastOnce;
-import static org.mockito.Mockito.anyInt;
+import com.att.cdp.zones.model.ModelObject;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Matchers;
+import org.onap.appc.adapter.iaas.ProviderAdapter;
import org.onap.appc.exceptions.APPCException;
import com.att.cdp.exceptions.ZoneException;
import com.att.cdp.zones.model.Server;
import com.att.cdp.zones.model.Server.Status;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+
+import java.util.Map;
+import java.util.Optional;
+
+import static java.util.Optional.of;
+import static org.mockito.Mockito.*;
public class TestMigrateServer {
@Test
- public void migrateServerSuspended() throws ZoneException {
+ public void should_migrateSuspendedServer() throws ZoneException {
MockGenerator mg = new MockGenerator(Status.SUSPENDED);
Server server = mg.getServer();
MigrateServer rbs = new MigrateServer();
@@ -50,4 +56,27 @@ public class TestMigrateServer {
verify(mg.getComputeService()).migrateServer(MockGenerator.SERVER_ID);
verify(server, atLeastOnce()).waitForStateChange(anyInt(), anyInt(), Matchers.anyVararg());
}
+
+ @Test
+ public void should_returnNullAsServer(){
+
+ // given
+ Map<String, String> params = mock(Map.class);
+ SvcLogicContext svcLogicContext = mock(SvcLogicContext.class);
+ MockGenerator mockGenerator = new MockGenerator(Status.READY);
+ MigrateServer migrateServer = new MigrateServer();
+ migrateServer.setProviderCache(mockGenerator.getProviderCacheMap());
+ ModelObject modelObject = new Server();
+
+ // when
+ when(params.get(ProviderAdapter.PROPERTY_INSTANCE_URL)).thenReturn(null);
+ try {
+ modelObject = migrateServer.executeProviderOperation(params,svcLogicContext);
+ } catch (APPCException e) {
+ Assert.fail("Exception during MigrateServer.executeProviderOperation");
+ }
+
+ // then
+ Assert.assertNull(modelObject);
+ }
} \ No newline at end of file