From 29e3cd1afbd3202a0c16ef6a4057662942ddefa2 Mon Sep 17 00:00:00 2001 From: sebdet Date: Thu, 13 Feb 2020 05:49:21 -0800 Subject: Add tests Add tests for https and aaf Issue-ID: CLAMP-624 Signed-off-by: sebdet Change-Id: Ia78ed8da7e54eaeaaed4fb87f483e0aff3a4a8c5 --- .../clamp/clds/client/PolicyEngineServices.java | 67 ++++++++++++++++++++-- .../policy/downloader/PolicyEngineController.java | 29 ++++------ 2 files changed, 73 insertions(+), 23 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java index e916afc1..2302cc89 100644 --- a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java +++ b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java @@ -26,6 +26,11 @@ package org.onap.clamp.clds.client; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.builder.ExchangeBuilder; @@ -38,6 +43,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; +import org.yaml.snakeyaml.Yaml; + + + /** * The class implements the communication with the Policy Engine to retrieve @@ -60,27 +69,52 @@ public class PolicyEngineServices { public static final String POLICY_RETRY_INTERVAL = "policy.retry.interval"; public static final String POLICY_RETRY_LIMIT = "policy.retry.limit"; + /** + * Default constructor. + * + * @param camelContext Camel context bean + * @param clampProperties ClampProperties bean + * @param policyModelsRepository policyModel repository bean + */ @Autowired - public PolicyEngineServices(CamelContext camelContext, ClampProperties refProp, + public PolicyEngineServices(CamelContext camelContext, ClampProperties clampProperties, PolicyModelsRepository policyModelsRepository) { this.camelContext = camelContext; this.policyModelsRepository = policyModelsRepository; - if (refProp.getStringValue(POLICY_RETRY_LIMIT) != null) { - retryLimit = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_LIMIT)); + if (clampProperties.getStringValue(POLICY_RETRY_LIMIT) != null) { + retryLimit = Integer.valueOf(clampProperties.getStringValue(POLICY_RETRY_LIMIT)); } - if (refProp.getStringValue(POLICY_RETRY_INTERVAL) != null) { - retryInterval = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_INTERVAL)); + if (clampProperties.getStringValue(POLICY_RETRY_INTERVAL) != null) { + retryInterval = Integer.valueOf(clampProperties.getStringValue(POLICY_RETRY_INTERVAL)); } } + /** + * This method query Policy engine and create a PolicyModel object with type and version. + * + * @param policyType The policyType id + * @param policyVersion The policy version of that type + * @return A PolicyModel created from policyEngine data + */ public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion) { return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion); } + /** + * This method query Policy engine and create a PolicyModel object with type and version. + * + * @param microService microservice object instance + * @return A PolicyModel created from policyEngine data + */ public PolicyModel createPolicyModelFromPolicyEngine(BlueprintMicroService microService) { return createPolicyModelFromPolicyEngine(microService.getModelType(), microService.getModelVersion()); } + /** + * Thie method creates an PolicyModel in Db if it does not exist. + * + * @param policyModel The policyModel to save + */ @Transactional(propagation = Propagation.REQUIRES_NEW) public void createPolicyInDbIfNeeded(PolicyModel policyModel) { if (!policyModelsRepository @@ -89,6 +123,29 @@ public class PolicyEngineServices { } } + /** + * This method synchronize the clamp database and the policy engine. + * So it creates the required PolicyModel. + */ + public void synchronizeAllPolicies() { + LinkedHashMap loadedYaml; + loadedYaml = new Yaml().load(downloadAllPolicies()); + if (loadedYaml == null || loadedYaml.isEmpty()) { + logger.warn("getAllPolicyType yaml returned by policy engine could not be decoded, as it's null or empty"); + return; + } + + List> policyTypesList = (List>) loadedYaml + .get("policy_types"); + policyTypesList.parallelStream().forEach(policyType -> { + Map.Entry policyTypeEntry = (Map.Entry) new ArrayList(policyType.entrySet()).get(0); + + createPolicyInDbIfNeeded( + createPolicyModelFromPolicyEngine(policyTypeEntry.getKey(), + ((String) ((LinkedHashMap) policyTypeEntry.getValue()).get("version")))); + }); + } + /** * This method can be used to download all policy types + data types defined in * policy engine. diff --git a/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java b/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java index f3eaf0c8..50be035b 100644 --- a/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java +++ b/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java @@ -26,6 +26,7 @@ package org.onap.clamp.policy.downloader; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import java.time.Instant; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -56,31 +57,23 @@ public class PolicyEngineController { private final PolicyEngineServices policyEngineServices; + private Instant lastInstantExecuted; + @Autowired public PolicyEngineController(PolicyEngineServices policyEngineService, - PolicyModelsRepository policyModelsRepository) { + PolicyModelsRepository policyModelsRepository) { this.policyEngineServices = policyEngineService; } @Scheduled(fixedRate = 120000) - public void synchronizeAllPolicies() { - LinkedHashMap loadedYaml; - loadedYaml = new Yaml().load(policyEngineServices.downloadAllPolicies()); - if (loadedYaml == null || loadedYaml.isEmpty()) { - logger.warn("getAllPolicyType yaml returned by policy engine could not be decoded, as it's null or empty"); - return; - } - - List> policyTypesList = (List>) loadedYaml - .get("policy_types"); - policyTypesList.parallelStream().forEach(policyType -> { - Entry policyTypeEntry = (Entry) new ArrayList(policyType.entrySet()).get(0); - - policyEngineServices.createPolicyInDbIfNeeded( - policyEngineServices.createPolicyModelFromPolicyEngine(policyTypeEntry.getKey(), - ((String) ((LinkedHashMap) policyTypeEntry.getValue()).get("version")))); + public synchronized void synchronizeAllPolicies() { + policyEngineServices.synchronizeAllPolicies(); + lastInstantExecuted = Instant.now(); + } - }); + public Instant getLastInstantExecuted() { + return lastInstantExecuted; } + } -- cgit 1.2.3-korg