From cf896d8cff45754a3bdb00f2f277eef3a5ae0cd2 Mon Sep 17 00:00:00 2001 From: "beili.zhou" Date: Wed, 27 Sep 2017 14:37:10 -0400 Subject: Increase sonar coverage for common Add Junit test in appc-common - cache - cache/impl - configuration - exceptions - i18n - logging Issue-Id: APPC-230 Change-Id: I04c457da4dfa881c1109273b8fd3e16e76ebd68f Signed-off-by: beili.zhou --- appc-common/pom.xml | 12 + .../main/java/org/openecomp/appc/Constants.java | 4 +- .../org/openecomp/appc/cache/CacheStrategies.java | 6 +- .../org/openecomp/appc/cache/CacheStrategy.java | 19 +- .../org/openecomp/appc/cache/MetadataCache.java | 19 +- .../org/openecomp/appc/cache/impl/LRUCache.java | 7 +- .../appc/cache/impl/MetadataCacheFactory.java | 21 +- .../appc/cache/impl/MetadataCacheImpl.java | 16 +- .../appc/configuration/DefaultConfiguration.java | 45 ++- .../openecomp/appc/exceptions/APPCException.java | 2 - .../appc/exceptions/UnknownProviderException.java | 7 +- .../openecomp/appc/logging/LoggingConstants.java | 40 ++- .../org/openecomp/appc/logging/LoggingUtils.java | 49 ++- .../objects/DependencyModelIdentifier.java | 48 ++- .../openecomp/appc/cache/CacheStrategiesTest.java | 48 +++ .../openecomp/appc/cache/impl/LRUCacheTest.java | 61 ++++ .../appc/cache/impl/MetadataCacheFactoryTest.java | 69 +++++ .../appc/cache/impl/MetadataCacheImplTest.java | 64 ++++ .../configuration/DefaultConfigurationTest.java | 337 +++++++++++++++++++++ .../appc/exceptions/APPCExceptionTest.java | 89 ++++++ .../appc/exceptions/InvalidInputExceptionTest.java | 39 +++ .../appc/exceptions/InvalidStateExceptionTest.java | 39 +++ .../exceptions/UnknownProviderExceptionTest.java | 93 ++++++ .../test/java/org/openecomp/appc/i18n/MsgTest.java | 11 +- .../appc/logging/LoggingConstantsTest.java | 60 ++++ .../objects/DependencyModelIdentifierTest.java | 105 +++++++ 26 files changed, 1229 insertions(+), 81 deletions(-) create mode 100644 appc-common/src/test/java/org/openecomp/appc/cache/CacheStrategiesTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/cache/impl/LRUCacheTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/cache/impl/MetadataCacheFactoryTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/cache/impl/MetadataCacheImplTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/configuration/DefaultConfigurationTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/exceptions/APPCExceptionTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/exceptions/InvalidInputExceptionTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/exceptions/InvalidStateExceptionTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/exceptions/UnknownProviderExceptionTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/logging/LoggingConstantsTest.java create mode 100644 appc-common/src/test/java/org/openecomp/appc/metadata/objects/DependencyModelIdentifierTest.java (limited to 'appc-common') diff --git a/appc-common/pom.xml b/appc-common/pom.xml index 72348bf74..4f228bc30 100644 --- a/appc-common/pom.xml +++ b/appc-common/pom.xml @@ -96,6 +96,18 @@ org.onap.ccsdk.sli.core dblib-provider + + org.powermock + powermock-reflect + + + org.powermock + powermock-module-junit4 + + + org.powermock + powermock-api-mockito + diff --git a/appc-common/src/main/java/org/openecomp/appc/Constants.java b/appc-common/src/main/java/org/openecomp/appc/Constants.java index c5315edd7..5e59971ac 100644 --- a/appc-common/src/main/java/org/openecomp/appc/Constants.java +++ b/appc-common/src/main/java/org/openecomp/appc/Constants.java @@ -168,10 +168,10 @@ public final class Constants { @SuppressWarnings("nls") public static final String STATUS_GETTER = "status-getter"; - @SuppressWarnings("nls") + @SuppressWarnings("nls") public static final String VM_FUSION_STATUS_GETTER = "fusion-vm-status-getter"; - /** + /** * The name for the status vm attribute to be set in the context when executing a vmstatuscheck. */ diff --git a/appc-common/src/main/java/org/openecomp/appc/cache/CacheStrategies.java b/appc-common/src/main/java/org/openecomp/appc/cache/CacheStrategies.java index 7f63e00bf..196b645a9 100644 --- a/appc-common/src/main/java/org/openecomp/appc/cache/CacheStrategies.java +++ b/appc-common/src/main/java/org/openecomp/appc/cache/CacheStrategies.java @@ -24,7 +24,9 @@ package org.openecomp.appc.cache; - +/** + * Enum of CacheStrategies + */ public enum CacheStrategies { - LRU; + LRU } diff --git a/appc-common/src/main/java/org/openecomp/appc/cache/CacheStrategy.java b/appc-common/src/main/java/org/openecomp/appc/cache/CacheStrategy.java index aa0a5d785..e07708119 100644 --- a/appc-common/src/main/java/org/openecomp/appc/cache/CacheStrategy.java +++ b/appc-common/src/main/java/org/openecomp/appc/cache/CacheStrategy.java @@ -24,8 +24,23 @@ package org.openecomp.appc.cache; - -public interface CacheStrategy { +/** + * Interface of CacheStrategy + * @param key + * @param value + */ +public interface CacheStrategy { + /** + * Get object + * @param key of the object + * @return value of the object + */ V getObject(K key); + + /** + * Put object + * @param key of the object + * @param value of the object + */ void putObject(K key,V value); } diff --git a/appc-common/src/main/java/org/openecomp/appc/cache/MetadataCache.java b/appc-common/src/main/java/org/openecomp/appc/cache/MetadataCache.java index d5b1d7b93..8196e8e3d 100644 --- a/appc-common/src/main/java/org/openecomp/appc/cache/MetadataCache.java +++ b/appc-common/src/main/java/org/openecomp/appc/cache/MetadataCache.java @@ -24,8 +24,23 @@ package org.openecomp.appc.cache; - -public interface MetadataCache { +/** + * Interface of MetadataCache + * @param key + * @param value + */ +public interface MetadataCache { + /** + * Get object + * @param key of the object + * @return value of the object + */ V getObject(K key); + + /** + * Put object + * @param key of the object + * @param value of the object + */ void putObject(K key,V value); } diff --git a/appc-common/src/main/java/org/openecomp/appc/cache/impl/LRUCache.java b/appc-common/src/main/java/org/openecomp/appc/cache/impl/LRUCache.java index 5cb8ef534..b97a5f642 100644 --- a/appc-common/src/main/java/org/openecomp/appc/cache/impl/LRUCache.java +++ b/appc-common/src/main/java/org/openecomp/appc/cache/impl/LRUCache.java @@ -29,12 +29,17 @@ import java.util.Map; import org.openecomp.appc.cache.CacheStrategy; +/** + * LRU cache implements CacheStategy + * @param Key + * @param Value + */ public class LRUCache implements CacheStrategy { private Map map; LRUCache(final Integer capacity){ - map = new LinkedHashMap(capacity, 0.75F, true){ + map = new LinkedHashMap(capacity, 0.75F, true) { @Override protected boolean removeEldestEntry(Map.Entry eldest){ return size() > capacity; diff --git a/appc-common/src/main/java/org/openecomp/appc/cache/impl/MetadataCacheFactory.java b/appc-common/src/main/java/org/openecomp/appc/cache/impl/MetadataCacheFactory.java index 306a23a99..91ad48f9b 100644 --- a/appc-common/src/main/java/org/openecomp/appc/cache/impl/MetadataCacheFactory.java +++ b/appc-common/src/main/java/org/openecomp/appc/cache/impl/MetadataCacheFactory.java @@ -25,16 +25,23 @@ package org.openecomp.appc.cache.impl; import org.openecomp.appc.cache.CacheStrategies; -import org.openecomp.appc.cache.CacheStrategy; import org.openecomp.appc.cache.MetadataCache; +/** + * Metadata Cache Factory + */ public class MetadataCacheFactory { - private static class ReferenceHolder{ + private static class ReferenceHolder { + private ReferenceHolder() { + throw new IllegalAccessError("ReferenceHolder"); + } + private static final MetadataCacheFactory FACTORY = new MetadataCacheFactory(); } - private MetadataCacheFactory(){ + private MetadataCacheFactory() { + // do nothing } public static MetadataCacheFactory getInstance(){ @@ -44,7 +51,13 @@ public class MetadataCacheFactory { public MetadataCache getMetadataCache(){ return new MetadataCacheImpl(); } - public MetadataCache getMetadataCache(CacheStrategies cacheStrategy){ + + /** + * Get MetadataCache + * @param cacheStrategy the CacheStrategies to be used to build MetadataCacheImpl + * @return a new instance of MetadataCacheImpl + */ + public MetadataCache getMetadataCache(CacheStrategies cacheStrategy) { return new MetadataCacheImpl(cacheStrategy); } diff --git a/appc-common/src/main/java/org/openecomp/appc/cache/impl/MetadataCacheImpl.java b/appc-common/src/main/java/org/openecomp/appc/cache/impl/MetadataCacheImpl.java index 966100666..249a97c3c 100644 --- a/appc-common/src/main/java/org/openecomp/appc/cache/impl/MetadataCacheImpl.java +++ b/appc-common/src/main/java/org/openecomp/appc/cache/impl/MetadataCacheImpl.java @@ -28,7 +28,11 @@ import org.openecomp.appc.cache.CacheStrategies; import org.openecomp.appc.cache.CacheStrategy; import org.openecomp.appc.cache.MetadataCache; - +/** + * Implementation of MetadataCache + * @param Key + * @param Value + */ public class MetadataCacheImpl implements MetadataCache { private CacheStrategy strategy; @@ -42,9 +46,13 @@ public class MetadataCacheImpl implements MetadataCache { } private CacheStrategy initializeStrategy(CacheStrategies strategy) { - switch (strategy){ - case LRU: - return new LRUCache<>(50); + if (strategy != null) { + switch (strategy) { + case LRU: + return new LRUCache<>(50); + default: + // do nothing + } } return null; } diff --git a/appc-common/src/main/java/org/openecomp/appc/configuration/DefaultConfiguration.java b/appc-common/src/main/java/org/openecomp/appc/configuration/DefaultConfiguration.java index 96d9be86c..635384952 100644 --- a/appc-common/src/main/java/org/openecomp/appc/configuration/DefaultConfiguration.java +++ b/appc-common/src/main/java/org/openecomp/appc/configuration/DefaultConfiguration.java @@ -24,7 +24,6 @@ package org.openecomp.appc.configuration; - import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -64,7 +63,7 @@ public final class DefaultConfiguration implements Configuration, Cloneable { /** * Construct the configuration object. */ - public DefaultConfiguration() { + DefaultConfiguration() { // do nothing } @@ -97,22 +96,20 @@ public final class DefaultConfiguration implements Configuration, Cloneable { */ @SuppressWarnings("nls") private static String decrypt(String value) { - if (value != null) { - if (value.startsWith(EncryptionTool.ENCRYPTED_VALUE_PREFIX)) { - try { - return EncryptionTool.getInstance().decrypt(value); - } catch (Exception e) { - StringBuilder out = new StringBuilder(); - for (Provider p : Security.getProviders()) { - for (Service s : p.getServices()) { - String algo = s.getAlgorithm(); - out.append(String.format("\n==Found Algorithm [ %s ] in provider [ %s ] and service [ %s ]", - algo, p.getName(), s.getClassName())); - } + if (value != null && value.startsWith(EncryptionTool.ENCRYPTED_VALUE_PREFIX)) { + try { + return EncryptionTool.getInstance().decrypt(value); + } catch (Exception e) { + StringBuilder out = new StringBuilder(); + for (Provider p : Security.getProviders()) { + for (Service s : p.getServices()) { + String algo = s.getAlgorithm(); + out.append(String.format("\n==Found Algorithm [ %s ] in provider [ %s ] and service [ %s ]", + algo, p.getName(), s.getClassName())); } - logger.debug(out.toString()); - logger.warn(String.format("Could not decrypt the configuration value [%s]", value), e); } + logger.debug(out.toString()); + logger.warn(String.format("Could not decrypt the configuration value [%s]", value), e); } } return value; @@ -172,9 +169,9 @@ public final class DefaultConfiguration implements Configuration, Cloneable { // template = decrypt(template); DH: Do not assign values to parameters, bad form! Also, Sonar complains // bitterly - StringBuffer buffer = new StringBuffer(decrypt(template)); + StringBuilder builder = new StringBuilder(decrypt(template)); Pattern pattern = Pattern.compile("\\$\\{([^\\}]+)\\}"); - Matcher matcher = pattern.matcher(buffer); + Matcher matcher = pattern.matcher(builder); while (matcher.find()) { String variable = matcher.group(1); String value = properties.getProperty(variable); @@ -184,11 +181,11 @@ public final class DefaultConfiguration implements Configuration, Cloneable { if (value == null) { value = ""; } - buffer.replace(matcher.start(), matcher.end(), value); + builder.replace(matcher.start(), matcher.end(), value); matcher.reset(); } - return buffer.toString().trim(); + return builder.toString().trim(); } /** @@ -379,7 +376,7 @@ public final class DefaultConfiguration implements Configuration, Cloneable { */ @Override public int hashCode() { - return (properties == null ? 0 : properties.hashCode()); + return properties == null ? 0 : properties.hashCode(); } /** @@ -487,7 +484,7 @@ public final class DefaultConfiguration implements Configuration, Cloneable { try { properties.load(is); } catch (IOException e) { - e.printStackTrace(); + logger.warn("setProperties with inputStream got exception", e); } } @@ -524,8 +521,8 @@ public final class DefaultConfiguration implements Configuration, Cloneable { @SuppressWarnings("nls") @Override public String toString() { - return String.format("Configuration: %d properties, keys:[%s]", properties.size(), properties.keySet() - .toString()); + return String.format("Configuration: %d properties, keys:[%s]", + properties.size(), properties.keySet().toString()); } /** diff --git a/appc-common/src/main/java/org/openecomp/appc/exceptions/APPCException.java b/appc-common/src/main/java/org/openecomp/appc/exceptions/APPCException.java index 808434d62..314d0a601 100644 --- a/appc-common/src/main/java/org/openecomp/appc/exceptions/APPCException.java +++ b/appc-common/src/main/java/org/openecomp/appc/exceptions/APPCException.java @@ -22,8 +22,6 @@ * ============LICENSE_END========================================================= */ - - package org.openecomp.appc.exceptions; /** diff --git a/appc-common/src/main/java/org/openecomp/appc/exceptions/UnknownProviderException.java b/appc-common/src/main/java/org/openecomp/appc/exceptions/UnknownProviderException.java index 1551f48d1..f3eb8a486 100644 --- a/appc-common/src/main/java/org/openecomp/appc/exceptions/UnknownProviderException.java +++ b/appc-common/src/main/java/org/openecomp/appc/exceptions/UnknownProviderException.java @@ -22,8 +22,6 @@ * ============LICENSE_END========================================================= */ - - package org.openecomp.appc.exceptions; /** @@ -100,7 +98,10 @@ public class UnknownProviderException extends APPCException { * @param writableStackTrace * whether or not the stack trace should be writable */ - public UnknownProviderException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + public UnknownProviderException(String message, + Throwable cause, + boolean enableSuppression, + boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } diff --git a/appc-common/src/main/java/org/openecomp/appc/logging/LoggingConstants.java b/appc-common/src/main/java/org/openecomp/appc/logging/LoggingConstants.java index 9d6a32fe9..5c6d7b2b7 100644 --- a/appc-common/src/main/java/org/openecomp/appc/logging/LoggingConstants.java +++ b/appc-common/src/main/java/org/openecomp/appc/logging/LoggingConstants.java @@ -24,9 +24,21 @@ package org.openecomp.appc.logging; +/** + * Constant definition of logging + */ public class LoggingConstants { + private LoggingConstants() { + throw new IllegalAccessError("LoggingConstants"); + } + /** + * Constants of MDC property keys + */ public static class MDCKeys { + private MDCKeys() { + throw new IllegalAccessError("MDCKeys"); + } public static final String ERROR_CODE = "ErrorCode"; public static final String ERROR_DESCRIPTION = "ErrorDescription"; @@ -44,12 +56,24 @@ public class LoggingConstants { public static final String TARGET_VIRTUAL_ENTITY = "TargetVirtualEntity"; } + /** + * Constants of status code values + */ public static class StatusCodes { + private StatusCodes() { + throw new IllegalAccessError("StatusCodes"); + } public static final String COMPLETE = "COMPLETE"; public static final String ERROR = "ERROR"; } + /** + * Constants of APPC target names + */ public static class TargetNames { + private TargetNames() { + throw new IllegalAccessError("TargetNames"); + } public static final String APPC = "APPC"; public static final String AAI = "A&AI"; public static final String DB = "DataBase"; @@ -62,9 +86,21 @@ public class LoggingConstants { public static final String REQUEST_HANDLER = "RequestHandler"; } - public static class TargetServiceNames{ + /** + * Constants of targeted service names + */ + public static class TargetServiceNames { + private TargetServiceNames() { + throw new IllegalAccessError("TargetServiceNames"); + } - public static class AAIServiceNames{ + /** + * Constants of AAI service names + */ + public static class AAIServiceNames { + private AAIServiceNames() { + throw new IllegalAccessError("AAIServiceNames"); + } public static final String QUERY = "query"; public static final String GET_VNF_DATA = "getVnfData"; } diff --git a/appc-common/src/main/java/org/openecomp/appc/logging/LoggingUtils.java b/appc-common/src/main/java/org/openecomp/appc/logging/LoggingUtils.java index 70c7fb1d2..65ea7893f 100644 --- a/appc-common/src/main/java/org/openecomp/appc/logging/LoggingUtils.java +++ b/appc-common/src/main/java/org/openecomp/appc/logging/LoggingUtils.java @@ -41,33 +41,43 @@ import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.TimeZone; - - +/** + * Logging utilities + */ public class LoggingUtils { private final static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger(); private final static EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); private final static EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger(); - public static void logErrorMessage(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String additionalMessage, String className) { + private LoggingUtils() { + throw new IllegalAccessError("LoggingUtils"); + } + + public static void logErrorMessage(String errorCode, String errorDescription, String targetEntity, + String targetServiceName, String additionalMessage, String className) { logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage, className); } - public static void logErrorMessage(String targetEntity, String targetServiceName, String additionalMessage, String className) { + public static void logErrorMessage(String targetEntity, String targetServiceName, String additionalMessage, + String className) { logError("", "", targetEntity, targetServiceName, additionalMessage, className); } public static void logErrorMessage(String targetServiceName, String additionalMessage, String className) { - logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, additionalMessage, className); + logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, + additionalMessage, className); } - private static void logError(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String additionalMessage, String className) { + private static void logError(String errorCode, String errorDescription, String targetEntity, + String targetServiceName, String additionalMessage, String className) { populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName, className); errorLogger.error(additionalMessage == null ? "" : additionalMessage); cleanErrorLogContext(); } - public static void logAuditMessage(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className) { + public static void logAuditMessage(Instant beginTimeStamp, Instant endTimeStamp, String code, + String responseDescription, String className) { populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className); auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_MSG, MDC.get(MDC_SERVICE_NAME), @@ -80,13 +90,15 @@ public class LoggingUtils { cleanAuditErrorContext(); } - public static void auditInfo(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className,EELFResolvableErrorEnum resourceId, String... arguments) { + public static void auditInfo(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, + String className,EELFResolvableErrorEnum resourceId, String... arguments) { populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className); auditLogger.info(resourceId,arguments); cleanAuditErrorContext(); } - public static void auditWarn(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className,EELFResolvableErrorEnum resourceId, String... arguments) { + public static void auditWarn(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, + String className,EELFResolvableErrorEnum resourceId, String... arguments) { populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className); auditLogger.warn(resourceId,arguments); cleanAuditErrorContext(); @@ -94,8 +106,11 @@ public class LoggingUtils { - public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) { - populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName, statusCode, responseCode, responseDescription, className); + public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, + String targetServiceName, String statusCode, String responseCode, + String responseDescription, String className) { + populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName, statusCode, + responseCode, responseDescription, className); metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG, MDC.get(MDC_SERVICE_NAME), MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY), @@ -108,10 +123,11 @@ public class LoggingUtils { cleanMetricContext(); } - private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className) { + private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp, String code, + String responseDescription, String className) { populateTimeContext(beginTimeStamp, endTimeStamp); MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code); - MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, code.equals("100") || code.equals("400") ? + MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, "100".equals(code) || "400".equals(code) ? LoggingConstants.StatusCodes.COMPLETE : LoggingConstants.StatusCodes.ERROR); MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:""); @@ -126,7 +142,8 @@ public class LoggingUtils { MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME); } - private static void populateErrorLogContext(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String className) { + private static void populateErrorLogContext(String errorCode, String errorDescription, String targetEntity, + String targetServiceName, String className) { populateErrorContext(errorCode, errorDescription); populateTargetContext(targetEntity, targetServiceName!=null?targetServiceName:""); MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:""); @@ -138,7 +155,9 @@ public class LoggingUtils { MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME); } - private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) { + private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, + String targetServiceName, String statusCode, String responseCode, + String responseDescription, String className) { populateTimeContext(beginTimeStamp, endTimeStamp); populateTargetContext(targetEntity, targetServiceName); populateResponseContext(statusCode, responseCode, responseDescription); diff --git a/appc-common/src/main/java/org/openecomp/appc/metadata/objects/DependencyModelIdentifier.java b/appc-common/src/main/java/org/openecomp/appc/metadata/objects/DependencyModelIdentifier.java index f76823308..8b1b5870a 100644 --- a/appc-common/src/main/java/org/openecomp/appc/metadata/objects/DependencyModelIdentifier.java +++ b/appc-common/src/main/java/org/openecomp/appc/metadata/objects/DependencyModelIdentifier.java @@ -24,51 +24,67 @@ package org.openecomp.appc.metadata.objects; - +/** + * Object of identifier for dependency model. + * Currently uses VNF type and catalog version + */ public class DependencyModelIdentifier { + static final String TO_STRING_FORMAT = "DependencyModelIdentifier : vnfType = %s , catalogVersion = %s"; + static final int prime = 31; + private String vnfType; private String catalogVersion; - public DependencyModelIdentifier(String vnfType, String catalogVersion){ + /** + * Constructor + * @param vnfType String of the VNF type + * @param catalogVersion String of the catalog version + */ + public DependencyModelIdentifier(String vnfType, String catalogVersion) { this.vnfType = vnfType; this.catalogVersion = catalogVersion; } - public int hashCode(){ - final int prime = 31; + @Override + public int hashCode() { int result = 1; result = result * prime + (this.vnfType == null ? 0 :this.vnfType.hashCode()); result = result * prime + (this.catalogVersion == null ? 0 :this.catalogVersion.hashCode()); return result; } - public boolean equals(Object obj){ - if(obj ==null) + @Override + public boolean equals(Object obj) { + if (obj ==null) { return false; - if(!(obj instanceof DependencyModelIdentifier)) + } + if (!(obj instanceof DependencyModelIdentifier)) { return false; + } DependencyModelIdentifier modelIdentifier = (DependencyModelIdentifier)obj; - if(this.vnfType == null){ - if(modelIdentifier.vnfType !=null) + if (this.vnfType == null) { + if (modelIdentifier.vnfType !=null) { return false; - } - else if(!this.vnfType.equals(modelIdentifier.vnfType)) + } + } else if (!this.vnfType.equals(modelIdentifier.vnfType)) { return false; + } - if(this.catalogVersion == null){ - if(modelIdentifier.catalogVersion !=null) + if (this.catalogVersion == null) { + if (modelIdentifier.catalogVersion !=null) { return false; - } - else if(!this.catalogVersion.equals(modelIdentifier.catalogVersion)) + } + } else if (!this.catalogVersion.equals(modelIdentifier.catalogVersion)) { return false; + } return true; } @Override public String toString() { - return "DependencyModelIdentifier : vnfType = "+vnfType + " , catalogVersion = " +catalogVersion; + return String.format(TO_STRING_FORMAT, vnfType, catalogVersion); } public String getVnfType() { diff --git a/appc-common/src/test/java/org/openecomp/appc/cache/CacheStrategiesTest.java b/appc-common/src/test/java/org/openecomp/appc/cache/CacheStrategiesTest.java new file mode 100644 index 000000000..c9e0e0034 --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/cache/CacheStrategiesTest.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.cache; + +import org.junit.Assert; +import org.junit.Test; + +public class CacheStrategiesTest { + private CacheStrategies cacheStrategies = CacheStrategies.LRU; + + @Test + public void testName() throws Exception { + Assert.assertEquals("Should have name LRU", "LRU", cacheStrategies.name()); + } + + @Test + public void testToString() throws Exception { + Assert.assertEquals("Should return String LRU", "LRU", cacheStrategies.toString()); + } + + @Test + public void testEquals() throws Exception { + Assert.assertTrue(cacheStrategies.equals(CacheStrategies.LRU)); + Assert.assertFalse(cacheStrategies.equals(null)); + } +} diff --git a/appc-common/src/test/java/org/openecomp/appc/cache/impl/LRUCacheTest.java b/appc-common/src/test/java/org/openecomp/appc/cache/impl/LRUCacheTest.java new file mode 100644 index 000000000..75be94dac --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/cache/impl/LRUCacheTest.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.cache.impl; + +import org.junit.Assert; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +import java.util.Map; + +public class LRUCacheTest { + + @Test + public void testConstructor() throws Exception { + LRUCache cache = new LRUCache(20); + Map internalMap = Whitebox.getInternalState(cache, "map"); + Assert.assertTrue(internalMap != null); + Assert.assertTrue(internalMap.size() == 0); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetAndPutObject() throws Exception { + LRUCache cache = new LRUCache(20); + + String key = "testing key"; + Assert.assertTrue(cache.getObject(key) == null); + + String value = "testing value"; + cache.putObject(key, value); + Map internalMap = Whitebox.getInternalState(cache, "map"); + Assert.assertTrue(internalMap.containsKey(key)); + Assert.assertTrue(internalMap.containsValue(value)); + Assert.assertTrue(internalMap.size() == 1); + + Assert.assertEquals(value, cache.getObject(key)); + } + +} diff --git a/appc-common/src/test/java/org/openecomp/appc/cache/impl/MetadataCacheFactoryTest.java b/appc-common/src/test/java/org/openecomp/appc/cache/impl/MetadataCacheFactoryTest.java new file mode 100644 index 000000000..db6658686 --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/cache/impl/MetadataCacheFactoryTest.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.cache.impl; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openecomp.appc.cache.CacheStrategies; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import static org.mockito.Mockito.mock; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MetadataCacheFactory.class, MetadataCacheImpl.class}) +public class MetadataCacheFactoryTest { + @Test + public void testConstructor() throws Exception { + Whitebox.invokeConstructor(MetadataCacheFactory.class); + } + + @Test + public void testGetInstance() throws Exception { + Assert.assertTrue("Should not return null", MetadataCacheFactory.getInstance() != null); + Assert.assertEquals("Should always return the same object", + MetadataCacheFactory.getInstance(), MetadataCacheFactory.getInstance()); + } + + @Test + public void testGetMetadataCacheWithNoArgument() throws Exception { + MetadataCacheImpl mockImpl = mock(MetadataCacheImpl.class); + PowerMockito.whenNew(MetadataCacheImpl.class).withNoArguments().thenReturn(mockImpl); + Assert.assertEquals(mockImpl, MetadataCacheFactory.getInstance().getMetadataCache()); + } + + @Test + public void testGetMetadataCacheWithArgument() throws Exception { + CacheStrategies cacheStrategies = CacheStrategies.LRU; + MetadataCacheImpl mockImpl = mock(MetadataCacheImpl.class); + PowerMockito.whenNew(MetadataCacheImpl.class).withArguments(cacheStrategies).thenReturn(mockImpl); + Assert.assertEquals(mockImpl, MetadataCacheFactory.getInstance().getMetadataCache(cacheStrategies)); + } + +} diff --git a/appc-common/src/test/java/org/openecomp/appc/cache/impl/MetadataCacheImplTest.java b/appc-common/src/test/java/org/openecomp/appc/cache/impl/MetadataCacheImplTest.java new file mode 100644 index 000000000..48cc67921 --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/cache/impl/MetadataCacheImplTest.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.cache.impl; + +import org.junit.Assert; +import org.junit.Test; +import org.openecomp.appc.cache.CacheStrategies; +import org.powermock.reflect.Whitebox; + +import static org.mockito.Mockito.spy; + +public class MetadataCacheImplTest { + @Test + public void testConstructor() throws Exception { + // test without parameter + MetadataCacheImpl impl = new MetadataCacheImpl<>(); + Assert.assertTrue("Should have initialized strategy", + Whitebox.getInternalState(impl, "strategy") != null); + + // test with parameter + impl = new MetadataCacheImpl<>(CacheStrategies.LRU); + Assert.assertTrue("Should have initialized strategy", + Whitebox.getInternalState(impl, "strategy") != null); + + impl = new MetadataCacheImpl<>(null); + Assert.assertTrue("Should not initialized strategy", + Whitebox.getInternalState(impl, "strategy") == null); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetAndPutObject() throws Exception { + MetadataCacheImpl impl = spy(new MetadataCacheImpl<>()); + + String key = "testing key"; + Assert.assertTrue(impl.getObject(key) == null); + + String value = "testing value"; + impl.putObject(key, value); + Assert.assertEquals(value, impl.getObject(key)); + } +} diff --git a/appc-common/src/test/java/org/openecomp/appc/configuration/DefaultConfigurationTest.java b/appc-common/src/test/java/org/openecomp/appc/configuration/DefaultConfigurationTest.java new file mode 100644 index 000000000..53bd9dbd6 --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/configuration/DefaultConfigurationTest.java @@ -0,0 +1,337 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.configuration; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.powermock.reflect.Whitebox; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import static org.mockito.Mockito.mock; + +public class DefaultConfigurationTest { + private static final String propKey1 = "testKey1"; + private static final String propKey2 = "testKey2"; + private static final String propValue1 = "testValue1"; + private static final String propValue2 = "testValue2"; + + private Properties prop = new Properties(); + private DefaultConfiguration defaultConfiguration; + + @Before + public void setUp() throws Exception { + prop.setProperty(propKey1, propValue1); + prop.setProperty(propKey2, propValue2); + + defaultConfiguration = new DefaultConfiguration(); + } + + @Test + public void testClear() throws Exception { + Whitebox.setInternalState(defaultConfiguration, "properties", prop); + defaultConfiguration.clear(); + Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties"); + Assert.assertTrue("internal properties should be cleared", internalProp.isEmpty()); + } + + @Test + public void testClone() throws Exception { + Object clonedObject = defaultConfiguration.clone(); + Assert.assertTrue("Should be DefaultConfiguration", clonedObject instanceof DefaultConfiguration); + Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties"); + Properties clonedInternalProp = Whitebox.getInternalState(clonedObject, "properties"); + Assert.assertEquals(internalProp, clonedInternalProp); + } + + @Test + public void testEquals() throws Exception { + // test compare with null + Assert.assertFalse(defaultConfiguration.equals(null)); + // test with non-DefaultConfiguration object + Assert.assertFalse(defaultConfiguration.equals("abc")); + + // test with not match DefaultConfiguration object + defaultConfiguration.setProperties(prop); + DefaultConfiguration newConfig = new DefaultConfiguration(); + Assert.assertFalse(defaultConfiguration.equals(newConfig)); + + // test with matching DefaultConfiguration object + newConfig.setProperties(prop); + Assert.assertTrue(defaultConfiguration.equals(newConfig)); + } + + @Test + public void testSetPropAndGetBooleanProperty() throws Exception { + String booleanKey = "booleanKey"; + // test default value + Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey)); + // test match value true + defaultConfiguration.setProperty(booleanKey, "true"); + Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey)); + defaultConfiguration.setProperty(booleanKey, "True"); + Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey)); + defaultConfiguration.setProperty(booleanKey, "TrUe"); + Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey)); + // test not matching true values + defaultConfiguration.setProperty(booleanKey, "false"); + Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey)); + defaultConfiguration.setProperty(booleanKey, "abc"); + Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey)); + } + + @Test + public void testSetPropAndGetBooleanPropertyWithDefaultValue() throws Exception { + String booleanKey = "booleanKey"; + // test default value + Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, false)); + Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, true)); + // test match value true + defaultConfiguration.setProperty(booleanKey, "true"); + Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false)); + defaultConfiguration.setProperty(booleanKey, "True"); + Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false)); + defaultConfiguration.setProperty(booleanKey, "TrUe"); + Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false)); + // test not matching true values + defaultConfiguration.setProperty(booleanKey, "false"); + Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true)); + defaultConfiguration.setProperty(booleanKey, "abc"); + Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true)); + } + + @Test + public void testSetPropAndGetDoubleProperty() throws Exception { + String doubleKey = "doubleKey"; + // test default value + Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey)); + // test NumberFormatException + defaultConfiguration.setProperty(doubleKey, "abc"); + Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey)); + // test normal + defaultConfiguration.setProperty(doubleKey, "1.1"); + Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey)); + } + + @Test + public void testSetPropAndGetDoublePropertyWithDefaultValue() throws Exception { + String doubleKey = "doubleKey"; + // test default value + Assert.assertTrue(2.2 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2)); + // test NumberFormatException + defaultConfiguration.setProperty(doubleKey, "abc"); + Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2)); + // test normal + defaultConfiguration.setProperty(doubleKey, "1.1"); + Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2)); + } + + @Test + public void testSetPropAndGetIntegerProperty() throws Exception { + String integerKey = "integerKey"; + // test default value + Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey)); + // test NumberFormatException + defaultConfiguration.setProperty(integerKey, "abc"); + Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey)); + // test normal + defaultConfiguration.setProperty(integerKey, "100"); + Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey)); + } + + @Test + public void testSetPropAndGetIntegerPropertyWithDefaultValue() throws Exception { + String integerKey = "integerKey"; + // test default value + Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 100)); + // test NumberFormatException + defaultConfiguration.setProperty(integerKey, "abc"); + Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey, 100)); + // test normal + defaultConfiguration.setProperty(integerKey, "100"); + Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10)); + } + + @Test + public void testSetPropAndGetLongProperty() throws Exception { + String longKey = "longKey"; + // test default value + Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey)); + // test NumberFormatException + defaultConfiguration.setProperty(longKey, "abc"); + Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey)); + // test normal + defaultConfiguration.setProperty(longKey, "100"); + Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey)); + } + + @Test + public void testSetPropAndGetLongPropertyWithDefaultVaue() throws Exception { + String longKey = "longKey"; + // test default value + Assert.assertTrue(10 == defaultConfiguration.getLongProperty(longKey, 10)); + // test NumberFormatException + defaultConfiguration.setProperty(longKey, "abc"); + Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey, 10)); + // test normal + defaultConfiguration.setProperty(longKey, "100"); + Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10)); + } + + @Test + public void testSetAndGetProperties() throws Exception { + Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties"); + Assert.assertEquals(internalProp, defaultConfiguration.getProperties()); + + defaultConfiguration.setProperties(prop); + internalProp = Whitebox.getInternalState(defaultConfiguration, "properties"); + Assert.assertEquals(internalProp, defaultConfiguration.getProperties()); + } + + @Test + public void testSetAndGetProperty() throws Exception { + String key = "key"; + // test default value + Assert.assertTrue(null == defaultConfiguration.getProperty(key)); + // test normal + defaultConfiguration.setProperty(key, "abc"); + Assert.assertEquals("abc", defaultConfiguration.getProperty(key)); + } + + @Test + public void testSetPropAndGetPropertyWithDefaultValue() throws Exception { + String key = "key"; + // test default value + Assert.assertTrue(null == defaultConfiguration.getProperty(key, null)); + Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc")); + // test normal + defaultConfiguration.setProperty(key, "abc"); + Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd")); + } + + @Test + public void testHashCode() throws Exception { + Properties properties = null; + Whitebox.setInternalState(defaultConfiguration, "properties", properties); + Assert.assertEquals(0, defaultConfiguration.hashCode()); + + + Whitebox.setInternalState(defaultConfiguration, "properties", prop); + Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode()); + } + + @Test + public void testIsPropertyDefined() throws Exception { + String key = "key"; + // test not exist + Assert.assertFalse(defaultConfiguration.isPropertyDefined(key)); + // test exist + defaultConfiguration.setProperty(key, "abc"); + Assert.assertTrue(defaultConfiguration.isPropertyDefined(key)); + } + + @Test + public void testIsValidBoolean() throws Exception { + String key = "key"; + // test not exist + Assert.assertFalse(defaultConfiguration.isValidBoolean(key)); + // test exist with invalid + defaultConfiguration.setProperty(key, "abc"); + Assert.assertFalse(defaultConfiguration.isValidBoolean(key)); + // test exist with valid + defaultConfiguration.setProperty(key, "True"); + Assert.assertTrue(defaultConfiguration.isPropertyDefined(key)); + defaultConfiguration.setProperty(key, "FaLse"); + Assert.assertTrue(defaultConfiguration.isPropertyDefined(key)); + } + + @Test + public void testIsValidDouble() throws Exception { + String key = "key"; + // test not exist + Assert.assertFalse(defaultConfiguration.isValidDouble(key)); + // test exist with invalid + defaultConfiguration.setProperty(key, "abc"); + Assert.assertFalse(defaultConfiguration.isValidDouble(key)); + // test exist with valid + defaultConfiguration.setProperty(key, "2"); + Assert.assertTrue(defaultConfiguration.isValidDouble(key)); + defaultConfiguration.setProperty(key, "3.45"); + Assert.assertTrue(defaultConfiguration.isValidDouble(key)); + } + + @Test + public void testIsValidInteger() throws Exception { + String key = "key"; + // test not exist + Assert.assertFalse(defaultConfiguration.isValidInteger(key)); + // test exist with invalid + defaultConfiguration.setProperty(key, "abc"); + Assert.assertFalse(defaultConfiguration.isValidInteger(key)); + defaultConfiguration.setProperty(key, "3.45"); + Assert.assertFalse(defaultConfiguration.isValidInteger(key)); + // test exist with valid + defaultConfiguration.setProperty(key, "2"); + Assert.assertTrue(defaultConfiguration.isValidInteger(key)); + } + + @Test + public void testIsValidLong() throws Exception { + String key = "key"; + // test not exist + Assert.assertFalse(defaultConfiguration.isValidLong(key)); + // test exist with invalid + defaultConfiguration.setProperty(key, "abc"); + Assert.assertFalse(defaultConfiguration.isValidLong(key)); + defaultConfiguration.setProperty(key, "3.45"); + Assert.assertFalse(defaultConfiguration.isValidLong(key)); + // test exist with valid + defaultConfiguration.setProperty(key, "2"); + Assert.assertTrue(defaultConfiguration.isValidLong(key)); + } + + @Test + public void testSetPropertiesWithInputStream() throws Exception { + InputStream mockIS = mock(InputStream.class); + defaultConfiguration.setProperties(mockIS); + + Properties mockProp = mock(Properties.class); + Mockito.doThrow(new IOException("testing exception")).when(mockProp).load(mockIS); + Whitebox.setInternalState(defaultConfiguration, "properties", mockProp); + defaultConfiguration.setProperties(mockIS); + // Should come here without exception + } + + @Test + public void testToString() throws Exception { + Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties"); + Assert.assertEquals(String.format("Configuration: %d properties, keys:[%s]", + internalProp.size(), internalProp.keySet().toString()), defaultConfiguration.toString()); + } +} diff --git a/appc-common/src/test/java/org/openecomp/appc/exceptions/APPCExceptionTest.java b/appc-common/src/test/java/org/openecomp/appc/exceptions/APPCExceptionTest.java new file mode 100644 index 000000000..fc6aa9e27 --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/exceptions/APPCExceptionTest.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.exceptions; + +import org.junit.Assert; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + + +public class APPCExceptionTest { + + @Test + public void testConstructorNoArgument() throws Exception { + APPCException appcException = new APPCException(); + Assert.assertTrue(appcException.getCause() == null); + Assert.assertTrue(appcException.getLocalizedMessage() == null); + Assert.assertTrue(appcException.getMessage() == null); + } + + @Test + public void testConstructorWithMessaqge() throws Exception { + String message = "testing message"; + APPCException appcException = new APPCException(message); + Assert.assertTrue(appcException.getCause() == null); + Assert.assertEquals(message, appcException.getLocalizedMessage()); + Assert.assertEquals(message, appcException.getMessage()); + } + + @Test + public void testConstructorWithThrowable() throws Exception { + String message = "testing message"; + Throwable throwable = new Throwable(message); + APPCException appcException = new APPCException(throwable); + Assert.assertEquals(throwable, appcException.getCause()); + Assert.assertTrue(appcException.getLocalizedMessage().contains(message)); + Assert.assertTrue(appcException.getMessage().contains(message)); + } + + @Test + public void testConstructorWithMessageAndThrowable() throws Exception { + String message = "testing message"; + String tMessage = "throwable message"; + Throwable throwable = new Throwable(tMessage); + APPCException appcException = new APPCException(message, throwable); + Assert.assertEquals(throwable, appcException.getCause()); + Assert.assertTrue(appcException.getLocalizedMessage().contains(message)); + Assert.assertTrue(appcException.getMessage().contains(message)); + } + + @Test + public void testConstructorWithFourArguments() throws Exception { + String message = "testing message"; + String tMessage = "throwable message"; + Throwable throwable = new Throwable(tMessage); + APPCException appcException = new APPCException(message, throwable, true, true); + Assert.assertEquals(throwable, appcException.getCause()); + Assert.assertTrue(appcException.getLocalizedMessage().contains(message)); + Assert.assertTrue(appcException.getMessage().contains(message)); + + Assert.assertTrue(Whitebox.getInternalState(appcException, "stackTrace") != null); + Assert.assertTrue(Whitebox.getInternalState(appcException, "suppressedExceptions") != null); + + appcException = new APPCException(message, throwable, false, false); + Assert.assertTrue(Whitebox.getInternalState(appcException, "stackTrace") == null); + Assert.assertTrue(Whitebox.getInternalState(appcException, "suppressedExceptions") == null); + } +} diff --git a/appc-common/src/test/java/org/openecomp/appc/exceptions/InvalidInputExceptionTest.java b/appc-common/src/test/java/org/openecomp/appc/exceptions/InvalidInputExceptionTest.java new file mode 100644 index 000000000..5608a1221 --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/exceptions/InvalidInputExceptionTest.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.exceptions; + +import org.junit.Assert; +import org.junit.Test; + +public class InvalidInputExceptionTest { + @Test + public void testConstructor() throws Exception { + String message = "testing message"; + InvalidInputException invalidInputException = new InvalidInputException(message); + Assert.assertTrue(invalidInputException.getCause() == null); + Assert.assertEquals(message, invalidInputException.getLocalizedMessage()); + Assert.assertEquals(message, invalidInputException.getMessage()); + } +} diff --git a/appc-common/src/test/java/org/openecomp/appc/exceptions/InvalidStateExceptionTest.java b/appc-common/src/test/java/org/openecomp/appc/exceptions/InvalidStateExceptionTest.java new file mode 100644 index 000000000..a095a5187 --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/exceptions/InvalidStateExceptionTest.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.exceptions; + +import org.junit.Assert; +import org.junit.Test; + +public class InvalidStateExceptionTest { + @Test + public void testConstructor() throws Exception { + String message = "testing message"; + InvalidStateException invalidStateException = new InvalidStateException(message); + Assert.assertTrue(invalidStateException.getCause() == null); + Assert.assertEquals(message, invalidStateException.getLocalizedMessage()); + Assert.assertEquals(message, invalidStateException.getMessage()); + } +} diff --git a/appc-common/src/test/java/org/openecomp/appc/exceptions/UnknownProviderExceptionTest.java b/appc-common/src/test/java/org/openecomp/appc/exceptions/UnknownProviderExceptionTest.java new file mode 100644 index 000000000..d3fc15d3b --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/exceptions/UnknownProviderExceptionTest.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.exceptions; + +import org.junit.Assert; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +public class UnknownProviderExceptionTest { + + @Test + public void testConstructorNoArgument() throws Exception { + UnknownProviderException unknownProviderException = new UnknownProviderException(); + Assert.assertTrue(unknownProviderException.getCause() == null); + Assert.assertTrue(unknownProviderException.getLocalizedMessage() == null); + Assert.assertTrue(unknownProviderException.getMessage() == null); + } + + @Test + public void testConstructorWithMessaqge() throws Exception { + String message = "testing message"; + UnknownProviderException unknownProviderException = new UnknownProviderException(message); + Assert.assertTrue(unknownProviderException.getCause() == null); + Assert.assertEquals(message, unknownProviderException.getLocalizedMessage()); + Assert.assertEquals(message, unknownProviderException.getMessage()); + } + + @Test + public void testConstructorWithThrowable() throws Exception { + String message = "testing message"; + Throwable throwable = new Throwable(message); + UnknownProviderException unknownProviderException = new UnknownProviderException(throwable); + Assert.assertEquals(throwable, unknownProviderException.getCause()); + Assert.assertTrue(unknownProviderException.getLocalizedMessage().contains(message)); + Assert.assertTrue(unknownProviderException.getMessage().contains(message)); + } + + @Test + public void testConstructorWithMessageAndThrowable() throws Exception { + String message = "testing message"; + String tMessage = "throwable message"; + Throwable throwable = new Throwable(tMessage); + UnknownProviderException unknownProviderException = new UnknownProviderException(message, throwable); + Assert.assertEquals(throwable, unknownProviderException.getCause()); + Assert.assertTrue(unknownProviderException.getLocalizedMessage().contains(message)); + Assert.assertTrue(unknownProviderException.getMessage().contains(message)); + } + + @Test + public void testConstructorWithFourArguements() throws Exception { + String message = "testing message"; + String tMessage = "throwable message"; + Throwable throwable = new Throwable(tMessage); + UnknownProviderException unknownProviderException = + new UnknownProviderException(message, throwable, true, true); + Assert.assertEquals(throwable, unknownProviderException.getCause()); + Assert.assertTrue(unknownProviderException.getLocalizedMessage().contains(message)); + Assert.assertTrue(unknownProviderException.getMessage().contains(message)); + + Assert.assertTrue(Whitebox.getInternalState(unknownProviderException, "stackTrace") != null); + Assert.assertTrue(Whitebox.getInternalState(unknownProviderException, + "suppressedExceptions") != null); + + unknownProviderException = new UnknownProviderException(message, throwable, false, false); + Assert.assertTrue(Whitebox.getInternalState(unknownProviderException, + "stackTrace") == null); + Assert.assertTrue(Whitebox.getInternalState(unknownProviderException, + "suppressedExceptions") == null); + } + +} diff --git a/appc-common/src/test/java/org/openecomp/appc/i18n/MsgTest.java b/appc-common/src/test/java/org/openecomp/appc/i18n/MsgTest.java index 24820b82a..aa30ae7e1 100644 --- a/appc-common/src/test/java/org/openecomp/appc/i18n/MsgTest.java +++ b/appc-common/src/test/java/org/openecomp/appc/i18n/MsgTest.java @@ -2,11 +2,18 @@ package org.openecomp.appc.i18n; import static org.junit.Assert.assertNotNull; +import org.junit.Assert; import org.junit.Test; - public class MsgTest { + @Test + public void testNameAndToString() throws Exception { + for (Msg msg : Msg.values()) { + Assert.assertEquals(msg.name(), msg.toString()); + } + } + @Test public void testCONFIGURATION_STARTED() { assertNotNull(Msg.valueOf("CONFIGURATION_STARTED")); @@ -760,5 +767,5 @@ public class MsgTest { @Test public void testOAM_OPERATION_INVALID_INPUT() { assertNotNull(Msg.valueOf("OAM_OPERATION_INVALID_INPUT")); - }; + } } diff --git a/appc-common/src/test/java/org/openecomp/appc/logging/LoggingConstantsTest.java b/appc-common/src/test/java/org/openecomp/appc/logging/LoggingConstantsTest.java new file mode 100644 index 000000000..547f180ca --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/logging/LoggingConstantsTest.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.logging; + +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +public class LoggingConstantsTest { + @Test (expected = IllegalAccessError.class) + public void testConstructor() throws Exception { + Whitebox.invokeConstructor(LoggingConstants.class); + } + + @Test (expected = IllegalAccessError.class) + public void testMdcKeysConstructor() throws Exception { + Whitebox.invokeConstructor(LoggingConstants.MDCKeys.class); + } + + @Test (expected = IllegalAccessError.class) + public void testStatusCodesConstructor() throws Exception { + Whitebox.invokeConstructor(LoggingConstants.StatusCodes.class); + } + + @Test (expected = IllegalAccessError.class) + public void testTargetNamesConstructor() throws Exception { + Whitebox.invokeConstructor(LoggingConstants.TargetNames.class); + } + + @Test (expected = IllegalAccessError.class) + public void testTargetServiceNamesConstructor() throws Exception { + Whitebox.invokeConstructor(LoggingConstants.TargetServiceNames.class); + } + + @Test (expected = IllegalAccessError.class) + public void testAAIServiceNamesConstructor() throws Exception { + Whitebox.invokeConstructor(LoggingConstants.TargetServiceNames.AAIServiceNames.class); + } +} \ No newline at end of file diff --git a/appc-common/src/test/java/org/openecomp/appc/metadata/objects/DependencyModelIdentifierTest.java b/appc-common/src/test/java/org/openecomp/appc/metadata/objects/DependencyModelIdentifierTest.java new file mode 100644 index 000000000..214943ec6 --- /dev/null +++ b/appc-common/src/test/java/org/openecomp/appc/metadata/objects/DependencyModelIdentifierTest.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.metadata.objects; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +import static org.openecomp.appc.metadata.objects.DependencyModelIdentifier.prime; + +public class DependencyModelIdentifierTest { + private static final String vnfType = "vnfType"; + private static final String vnfType2 = "vnfType2"; + private static final String cVersion = "catalogVersion"; + private DependencyModelIdentifier identifier; + private DependencyModelIdentifier identifier1; + private DependencyModelIdentifier identifier2; + private DependencyModelIdentifier identifier3; + + @Before + public void setUp() throws Exception { + identifier = new DependencyModelIdentifier(vnfType, cVersion); + identifier1 = new DependencyModelIdentifier(null, null); + identifier2 = new DependencyModelIdentifier(vnfType, null); + identifier3 = new DependencyModelIdentifier(null, cVersion); + } + + @Test + public void testConstructorAndGetterAndToString() throws Exception { + Assert.assertEquals(vnfType, Whitebox.getInternalState(identifier, "vnfType")); + Assert.assertEquals(cVersion, Whitebox.getInternalState(identifier, "catalogVersion")); + + Assert.assertEquals(vnfType, identifier.getVnfType()); + Assert.assertEquals(cVersion, identifier.getCatalogVersion()); + + Assert.assertEquals(String.format(DependencyModelIdentifier.TO_STRING_FORMAT, vnfType, cVersion), + identifier.toString()); + } + + @Test + public void testHashCode() throws Exception { + Assert.assertEquals((prime + vnfType.hashCode()) * prime + cVersion.hashCode(), identifier.hashCode()); + Assert.assertEquals(prime * prime, identifier1.hashCode()); + Assert.assertEquals((prime + vnfType.hashCode()) * prime , identifier2.hashCode()); + Assert.assertEquals(prime* prime + cVersion.hashCode(), identifier3.hashCode()); + } + + @Test + public void testEquals() throws Exception { + // other object is null + Assert.assertFalse(identifier.equals(null)); + // other object is wrong data type + Assert.assertFalse(identifier.equals("abc")); + + // my vnfType is null + Assert.assertFalse(identifier1.equals(identifier)); + // different vnfType + DependencyModelIdentifier identifier4 = new DependencyModelIdentifier(vnfType2, cVersion); + Assert.assertFalse(identifier.equals(identifier4)); + // same vnfType, my catalogVerson is null + Assert.assertFalse(identifier2.equals(identifier)); + // same vnfType and both catalogVersion are null + identifier4 = new DependencyModelIdentifier(vnfType, null); + Assert.assertTrue(identifier2.equals(identifier4)); + + Assert.assertFalse(identifier.equals(identifier1)); + Assert.assertFalse(identifier.equals(identifier2)); + Assert.assertFalse(identifier.equals(identifier3)); + + Assert.assertFalse(identifier2.equals(identifier1)); + Assert.assertFalse(identifier2.equals(identifier3)); + + + Assert.assertFalse(identifier3.equals(identifier)); + Assert.assertFalse(identifier3.equals(identifier1)); + Assert.assertFalse(identifier3.equals(identifier2)); + + identifier4 = new DependencyModelIdentifier(vnfType, cVersion); + Assert.assertTrue(identifier.equals(identifier4)); + } + +} -- cgit 1.2.3-korg