JUnit additions for PAP-REST, checkstyle fixes
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / components / OptimizationConfigPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018-2019 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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.components;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27
28 import com.att.research.xacml.api.pap.PAPException;
29 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
30 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.onap.policy.rest.adapter.PolicyRestAdapter;
35
36 public class OptimizationConfigPolicyTest {
37     @Rule
38     public ExpectedException thrown = ExpectedException.none();
39
40     @Test
41     public void testConstructor1() {
42         thrown.expect(NullPointerException.class);
43         OptimizationConfigPolicy policy = new OptimizationConfigPolicy();
44         policy.getCorrectPolicyDataObject();
45         fail("Expected an exception");
46     }
47
48     @Test
49     public void testConstructor2() {
50         PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
51         OptimizationConfigPolicy policy = new OptimizationConfigPolicy(policyAdapter);
52         assertNull(policy.getCorrectPolicyDataObject());
53     }
54
55     @Test
56     public void testSave() throws PAPException {
57         PolicyType policyType = new PolicyType();
58         PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
59         policyAdapter.setPolicyID("id");
60         policyAdapter.setHighestVersion(1);
61         policyAdapter.setPolicyType("Config");
62         policyAdapter.setNewFileName("newfile");
63         policyAdapter.setData(policyType);
64         policyAdapter.setJsonBody("{\"key\":\"value\"}");
65         policyAdapter.setServiceType("svcType");
66         OptimizationConfigPolicy policy = new OptimizationConfigPolicy(policyAdapter);
67         assertThatThrownBy(() -> policy.savePolicies()).isInstanceOf(Exception.class);
68     }
69
70     @Test
71     public void testAdvice() {
72         PolicyType policyType = new PolicyType();
73         PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
74         policyAdapter.setPolicyID("id");
75         policyAdapter.setHighestVersion(1);
76         policyAdapter.setPolicyType("Config");
77         policyAdapter.setNewFileName("newfile");
78         policyAdapter.setData(policyType);
79         policyAdapter.setJsonBody("{\"key\":\"value\"}");
80         policyAdapter.setServiceType("svcType");
81
82         OptimizationConfigPolicy policy = new OptimizationConfigPolicy(policyAdapter);
83         assertThatThrownBy(() -> policy.savePolicies()).isInstanceOf(Exception.class);
84
85         AdviceExpressionsType expType = policy.getAdviceExpressions(1, "filename");
86         assertEquals(1, expType.getAdviceExpression().size());
87     }
88 }