Fix optimization bug add coverage plus
[policy/xacml-pdp.git] / applications / optimization / src / test / java / org / onap / policy / xacml / pdp / application / optimization / OptimizationSubscriberRequestTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.optimization;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26
27 import com.att.research.xacml.api.Request;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.resources.TextFileUtils;
36 import org.onap.policy.models.decisions.concepts.DecisionRequest;
37
38 public class OptimizationSubscriberRequestTest {
39     private static StandardCoder gson = new StandardCoder();
40     private DecisionRequest request;
41
42     /**
43      * setupLoadDecision loads the decision request object for testing.
44      *
45      * @throws Exception Exception if unable to load
46      */
47     @Before
48     public void setupLoadDecision() throws Exception {
49         request = gson.decode(
50                 TextFileUtils
51                     .getTextFileAsString(
52                             "src/test/resources/decision.optimization.input.json"),
53                     DecisionRequest.class);
54
55         assertThat(request).isNotNull();
56     }
57
58     @Test
59     public void testDecisionRequest() throws Exception {
60         //
61         // Add context
62         //
63         Map<String, Object> context = new HashMap<>();
64         context.put("subscriberRole", "role1");
65         request.setContext(context);
66         //
67         // Check the return object
68         //
69         Request xacml = OptimizationSubscriberRequest.createInstance(request);
70
71         assertThat(xacml).isNotNull();
72         assertThat(xacml.getRequestAttributes()).hasSize(4);
73
74         List<String> roles = new ArrayList<>();
75         roles.add("role-A");
76         roles.add("role-B");
77         context.put("subscriberRole", "role1");
78         request.setContext(context);
79
80         xacml = OptimizationSubscriberRequest.createInstance(request);
81         assertThat(xacml).isNotNull();
82         assertThat(xacml.getRequestAttributes()).hasSize(4);
83     }
84 }