diff options
Diffstat (limited to 'adapters/mso-adapter-utils/src')
22 files changed, 405 insertions, 265 deletions
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudConfigFactory.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudConfigFactory.java index 79fe8fda8e..445c135dab 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudConfigFactory.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudConfigFactory.java @@ -185,12 +185,12 @@ public class CloudConfigFactory implements Serializable { StringBuffer response = new StringBuffer (); response.append ("Cloud Sites:\n"); for (CloudSite site : cloudConfig.getCloudSites ().values ()) { - response.append (site.toString () + "\n"); + response.append(site.toString()).append("\n"); } response.append ("\n\nCloud Identity Services:\n"); for (CloudIdentity identity : cloudConfig.getIdentityServices ().values ()) { - response.append (identity.toString () + "\n"); + response.append(identity.toString()).append("\n"); } return Response.status (200).entity (response).build (); diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudIdentity.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudIdentity.java index d0ba7e09fc..3dd4cbfc9c 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudIdentity.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudIdentity.java @@ -23,28 +23,23 @@ package org.openecomp.mso.cloud; -import java.io.IOException; -import java.net.URISyntaxException; +import com.woorea.openstack.keystone.model.Authentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; import java.security.GeneralSecurityException; - import org.codehaus.jackson.annotate.JsonProperty; import org.codehaus.jackson.map.annotate.JsonDeserialize; import org.codehaus.jackson.map.annotate.JsonSerialize; -import org.openecomp.mso.openstack.exceptions.MsoAdapterException; -import org.openecomp.mso.openstack.exceptions.MsoException; -import org.openecomp.mso.openstack.utils.MsoKeystoneUtils; -import org.openecomp.mso.openstack.utils.MsoTenantUtils; -import org.openecomp.mso.openstack.utils.MsoTenantUtilsFactory; import org.openecomp.mso.cloud.authentication.AuthenticationMethodFactory; import org.openecomp.mso.cloud.authentication.AuthenticationWrapper; import org.openecomp.mso.cloud.authentication.wrappers.RackspaceAPIKeyWrapper; import org.openecomp.mso.cloud.authentication.wrappers.UsernamePasswordWrapper; import org.openecomp.mso.logger.MessageEnum; import org.openecomp.mso.logger.MsoLogger; - -import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import org.openecomp.mso.openstack.exceptions.MsoException; +import org.openecomp.mso.openstack.utils.MsoKeystoneUtils; +import org.openecomp.mso.openstack.utils.MsoTenantUtils; +import org.openecomp.mso.openstack.utils.MsoTenantUtilsFactory; import org.openecomp.mso.utils.CryptoUtils; -import com.woorea.openstack.keystone.model.Authentication; /** * JavaBean JSON class for a CloudIdentity. This bean represents a cloud identity @@ -143,15 +138,11 @@ public class CloudIdentity { } } } - - public Authentication getAuthentication () throws MsoException { + + public Authentication getAuthentication() { if (this.getIdentityAuthenticationType() != null) { - try { return AuthenticationMethodFactory.getAuthenticationFor(this); - } catch (IllegalAccessException | InstantiationException | ClassNotFoundException | IOException | URISyntaxException e) { - throw new MsoAdapterException("Could not retrieve authentication for " + this.identityAuthenticationType, e); - } - } else { // Fallback + } else { return new UsernamePassword(this.getMsoId(), this.getMsoPass()); } } @@ -236,29 +227,18 @@ public class CloudIdentity { public void setIdentityAuthenticationType(IdentityAuthenticationType identityAuthenticationType) { this.identityAuthenticationType = identityAuthenticationType; } - + @Override public String toString () { - StringBuilder stringBuilder = new StringBuilder (); - stringBuilder.append ("Cloud Identity Service: id=") - .append (id) - .append (", identityUrl=") - .append (this.identityUrl) - .append (", msoId=") - .append (msoId) - .append (", adminTenant=") - .append (adminTenant) - .append (", memberRole=") - .append (memberRole) - .append (", tenantMetadata=") - .append (tenantMetadata) - .append (", identityServerType=") - .append (identityServerType == null ? "null" : identityServerType.toString()) - .append (", identityAuthenticationType=") - .append (identityAuthenticationType == null ? "null" : identityAuthenticationType.toString()); - - return stringBuilder.toString (); - } + return "Cloud Identity Service: id=" + id + + ", identityUrl=" + this.identityUrl + + ", msoId=" + msoId + + ", adminTenant=" + adminTenant + + ", memberRole=" + memberRole + + ", tenantMetadata=" + tenantMetadata + + ", identityServerType=" + (identityServerType == null ? "null" : identityServerType.toString()) + + ", identityAuthenticationType=" + (identityAuthenticationType == null ? "null" : identityAuthenticationType.toString()); + } public static String encryptPassword (String msoPass) { try { diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactory.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactory.java index 85cb2967d6..c9be2c7949 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactory.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactory.java @@ -23,18 +23,13 @@ package org.openecomp.mso.cloud.authentication;
-import java.io.IOException;
-import java.net.URISyntaxException;
+import com.woorea.openstack.keystone.model.Authentication;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-
import org.openecomp.mso.cloud.CloudIdentity;
-import com.woorea.openstack.keystone.model.Authentication;
-
/**
* This factory manages all the wrappers associated to authentication types.
- *
*/
public final class AuthenticationMethodFactory {
@@ -58,7 +53,7 @@ public final class AuthenticationMethodFactory { }
}
- public static final synchronized Authentication getAuthenticationFor(CloudIdentity cloudIdentity) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException, URISyntaxException {
+ public static final synchronized Authentication getAuthenticationFor(CloudIdentity cloudIdentity) {
if (cloudIdentity == null) {
throw new IllegalArgumentException("Cloud identity cannot be null");
}
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/wrappers/RackspaceAPIKeyWrapper.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/wrappers/RackspaceAPIKeyWrapper.java index 5cf5587481..e82306b6cd 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/wrappers/RackspaceAPIKeyWrapper.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/wrappers/RackspaceAPIKeyWrapper.java @@ -35,16 +35,6 @@ import com.woorea.openstack.keystone.model.Authentication; */
public class RackspaceAPIKeyWrapper extends AuthenticationWrapper {
- /**
- *
- */
- public RackspaceAPIKeyWrapper() {
- // TODO Auto-generated constructor stub
- }
-
- /* (non-Javadoc)
- * @see org.openecomp.mso.cloud.authentication.AuthenticationWrapper#getAuthentication(org.openecomp.mso.cloud.CloudIdentity)
- */
@Override
public Authentication getAuthentication(CloudIdentity cloudIdentity) {
if (cloudIdentity == null) {
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/wrappers/UsernamePasswordWrapper.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/wrappers/UsernamePasswordWrapper.java index 5fd3e521f0..a1beabe564 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/wrappers/UsernamePasswordWrapper.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/wrappers/UsernamePasswordWrapper.java @@ -32,19 +32,9 @@ import com.woorea.openstack.keystone.model.authentication.UsernamePassword; /**
* This class implements the authentication wrapper for Openstack provided for
* user name and password authentication.
- *
*/
public class UsernamePasswordWrapper extends AuthenticationWrapper {
- /**
- *
- */
- public UsernamePasswordWrapper() {
- }
-
- /* (non-Javadoc)
- * @see org.openecomp.mso.cloud.authentication.AuthenticationWrapper#getAuthentication(org.openecomp.mso.cloud.CloudIdentity)
- */
@Override
public Authentication getAuthentication(CloudIdentity cloudIdentity) {
if (cloudIdentity == null) {
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/beans/NetworkInfo.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/beans/NetworkInfo.java index 2cda8f99e2..64460b20bb 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/beans/NetworkInfo.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/beans/NetworkInfo.java @@ -139,15 +139,13 @@ public class NetworkInfo { @Override public String toString() { - final StringBuilder sb = new StringBuilder("NetworkInfo{"); - sb.append("name='").append(name).append('\''); - sb.append(", id='").append(id).append('\''); - sb.append(", status=").append(status); - sb.append(", provider='").append(provider).append('\''); - sb.append(", vlans=").append(vlans); - sb.append(", subnets=").append(subnets); - sb.append('}'); - return sb.toString(); + return "NetworkInfo{" + "name='" + name + '\'' + + ", id='" + id + '\'' + + ", status=" + status + + ", provider='" + provider + '\'' + + ", vlans=" + vlans + + ", subnets=" + subnets + + '}'; } } diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/beans/StackInfo.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/beans/StackInfo.java index 600985e310..506b62994d 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/beans/StackInfo.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/beans/StackInfo.java @@ -67,10 +67,8 @@ public class StackInfo { if (stack.getStackStatus() == null) { this.status = HeatStatus.INIT; - } else if (heatStatusMap.containsKey(stack.getStackStatus())) { - this.status = heatStatusMap.get(stack.getStackStatus()); } else { - this.status = HeatStatus.UNKNOWN; + this.status = heatStatusMap.getOrDefault(stack.getStackStatus(), HeatStatus.UNKNOWN); } if (stack.getOutputs() != null) { this.outputs = new HashMap<>(); diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/exceptions/MsoOpenstackException.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/exceptions/MsoOpenstackException.java index 8aa8d3f5e8..eead8439d4 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/exceptions/MsoOpenstackException.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/exceptions/MsoOpenstackException.java @@ -68,16 +68,13 @@ public class MsoOpenstackException extends MsoException this.statusMessage = message; this.errorDetail = detail; } - + @Override - public String toString () { - StringBuilder error = new StringBuilder(); - error.append(""); - error.append(statusCode); - error.append(" "); - error.append(statusMessage); - error.append(": "); - error.append(errorDetail); - return error.toString(); + public String toString() { + return statusCode + + " " + + statusMessage + + ": " + + errorDetail; } } diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoCommonUtils.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoCommonUtils.java index 269f6b0cdf..7d6de317ad 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoCommonUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoCommonUtils.java @@ -129,6 +129,7 @@ public class MsoCommonUtils { Thread.sleep (retryDelay * 1000L); } catch (InterruptedException e1) { logger.debug ("Thread interrupted while sleeping", e1); + Thread.currentThread().interrupt(); } } else @@ -144,6 +145,7 @@ public class MsoCommonUtils { Thread.sleep (retryDelay * 1000L); } catch (InterruptedException e1) { logger.debug ("Thread interrupted while sleeping", e1); + Thread.currentThread().interrupt(); } } else diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentEntry.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentEntry.java index 69da437797..04dbf523d0 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentEntry.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentEntry.java @@ -65,11 +65,9 @@ public class MsoHeatEnvironmentEntry { @Override public String toString() { - final StringBuilder sb = new StringBuilder("MsoHeatEnvironmentEntry{"); - sb.append("parameters=").append(parameters); - sb.append(", resourceRegistryEntryRaw='").append(resourceRegistryEntryRaw).append('\''); - sb.append('}'); - return sb.toString(); + return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters + + ", resourceRegistryEntryRaw='" + resourceRegistryEntryRaw + '\'' + + '}'; } public String getRawEntry() { diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentResource.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentResource.java index a2e605f897..a0c9b7a105 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentResource.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentResource.java @@ -59,12 +59,10 @@ public class MsoHeatEnvironmentResource { @Override public String toString() { - StringBuilder str = new StringBuilder(); - str.append("\""); - str.append(this.name); - str.append("\": "); - str.append(this.value); - return str.toString(); + return "\"" + + this.name + + "\": " + + this.value; } @Override diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtils.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtils.java index acc6d72f63..6862492d7e 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtils.java @@ -143,7 +143,7 @@ public class MsoHeatUtils extends MsoCommonUtils { String tenantId, String stackName, String heatTemplate, - Map <String, ? extends Object> stackInputs, + Map <String, ?> stackInputs, boolean pollForCompletion, int timeoutMinutes) throws MsoException { // Just call the new method with the environment & files variable set to null @@ -165,7 +165,7 @@ public class MsoHeatUtils extends MsoCommonUtils { String tenantId, String stackName, String heatTemplate, - Map <String, ? extends Object> stackInputs, + Map <String, ?> stackInputs, boolean pollForCompletion, int timeoutMinutes, String environment) throws MsoException { @@ -188,7 +188,7 @@ public class MsoHeatUtils extends MsoCommonUtils { String tenantId, String stackName, String heatTemplate, - Map <String, ? extends Object> stackInputs, + Map <String, ?> stackInputs, boolean pollForCompletion, int timeoutMinutes, String environment, @@ -211,7 +211,7 @@ public class MsoHeatUtils extends MsoCommonUtils { String tenantId, String stackName, String heatTemplate, - Map <String, ? extends Object> stackInputs, + Map <String, ?> stackInputs, boolean pollForCompletion, int timeoutMinutes, String environment, @@ -275,7 +275,7 @@ public class MsoHeatUtils extends MsoCommonUtils { String tenantId, String stackName, String heatTemplate, - Map <String, ? extends Object> stackInputs, + Map <String, ?> stackInputs, boolean pollForCompletion, int timeoutMinutes, String environment, @@ -886,7 +886,7 @@ public class MsoHeatUtils extends MsoCommonUtils { public Map <String, Object> validateStackParams (Map <String, Object> inputParams, HeatTemplate heatTemplate) throws IllegalArgumentException { // Check that required parameters have been supplied for this template type - String missingParams = null; + StringBuilder missingParams = null; List <String> paramList = new ArrayList <> (); // TODO: Enhance DB to support defaults for Heat Template parameters @@ -894,9 +894,9 @@ public class MsoHeatUtils extends MsoCommonUtils { for (HeatTemplateParam parm : heatTemplate.getParameters ()) { if (parm.isRequired () && !inputParams.containsKey (parm.getParamName ())) { if (missingParams == null) { - missingParams = parm.getParamName (); + missingParams = new StringBuilder(parm.getParamName()); } else { - missingParams += "," + parm.getParamName (); + missingParams.append("," + parm.getParamName()); } } paramList.add (parm.getParamName ()); @@ -1106,11 +1106,8 @@ public class MsoHeatUtils extends MsoCommonUtils { } public boolean isExpired () { - if (expires == null) { - return true; - } + return expires == null || System.currentTimeMillis() > expires.getTimeInMillis(); - return System.currentTimeMillis() > expires.getTimeInMillis(); } } @@ -1217,18 +1214,18 @@ public class MsoHeatUtils extends MsoCommonUtils { } else { for (String key : params.keySet()) { if (params.get(key) instanceof String) { - sb.append("\n" + key + "=" + (String) params.get(key)); + sb.append("\n").append(key).append("=").append((String) params.get(key)); } else if (params.get(key) instanceof JsonNode) { String jsonStringOut = this.convertNode((JsonNode)params.get(key)); - sb.append("\n" + key + "=" + jsonStringOut); + sb.append("\n").append(key).append("=").append(jsonStringOut); } else if (params.get(key) instanceof Integer) { String integerOut = "" + params.get(key); - sb.append("\n" + key + "=" + integerOut); + sb.append("\n").append(key).append("=").append(integerOut); } else { try { String str = params.get(key).toString(); - sb.append("\n" + key + "=" + str); + sb.append("\n").append(key).append("=").append(str); } catch (Exception e) { LOGGER.debug("Exception :",e); } @@ -1243,8 +1240,6 @@ public class MsoHeatUtils extends MsoCommonUtils { final Object obj = JSON_MAPPER.treeToValue(node, Object.class); final String json = JSON_MAPPER.writeValueAsString(obj); return json; - } catch (JsonParseException jpe) { - LOGGER.debug("Error converting json to string " + jpe.getMessage(), jpe); } catch (Exception e) { LOGGER.debug("Error converting json to string " + e.getMessage(), e); } @@ -1272,16 +1267,16 @@ public class MsoHeatUtils extends MsoCommonUtils { int counter = 0; sb.append("OUTPUTS:\n"); for (String key : outputs.keySet()) { - sb.append("outputs[" + counter++ + "]: " + key + "="); + sb.append("outputs[").append(counter++).append("]: ").append(key).append("="); Object obj = outputs.get(key); if (obj instanceof String) { - sb.append((String)obj +" (a string)"); + sb.append((String) obj).append(" (a string)"); } else if (obj instanceof JsonNode) { - sb.append(this.convertNode((JsonNode)obj) + " (a JsonNode)"); + sb.append(this.convertNode((JsonNode) obj)).append(" (a JsonNode)"); } else if (obj instanceof java.util.LinkedHashMap) { try { String str = JSON_MAPPER.writeValueAsString(obj); - sb.append(str + " (a java.util.LinkedHashMap)"); + sb.append(str).append(" (a java.util.LinkedHashMap)"); } catch (Exception e) { LOGGER.debug("Exception :",e); sb.append("(a LinkedHashMap value that would not convert nicely)"); @@ -1439,7 +1434,7 @@ public class MsoHeatUtils extends MsoCommonUtils { if (inputs == null) { LOGGER.debug("convertInputMap - inputs is null - nothing to do here"); - return new HashMap<String, Object>(); + return new HashMap<>(); } LOGGER.debug("convertInputMap in MsoHeatUtils called, with " + inputs.size() + " inputs, and template " + template.getArtifactUuid()); @@ -1548,7 +1543,7 @@ public class MsoHeatUtils extends MsoCommonUtils { } } else if ("boolean".equalsIgnoreCase(type)) { String booleanString = inputs.get(key); - Boolean aBool = new Boolean(booleanString); + Boolean aBool = Boolean.valueOf(booleanString); if (alias) newInputs.put(realName, aBool); else diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsWithUpdate.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsWithUpdate.java index 2465b30eca..75bb00351c 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsWithUpdate.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsWithUpdate.java @@ -230,7 +230,7 @@ public class MsoHeatUtilsWithUpdate extends MsoHeatUtils { if (haveFiles && haveHeatFiles) { // Let's do this here - not in the bean LOGGER.debug ("Found files AND heatFiles - combine and add!"); - Map <String, Object> combinedFiles = new HashMap <String, Object> (); + Map <String, Object> combinedFiles = new HashMap<>(); for (String keyString : files.keySet ()) { combinedFiles.put (keyString, files.get (keyString)); } @@ -358,23 +358,23 @@ public class MsoHeatUtilsWithUpdate extends MsoHeatUtils { sb.append("(outputs is empty)"); return sb; } - Map<String, Object> outputs = new HashMap<String,Object>(); + Map<String, Object> outputs = new HashMap<>(); for (Output outputItem : outputList) { outputs.put(outputItem.getOutputKey(), outputItem.getOutputValue()); } int counter = 0; sb.append("OUTPUTS:\n"); for (String key : outputs.keySet()) { - sb.append("outputs[" + counter++ + "]: " + key + "="); + sb.append("outputs[").append(counter++).append("]: ").append(key).append("="); Object obj = outputs.get(key); if (obj instanceof String) { - sb.append((String)obj +" (a string)"); + sb.append((String) obj).append(" (a string)"); } else if (obj instanceof JsonNode) { - sb.append(this.convertNode((JsonNode)obj) + " (a JsonNode)"); + sb.append(this.convertNode((JsonNode) obj)).append(" (a JsonNode)"); } else if (obj instanceof java.util.LinkedHashMap) { try { String str = JSON_MAPPER.writeValueAsString(obj); - sb.append(str + " (a java.util.LinkedHashMap)"); + sb.append(str).append(" (a java.util.LinkedHashMap)"); } catch (Exception e) { LOGGER.debug("Exception :", e); sb.append("(a LinkedHashMap value that would not convert nicely)"); @@ -428,8 +428,6 @@ public class MsoHeatUtilsWithUpdate extends MsoHeatUtils { final Object obj = JSON_MAPPER.treeToValue(node, Object.class); final String json = JSON_MAPPER.writeValueAsString(obj); return json; - } catch (JsonParseException jpe) { - LOGGER.debug("Error converting json to string " + jpe.getMessage(), jpe); } catch (Exception e) { LOGGER.debug("Error converting json to string " + e.getMessage(), e); } diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java index be36d67e0b..3b710032c1 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java @@ -209,7 +209,7 @@ public class MsoKeystoneUtils extends MsoTenantUtils { return null; } - Map <String, String> metadata = new HashMap <String, String> (); + Map <String, String> metadata = new HashMap<>(); if (cloudSite.getIdentityService ().hasTenantMetadata ()) { OpenStackRequest <Metadata> request = keystoneAdminClient.tenants ().showMetadata (tenant.getId ()); Metadata tenantMetadata = executeAndRecordOpenstackRequest (request, msoProps); @@ -255,7 +255,7 @@ public class MsoKeystoneUtils extends MsoTenantUtils { return null; } - Map <String, String> metadata = new HashMap <String, String> (); + Map <String, String> metadata = new HashMap<>(); if (cloudSite.getIdentityService ().hasTenantMetadata ()) { OpenStackRequest <Metadata> request = keystoneAdminClient.tenants ().showMetadata (tenant.getId ()); Metadata tenantMetadata = executeAndRecordOpenstackRequest (request, msoProps); @@ -567,11 +567,8 @@ public class MsoKeystoneUtils extends MsoTenantUtils { } public boolean isExpired () { - if (expires == null) { - return true; - } + return expires == null || System.currentTimeMillis() > expires.getTimeInMillis(); - return System.currentTimeMillis() > expires.getTimeInMillis(); } } diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoNeutronUtils.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoNeutronUtils.java index 50a594663e..9eba799e18 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoNeutronUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoNeutronUtils.java @@ -63,7 +63,7 @@ public class MsoNeutronUtils extends MsoCommonUtils // token will be used until it expires. // // The cache key is "tenantId:cloudId" - private static Map<String,NeutronCacheEntry> neutronClientCache = new HashMap<String,NeutronCacheEntry>(); + private static Map<String,NeutronCacheEntry> neutronClientCache = new HashMap<>(); // Fetch cloud configuration each time (may be cached in CloudConfig class) private CloudConfig cloudConfig; @@ -129,7 +129,7 @@ public class MsoNeutronUtils extends MsoCommonUtils } } else if (type == NetworkType.MULTI_PROVIDER) { if (provider != null && vlans != null && vlans.size() > 0) { - List<Segment> segments = new ArrayList<Segment>(vlans.size()); + List<Segment> segments = new ArrayList<>(vlans.size()); for (int vlan : vlans) { Segment segment = new Segment(); segment.setProviderPhysicalNetwork (provider); @@ -297,7 +297,7 @@ public class MsoNeutronUtils extends MsoCommonUtils } } else if (type == NetworkType.MULTI_PROVIDER) { if (provider != null && vlans != null && vlans.size() > 0) { - List<Segment> segments = new ArrayList<Segment>(vlans.size()); + List<Segment> segments = new ArrayList<>(vlans.size()); for (int vlan : vlans) { Segment segment = new Segment(); segment.setProviderPhysicalNetwork (provider); @@ -559,11 +559,7 @@ public class MsoNeutronUtils extends MsoCommonUtils } public boolean isExpired() { - if (expires == null) { - return true; - } - - return System.currentTimeMillis() > expires.getTimeInMillis(); + return expires == null || System.currentTimeMillis() > expires.getTimeInMillis(); } } diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoYamlEditorWithEnvt.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoYamlEditorWithEnvt.java index c227d72f0c..8002087ddd 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoYamlEditorWithEnvt.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoYamlEditorWithEnvt.java @@ -27,6 +27,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.Map.Entry; @@ -73,33 +74,32 @@ public class MsoYamlEditorWithEnvt { if (resourceMap == null) { return paramSet; } - Iterator <Entry <String, Object>> it = resourceMap.entrySet().iterator(); - - while (it.hasNext()) { - MsoHeatEnvironmentParameter hep = new MsoHeatEnvironmentParameter(); - Map.Entry <String, Object> pair = it.next(); - String value; - Object obj = pair.getValue(); - if (obj instanceof java.lang.String) { - value = yaml.dump(obj); - // but this adds an extra '\n' at the end - which won't hurt - but we don't need it - value = value.substring(0, value.length() - 1); - } else if (obj instanceof java.util.LinkedHashMap) { - //Handle that it's json - try { - value = JSON_MAPPER.writeValueAsString(obj); - } catch (Exception e) { - LOGGER.debug("Exception:", e); - value = "_BAD_JSON_MAPPING"; - } - } else { - //this handles integers/longs/floats/etc. - value = String.valueOf(obj); - } - hep.setName((String) pair.getKey()); - hep.setValue(value); - paramSet.add(hep); - } + + for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) { + MsoHeatEnvironmentParameter hep = new MsoHeatEnvironmentParameter(); + Entry<String, Object> pair = stringObjectEntry; + String value; + Object obj = pair.getValue(); + if (obj instanceof String) { + value = yaml.dump(obj); + // but this adds an extra '\n' at the end - which won't hurt - but we don't need it + value = value.substring(0, value.length() - 1); + } else if (obj instanceof LinkedHashMap) { + //Handle that it's json + try { + value = JSON_MAPPER.writeValueAsString(obj); + } catch (Exception e) { + LOGGER.debug("Exception:", e); + value = "_BAD_JSON_MAPPING"; + } + } else { + //this handles integers/longs/floats/etc. + value = String.valueOf(obj); + } + hep.setName((String) pair.getKey()); + hep.setValue(value); + paramSet.add(hep); + } return paramSet; } public synchronized Set <MsoHeatEnvironmentResource> getResourceListFromEnvt() { @@ -107,15 +107,14 @@ public class MsoYamlEditorWithEnvt { Set<MsoHeatEnvironmentResource> resourceList = new HashSet<>(); @SuppressWarnings("unchecked") Map<String, Object> resourceMap = (Map<String,Object>) yml.get("resource_registry"); - Iterator<Entry <String,Object>> it = resourceMap.entrySet().iterator(); - - while (it.hasNext()) { - MsoHeatEnvironmentResource her = new MsoHeatEnvironmentResource(); - Map.Entry<String, Object> pair = it.next(); - her.setName((String) pair.getKey()); - her.setValue((String) pair.getValue()); - resourceList.add(her); - } + + for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) { + MsoHeatEnvironmentResource her = new MsoHeatEnvironmentResource(); + Entry<String, Object> pair = stringObjectEntry; + her.setName((String) pair.getKey()); + her.setValue((String) pair.getValue()); + resourceList.add(her); + } return resourceList; } catch (Exception e) { LOGGER.debug("Exception:", e); @@ -126,35 +125,34 @@ public class MsoYamlEditorWithEnvt { Set <HeatTemplateParam> paramSet = new HashSet <> (); @SuppressWarnings("unchecked") Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters"); - Iterator <Entry <String, Object>> it = resourceMap.entrySet ().iterator (); - while (it.hasNext ()) { - HeatTemplateParam param = new HeatTemplateParam (); - Map.Entry <String, Object> pair = it.next (); + for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) { + HeatTemplateParam param = new HeatTemplateParam(); + Entry<String, Object> pair = stringObjectEntry; @SuppressWarnings("unchecked") - Map <String, String> resourceEntry = (Map <String, String>) pair.getValue (); + Map<String, String> resourceEntry = (Map<String, String>) pair.getValue(); String value = null; try { - value = resourceEntry.get ("default"); - } catch (java.lang.ClassCastException cce) { + value = resourceEntry.get("default"); + } catch (ClassCastException cce) { LOGGER.debug("Exception:", cce); - // This exception only - the value is an integer. For what we're doing - // here - we don't care - so set value to something - and it will - // get marked as not being required - which is correct. - //System.out.println("cce exception!"); - value = "300"; - // okay + // This exception only - the value is an integer. For what we're doing + // here - we don't care - so set value to something - and it will + // get marked as not being required - which is correct. + //System.out.println("cce exception!"); + value = "300"; + // okay } - param.setParamName ((String) pair.getKey ()); + param.setParamName((String) pair.getKey()); if (value != null) { - param.setRequired (false); + param.setRequired(false); } else { - param.setRequired (true); + param.setRequired(true); } - value = resourceEntry.get ("type"); - param.setParamType (value); + value = resourceEntry.get("type"); + param.setParamType(value); - paramSet.add (param); + paramSet.add(param); } return paramSet; diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/adapter_utils/tests/MsoHeatUtilsTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/adapter_utils/tests/MsoHeatUtilsTest.java index 6fd95d5948..c2d4f9ba60 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/adapter_utils/tests/MsoHeatUtilsTest.java +++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/adapter_utils/tests/MsoHeatUtilsTest.java @@ -60,7 +60,7 @@ public class MsoHeatUtilsTest extends MsoCommonUtils { public final void testCreateStackBadCloudConfig() throws MsoStackAlreadyExists, MsoTenantNotFound, MsoException, MsoCloudSiteNotFound { try { - msoHeatUtils.createStack("DOESNOTEXIST", "test", "stackName", "test", new HashMap<String, Object>(), + msoHeatUtils.createStack("DOESNOTEXIST", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10); } catch (MsoCloudSiteNotFound e) { @@ -74,7 +74,7 @@ public class MsoHeatUtilsTest extends MsoCommonUtils { public final void testCreateStackFailedConnectionHeatClient() throws MsoStackAlreadyExists, MsoTenantNotFound, MsoException, MsoCloudSiteNotFound { try { - msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<String, Object>(), Boolean.TRUE, + msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10); } catch (MsoIOException e) { @@ -86,7 +86,7 @@ public class MsoHeatUtilsTest extends MsoCommonUtils { public final void testCreateStackFailedConnection() throws MsoStackAlreadyExists, MsoTenantNotFound, MsoException, MsoCloudSiteNotFound { try { - msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<String, Object>(), Boolean.TRUE, + msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10); } catch (MsoIOException e) { @@ -97,7 +97,7 @@ public class MsoHeatUtilsTest extends MsoCommonUtils { @Test public final void createStackSuccessWithEnvironment() throws MsoException { try { - msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<String, Object>(), Boolean.TRUE, 10, + msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10, "environment"); } catch (MsoIOException e) { @@ -108,8 +108,8 @@ public class MsoHeatUtilsTest extends MsoCommonUtils { @Test public final void createStackSuccessWithFiles() throws MsoException { try { - msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<String, Object>(), Boolean.TRUE, 10, - "environment", new HashMap<String, Object>()); + msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10, + "environment", new HashMap<>()); } catch (MsoIOException e) { } @@ -119,8 +119,8 @@ public class MsoHeatUtilsTest extends MsoCommonUtils { @Test public final void createStackSuccessWithHeatFiles() throws MsoException { try { - msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<String, Object>(), Boolean.TRUE, 10, - "environment", new HashMap<String, Object>(), new HashMap<String, Object>()); + msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10, + "environment", new HashMap<>(), new HashMap<>()); } catch (MsoIOException e) { } @@ -131,7 +131,7 @@ public class MsoHeatUtilsTest extends MsoCommonUtils { CreateStackParam param = new CreateStackParam(); param.setDisableRollback(false); param.setEnvironment("environment"); - param.setFiles(new HashMap<String, Object>()); + param.setFiles(new HashMap<>()); param.setParameters(new HashMap<>()); param.setStackName("stackName"); param.setTemplate("template"); diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactoryTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactoryTest.java new file mode 100644 index 0000000000..2cfce276d8 --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactoryTest.java @@ -0,0 +1,103 @@ +/* + * ============LICENSE_START========================================== + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Licensed 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. + * ============LICENSE_END============================================ + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + * + */ + +package org.openecomp.mso.cloud.authentication; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.woorea.openstack.keystone.model.Authentication; +import org.junit.Test; +import org.openecomp.mso.cloud.CloudIdentity; +import org.openecomp.mso.cloud.CloudIdentity.IdentityAuthenticationType; +import org.openecomp.mso.cloud.authentication.wrappers.RackspaceAPIKeyWrapper; + +public class AuthenticationMethodFactoryTest { + + private static final Class WRAPPER_CLASS = RackspaceAPIKeyWrapper.class; + private static final String AUTHENTICATION_TYPE = "authenticationTest"; + + @Test + public void register_NoExceptionThrown() throws IllegalAccessException, InstantiationException { + AuthenticationMethodFactory.register(AUTHENTICATION_TYPE, WRAPPER_CLASS); + } + + @Test + public void register_throwExceptionWhenAuthTypeIsNull() throws InstantiationException, IllegalAccessException { + try { + AuthenticationMethodFactory.register(null, WRAPPER_CLASS); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty().contains("Authentication Type to register cannot be null " + + "or an empty name string"); + } + } + + @Test + public void register_throwExceptionWhenAuthTypeIsEmpty() throws InstantiationException, IllegalAccessException { + try { + AuthenticationMethodFactory.register("", WRAPPER_CLASS); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty().contains("Authentication Type to register cannot be null " + + "or an empty name string"); + } + } + + @Test + public void register_throwExceptionWhenWrapperIsNull() throws IllegalAccessException, InstantiationException { + try { + AuthenticationMethodFactory.register(AUTHENTICATION_TYPE, null); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty() + .contains("Wrapper Class to register for Authentication cannot be null"); + } + } + + @Test + public void getAuthentication_NoExceptionThrown() { + CloudIdentity cloudIdentity = new CloudIdentity(); + cloudIdentity.setIdentityAuthenticationType(IdentityAuthenticationType.RACKSPACE_APIKEY); + cloudIdentity.setMsoId("msoIdTest"); + cloudIdentity.setMsoPass("123"); + Authentication result = AuthenticationMethodFactory.getAuthenticationFor(cloudIdentity); + assertThat(result).isNotNull(); + } + + @Test + public void getAuthentication_ThrowExWhenCloudSiteIsNull() { + try { + AuthenticationMethodFactory.getAuthenticationFor(null); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty().contains("Cloud identity cannot be null"); + } + } + + @Test + public void getAuthentication_ThrowExWhenIdentityAuthenticationTypeIsNotSet() { + try { + AuthenticationMethodFactory.getAuthenticationFor(new CloudIdentity()); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty() + .contains("Cloud identity authentication type cannot be null or empty"); + } + } + +} diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodTest.java index ccfede7297..b6c1c7373f 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodTest.java +++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodTest.java @@ -25,16 +25,11 @@ package org.openecomp.mso.cloud.authentication; import static org.junit.Assert.assertTrue;
-import java.io.IOException;
-import java.net.URISyntaxException;
-
+import com.woorea.openstack.keystone.model.Authentication;
+import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
import org.junit.Test;
import org.openecomp.mso.cloud.CloudIdentity;
import org.openecomp.mso.cloud.authentication.models.RackspaceAuthentication;
-import org.openecomp.mso.openstack.exceptions.MsoException;
-
-import com.woorea.openstack.keystone.model.Authentication;
-import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
/**
* A few JUnit tests to evaluate the new factory that manages authentication
@@ -50,52 +45,15 @@ public class AuthenticationMethodTest { public AuthenticationMethodTest() {
// TODO Auto-generated constructor stub
}
-
- @Test
- public void testCustomRackspaceAuth() {
- CloudIdentity ci = new CloudIdentity();
- ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.RACKSPACE_APIKEY);
- ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
- ci.setMsoId("test");
-
- try {
- Authentication auth = AuthenticationMethodFactory.getAuthenticationFor(ci);
- assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));
- } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IOException
- | URISyntaxException e) {
- e.printStackTrace();
- }
- }
-
- @Test
- public void testCoreUsernamePasswordAuth() {
- CloudIdentity ci = new CloudIdentity();
- ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.USERNAME_PASSWORD);
- ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
- ci.setMsoId("someuser");
-
- try {
- Authentication auth = AuthenticationMethodFactory.getAuthenticationFor(ci);
- assertTrue(UsernamePassword.class.equals(auth.getClass()));
- } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IOException
- | URISyntaxException e) {
- e.printStackTrace();
- }
- }
-
+
@Test
public void testCustomRackspaceAuthFromCloudIdentity() {
CloudIdentity ci = new CloudIdentity();
ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.RACKSPACE_APIKEY);
ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
ci.setMsoId("test");
-
- try {
- Authentication auth = ci.getAuthentication();
- assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));
- } catch (MsoException e) {
- e.printStackTrace();
- }
+ Authentication auth = ci.getAuthentication();
+ assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));
}
@Test
@@ -104,12 +62,7 @@ public class AuthenticationMethodTest { ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.USERNAME_PASSWORD);
ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
ci.setMsoId("someuser");
-
- try {
- Authentication auth = ci.getAuthentication();
- assertTrue(UsernamePassword.class.equals(auth.getClass()));
- } catch (MsoException e) {
- e.printStackTrace();
- }
+ Authentication auth = ci.getAuthentication();
+ assertTrue(UsernamePassword.class.equals(auth.getClass()));
}
}
diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/wrappers/RackspaceAPIKeyWrapperTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/wrappers/RackspaceAPIKeyWrapperTest.java new file mode 100644 index 0000000000..33f91c641e --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/wrappers/RackspaceAPIKeyWrapperTest.java @@ -0,0 +1,55 @@ +/* + * ============LICENSE_START========================================== + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Licensed 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. + * ============LICENSE_END============================================ + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + * + */ + +package org.openecomp.mso.cloud.authentication.wrappers; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.woorea.openstack.keystone.model.Authentication; +import org.junit.Test; +import org.openecomp.mso.cloud.authentication.models.RackspaceAuthentication; + +public class RackspaceAPIKeyWrapperTest { + + @Test + public void getAuthenticationSuccessful() { + RackspaceAPIKeyWrapper testedObject = new RackspaceAPIKeyWrapper(); + Authentication authentication = testedObject.getAuthentication(WrapperTestUtility.createCloudIdentity()); + + assertThat(authentication).isInstanceOf(RackspaceAuthentication.class); + RackspaceAuthentication rackspaceAuthentication = (RackspaceAuthentication) authentication; + assertThat(rackspaceAuthentication.getToken().getUsername()) + .isEqualTo(WrapperTestUtility.CLOUD_IDENTITY_MSO_ID); + assertThat(rackspaceAuthentication.getToken().getApiKey()) + .isEqualTo(WrapperTestUtility.CLOUD_IDENTITY_MSO_PASS); + } + + @Test + public void getAuthenticationThrowsException() { + assertThatThrownBy(() -> new RackspaceAPIKeyWrapper().getAuthentication(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(WrapperTestUtility.EXCEPTION_MESSAGE); + } + +} diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/wrappers/UsernamePasswordWrapperTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/wrappers/UsernamePasswordWrapperTest.java new file mode 100644 index 0000000000..0cfe287dfe --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/wrappers/UsernamePasswordWrapperTest.java @@ -0,0 +1,55 @@ +/* + * ============LICENSE_START========================================== + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Licensed 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. + * ============LICENSE_END============================================ + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + * + */ + +package org.openecomp.mso.cloud.authentication.wrappers; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.woorea.openstack.keystone.model.Authentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import org.junit.Test; + +public class UsernamePasswordWrapperTest { + + @Test + public void getAuthenticationSuccessful() { + UsernamePasswordWrapper testedObject = new UsernamePasswordWrapper(); + Authentication authentication = testedObject.getAuthentication(WrapperTestUtility.createCloudIdentity()); + + assertThat(authentication).isInstanceOf(UsernamePassword.class); + UsernamePassword usernamePassword = (UsernamePassword) authentication; + assertThat(usernamePassword.getPasswordCredentials().getUsername()) + .isEqualTo(WrapperTestUtility.CLOUD_IDENTITY_MSO_ID); + assertThat(usernamePassword.getPasswordCredentials().getPassword()) + .isEqualTo(WrapperTestUtility.CLOUD_IDENTITY_MSO_PASS); + } + + @Test + public void getAuthenticationThrowsException() { + assertThatThrownBy(() -> new UsernamePasswordWrapper().getAuthentication(null)). + isInstanceOf(IllegalArgumentException.class). + hasMessage(WrapperTestUtility.EXCEPTION_MESSAGE); + } + +} diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/wrappers/WrapperTestUtility.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/wrappers/WrapperTestUtility.java new file mode 100644 index 0000000000..3cbc48d090 --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/wrappers/WrapperTestUtility.java @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START========================================== + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Licensed 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. + * ============LICENSE_END============================================ + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + * + */ + +package org.openecomp.mso.cloud.authentication.wrappers; + +import org.openecomp.mso.cloud.CloudIdentity; + +final class WrapperTestUtility { + + static final String CLOUD_IDENTITY_MSO_ID = "msoIdTest"; + static final String CLOUD_IDENTITY_MSO_PASS = "msoPassTest"; + static final String EXCEPTION_MESSAGE = "Provided cloud identity is null, cannot extract username and " + + "password"; + + private WrapperTestUtility() { + } + + static CloudIdentity createCloudIdentity() { + CloudIdentity cloudIdentity = new CloudIdentity(); + cloudIdentity.setMsoId(CLOUD_IDENTITY_MSO_ID); + cloudIdentity.setMsoPass(CloudIdentity.encryptPassword(CLOUD_IDENTITY_MSO_PASS)); + return cloudIdentity; + } +} |