2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.controlloop.actor.xacml;
 
  23 import static org.assertj.core.api.Assertions.assertThat;
 
  24 import static org.junit.Assert.assertEquals;
 
  25 import static org.junit.Assert.assertNotNull;
 
  26 import static org.junit.Assert.assertTrue;
 
  27 import static org.mockito.Mockito.mock;
 
  28 import static org.mockito.Mockito.when;
 
  31 import java.util.function.Consumer;
 
  32 import org.junit.AfterClass;
 
  33 import org.junit.Before;
 
  34 import org.junit.BeforeClass;
 
  35 import org.junit.Test;
 
  36 import org.mockito.Mock;
 
  37 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
 
  38 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
 
  39 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
 
  40 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
 
  41 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 
  42 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 
  43 import org.onap.policy.models.decisions.concepts.DecisionRequest;
 
  44 import org.onap.policy.models.decisions.concepts.DecisionResponse;
 
  46 public class ConfigureOperationTest extends BasicHttpOperation {
 
  49     private Consumer<OperationOutcome> started;
 
  51     private Consumer<OperationOutcome> completed;
 
  53     private DecisionConfig operConfig;
 
  54     private ConfigureOperation oper;
 
  57      * Starts the simulator.
 
  60     public static void setUpBeforeClass() throws Exception {
 
  61         org.onap.policy.simulators.Util.buildXacmlSim();
 
  63         BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("policy/pdpx/v1/")
 
  64                         .hostname("localhost").managed(true).port(org.onap.policy.simulators.Util.XACMLSIM_SERVER_PORT)
 
  66         HttpClientFactoryInstance.getClientFactory().build(clientParams);
 
  70     public static void tearDownAfterClass() {
 
  71         HttpClientFactoryInstance.getClientFactory().destroy();
 
  72         HttpServletServerFactoryInstance.getServerFactory().destroy();
 
  82         operConfig = mock(DecisionConfig.class);
 
  83         when(operConfig.makeRequest()).thenAnswer(args -> {
 
  84             DecisionRequest req = new DecisionRequest();
 
  85             req.setAction("guard");
 
  86             req.setOnapComponent("my-onap-component");
 
  87             req.setOnapInstance("my-onap-instance");
 
  88             req.setOnapName("my-onap-name");
 
  95         params = params.toBuilder().startCallback(started).completeCallback(completed).build();
 
  97         oper = new ConfigureOperation(params, config);
 
 101     public void testConstructor() {
 
 102         assertEquals(DEFAULT_ACTOR, oper.getActorName());
 
 103         assertEquals(DEFAULT_OPERATION, oper.getName());
 
 107      * Tests "success" case with simulator.
 
 110     public void testSuccess() throws Exception {
 
 111         DecisionParams opParams =
 
 112                         DecisionParams.builder().clientName(MY_CLIENT).path("decision").action("configure").build();
 
 113         config = new DecisionConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
 
 115         params = params.toBuilder().payload(Map.of("policy-id", "test-policy")).retry(0).timeoutSec(5)
 
 116                         .executor(blockingExecutor).build();
 
 117         oper = new ConfigureOperation(params, config);
 
 119         outcome = oper.start().get();
 
 120         assertEquals(OperationResult.SUCCESS, outcome.getResult());
 
 122         DecisionResponse response = outcome.getResponse();
 
 123         assertTrue(response instanceof DecisionResponse);
 
 124         assertNotNull(response.getPolicies());
 
 125         assertThat(response.getPolicies()).containsKey("test-policy");
 
 129      * Tests "failure" case with simulator.
 
 132     public void testFailure() throws Exception {
 
 133         DecisionParams opParams =
 
 134                         DecisionParams.builder().clientName(MY_CLIENT).path("decision").action("configure").build();
 
 135         config = new DecisionConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
 
 137         params = params.toBuilder().payload(Map.of("policy-id", "nonexistent")).retry(0).timeoutSec(5)
 
 138                         .executor(blockingExecutor).build();
 
 139         oper = new ConfigureOperation(params, config);
 
 141         outcome = oper.start().get();
 
 142         assertEquals(OperationResult.FAILURE, outcome.getResult());
 
 144         DecisionResponse response = outcome.getResponse();
 
 145         assertTrue(response instanceof DecisionResponse);
 
 146         assertNotNull(response.getPolicies());
 
 147         assertThat(response.getPolicies()).isEmpty();