* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
import com.att.research.xacml.api.Response;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runners.MethodSorters;
-import org.onap.policy.common.endpoints.parameters.RestServerParameters;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.resources.TextFileUtils;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
import org.onap.policy.models.decisions.concepts.DecisionResponse;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
private static XacmlApplicationServiceProvider service;
private static StandardCoder gson = new StandardCoder();
private static DecisionRequest baseRequest;
- private static RestServerParameters clientParams;
private static String[] listPolicyTypeFiles = {
"onap.policies.Optimization",
"onap.policies.optimization.Resource",
*/
@BeforeClass
public static void setUp() throws Exception {
- clientParams = mock(RestServerParameters.class);
- when(clientParams.getHost()).thenReturn("localhost");
- when(clientParams.getPort()).thenReturn(6969);
//
// Load Single Decision Request
//
// Tell it to initialize based on the properties file
// we just built for it.
//
- service.initialize(propertiesFile.toPath().getParent(), clientParams);
+ service.initialize(propertiesFile.toPath().getParent(), null);
}
/**
//
// Does it return the correct decisions
//
- assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
+ assertThat(service.actionDecisionsSupported()).hasSize(1);
assertThat(service.actionDecisionsSupported()).contains("optimize");
//
// Ensure it has the supported policy types and
// can support the correct policy types.
//
- assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
+ assertThat(service.canSupportPolicyType(new ToscaConceptIdentifier(
"onap.policies.optimization.resource.AffinityPolicy", "1.0.0"))).isTrue();
- assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
+ assertThat(service.canSupportPolicyType(new ToscaConceptIdentifier(
"onap.policies.optimization.service.SubscriberPolicy", "1.0.0"))).isTrue();
- assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
+ assertThat(service.canSupportPolicyType(new ToscaConceptIdentifier(
+ "onap.policies.optimization.service.CustomUseCase", "1.0.0"))).isTrue();
+ assertThat(service.canSupportPolicyType(new ToscaConceptIdentifier(
"onap.foobar", "1.0.0"))).isFalse();
}
LOGGER.info("Decision {}", decision.getKey());
assertThat(decision.getKey()).isNotNull();
- assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
+ assertThat(decision.getKey().getPolicies()).isEmpty();
+ //
+ // Optimization applications should not have this information returned. Except advice
+ // for subscriber details, which does get checked in the tests following.
+ //
+ assertThat(decision.getKey().getAdvice()).isNull();
+ assertThat(decision.getKey().getObligations()).isNull();
+ assertThat(decision.getKey().getAttributes()).isNull();
}
/**
//
List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/test-optimization-policies.yaml",
service);
- assertThat(loadedPolicies).isNotNull();
- assertThat(loadedPolicies).hasSize(14);
- //
- // Ask for a decision for available default policies
- //
- DecisionResponse response = makeDecision();
+ assertThat(loadedPolicies).isNotNull().hasSize(14);
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(2);
- //
- // Validate it
- //
- validateDecision(response, baseRequest);
+ validateDecisionCount(2);
}
/**
DecisionResponse response = makeDecision();
assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(1);
+ assertThat(response.getPolicies()).hasSize(1);
response.getPolicies().forEach((key, value) -> {
- assertThat(((Map<String, Object>) value).get("type"))
- .isEqualTo(("onap.policies.optimization.resource.HpaPolicy"));
+ assertThat(((Map<String, Object>) value)).containsEntry("type",
+ "onap.policies.optimization.resource.HpaPolicy");
});
//
// Validate it
//
// Add US to the geography list
//
- ((List<String>)baseRequest.getResource().get("geography")).add("US");
- //
- // Ask for a decision for default US Policy
- //
- DecisionResponse response = makeDecision();
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(2);
- //
- // Validate it
- //
- validateDecision(response, baseRequest);
+ ((List<String>) baseRequest.getResource().get("geography")).add("US");
+
+ validateDecisionCount(2);
}
/**
//
// Add vCPE to the service list
//
- ((List<String>)baseRequest.getResource().get("services")).add("vCPE");
- //
- // Ask for a decision for default US policy for vCPE service
- //
- DecisionResponse response = makeDecision();
+ ((List<String>) baseRequest.getResource().get("services")).add("vCPE");
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(3);
- //
- // Validate it
- //
- validateDecision(response, baseRequest);
+ validateDecisionCount(3);
}
/**
//
// Add vG to the resource list
//
- ((List<String>)baseRequest.getResource().get("resources")).add("vG");
- //
- // Ask for a decision for default US service vCPE resource vG policy
- //
- DecisionResponse response = makeDecision();
+ ((List<String>) baseRequest.getResource().get("resources")).add("vG");
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(6);
- //
- // Validate it
- //
- validateDecision(response, baseRequest);
+ validateDecisionCount(6);
}
/**
//
// Add gold as a scope
//
- ((List<String>)baseRequest.getContext().get("subscriberName")).add("subscriber_a");
- //
- // Ask for a decision for specific US vCPE vG gold
- //
- DecisionResponse response = makeDecision();
+ ((List<String>) baseRequest.getContext().get("subscriberName")).add("subscriber_a");
- assertThat(response).isNotNull();
- assertThat(response.getPolicies()).hasSize(6);
- assertThat(response.getAdvice()).hasSize(2);
- //
- // Validate it
- //
- validateDecision(response, baseRequest);
+ validateDecisionCount(6, 2);
}
/**
//
// Add platinum to the scope list: this is now gold OR platinum
//
- ((List<String>)baseRequest.getResource().get("scope")).remove("gold");
- ((List<String>)baseRequest.getContext().get("subscriberName")).add("subscriber_x");
- //
- // Ask for a decision for specific US vCPE vG (gold or platinum)
- //
- DecisionResponse response = makeDecision();
+ ((List<String>) baseRequest.getResource().get("scope")).remove("gold");
+ ((List<String>) baseRequest.getContext().get("subscriberName")).add("subscriber_x");
- assertThat(response).isNotNull();
- assertThat(response.getPolicies()).hasSize(8);
- assertThat(response.getAdvice()).hasSize(2);
- //
- // Validate it
- //
- validateDecision(response, baseRequest);
+ validateDecisionCount(8, 2);
}
/**
//
// Add gold as a scope
//
- ((List<String>)baseRequest.getResource().get("scope")).remove("gold");
- ((List<String>)baseRequest.getResource().get("scope")).remove("platinum");
- ((List<String>)baseRequest.getContext().get("subscriberName")).remove("subscriber_a");
- //
- // Ask for a decision for specific US vCPE vG gold
- //
- DecisionResponse response = makeDecision();
+ ((List<String>) baseRequest.getResource().get("scope")).remove("gold");
+ ((List<String>) baseRequest.getResource().get("scope")).remove("platinum");
+ ((List<String>) baseRequest.getContext().get("subscriberName")).remove("subscriber_a");
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(7);
- //
- // Validate it
- //
- validateDecision(response, baseRequest);
+ validateDecisionCount(7);
}
/**
//
List<String> policyTypes = Lists.newArrayList("onap.policies.optimization.resource.AffinityPolicy");
baseRequest.getResource().put("policy-type", policyTypes);
- //
- // Ask for a decision for default
- //
- DecisionResponse response = makeDecision();
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(1);
- //
- // Validate it
- //
- validateDecision(response, baseRequest);
+ validateDecisionCount(1);
}
/**
//
((List<String>) baseRequest.getResource().get("policy-type"))
.add("onap.policies.optimization.resource.HpaPolicy");
- //
- // Ask for a decision for default
- //
- DecisionResponse response = makeDecision();
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(2);
- //
- // Validate it
- //
- validateDecision(response, baseRequest);
+ validateDecisionCount(2);
}
@Test
//
// Serialize it into a class
//
- ToscaServiceTemplate serviceTemplate;
- try {
- serviceTemplate = yamlCoder.decode(policyYaml, ToscaServiceTemplate.class);
- } catch (CoderException e) {
- throw new XacmlApplicationException("Failed to decode policy from resource file", e);
- }
+ JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+ ToscaServiceTemplate serviceTemplate = yamlCoder.decode(policyYaml, ToscaServiceTemplate.class);
+ jtst.fromAuthorative(serviceTemplate);
//
// Make sure all the fields are setup properly
//
- JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
- jtst.fromAuthorative(serviceTemplate);
ToscaServiceTemplate completedJtst = jtst.toAuthorative();
//
// Get the policies
return decision.getKey();
}
+ private DecisionResponse validateDecisionCount(int expectedPolicyCount) {
+ //
+ // Ask for a decision for default
+ //
+ DecisionResponse response = makeDecision();
+
+ assertThat(response).isNotNull();
+ assertThat(response.getPolicies()).hasSize(expectedPolicyCount);
+ //
+ // Optimization applications should not have this information returned
+ //
+ assertThat(response.getObligations()).isNull();
+ assertThat(response.getAttributes()).isNull();
+ //
+ // Validate it
+ //
+ validateDecision(response, baseRequest);
+
+ return response;
+ }
+
+ private void validateDecisionCount(int expectedPolicyCount, int expectedAdviceCount) {
+ DecisionResponse response = validateDecisionCount(expectedPolicyCount);
+
+ assertThat(response.getAdvice()).hasSize(expectedAdviceCount);
+ }
+
@SuppressWarnings("unchecked")
private void validateDecision(DecisionResponse decision, DecisionRequest request) {
for (Entry<String, Object> entrySet : decision.getPolicies().entrySet()) {
LOGGER.info("Decision Returned Policy {}", entrySet.getKey());
assertThat(entrySet.getValue()).isInstanceOf(Map.class);
Map<String, Object> policyContents = (Map<String, Object>) entrySet.getValue();
- assertThat(policyContents.containsKey("properties")).isTrue();
+ assertThat(policyContents).containsKey("properties");
assertThat(policyContents.get("properties")).isInstanceOf(Map.class);
Map<String, Object> policyProperties = (Map<String, Object>) policyContents.get("properties");
@SuppressWarnings("unchecked")
private void cleanOutResources() {
- ((List<String>)baseRequest.getResource().get("scope")).clear();
- ((List<String>)baseRequest.getResource().get("services")).clear();
- ((List<String>)baseRequest.getResource().get("resources")).clear();
- ((List<String>)baseRequest.getResource().get("geography")).clear();
- if (((List<String>)baseRequest.getResource().get("policy-type")) != null) {
+ ((List<String>) baseRequest.getResource().get("scope")).clear();
+ ((List<String>) baseRequest.getResource().get("services")).clear();
+ ((List<String>) baseRequest.getResource().get("resources")).clear();
+ ((List<String>) baseRequest.getResource().get("geography")).clear();
+ if (((List<String>) baseRequest.getResource().get("policy-type")) != null) {
baseRequest.getResource().remove("policy-type");
}
}