Few JUnit additions for PAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / components / BRMSPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.xacml.rest.components;
24
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import com.att.research.xacml.api.pap.PAPException;
32 import java.io.IOException;
33 import java.nio.charset.Charset;
34 import java.util.HashMap;
35 import java.util.Map;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.Mockito;
40 import org.onap.policy.rest.adapter.PolicyRestAdapter;
41 import org.onap.policy.rest.dao.CommonClassDao;
42
43 public class BRMSPolicyTest {
44     @Rule
45     public ExpectedException thrown = ExpectedException.none();
46
47     @Test
48     public void testConstructor1() {
49         CreateBRMSRuleTemplate template = new CreateBRMSRuleTemplate();
50         assertNotNull(template);
51     }
52
53     @Test
54     public void testConstructor2() {
55         CommonClassDao commonClassDao = null;
56         CreateBRMSRuleTemplate template = new CreateBRMSRuleTemplate(commonClassDao);
57         assertNotNull(template);
58     }
59
60     @Test
61     public void testReadFile() throws IOException {
62         String goodRule = "declare Params\nparam1 : int\nend\n";
63         String badRule = "declare Params\nparam1+ : int\nend\n";
64         assertEquals(CreateBRMSRuleTemplate.validateRuleParams(goodRule), true);
65         assertEquals(CreateBRMSRuleTemplate.validateRuleParams(badRule), false);
66     }
67
68     @Test
69     public void testAdd() {
70         CommonClassDao dao = Mockito.mock(CommonClassDao.class);
71         CreateBRMSRuleTemplate template = new CreateBRMSRuleTemplate(dao);
72         String rule = "package foo\n";
73         String ruleName = "testName";
74         String description = "testDesc";
75         String userID = "testID";
76         assertEquals(1, template.addRule(rule, ruleName, description, userID).size());
77     }
78
79     @Test
80     public void testCreateBrmsParamPolicyAdapter() throws PAPException {
81         Map<String, String> brmsParamBody = new HashMap<String, String>();
82         brmsParamBody.put("key", "value");
83
84         PolicyRestAdapter adapter = new PolicyRestAdapter();
85         adapter.setHighestVersion(1);
86         adapter.setPolicyType("Config");
87         adapter.setBrmsParamBody(brmsParamBody);
88         adapter.setNewFileName("policyName.1.xml");
89         Map<String, String> dynamicFieldConfigAttributes = new HashMap<String, String>();
90         dynamicFieldConfigAttributes.put("key", "value");
91         adapter.setDynamicFieldConfigAttributes(dynamicFieldConfigAttributes);
92         CreateBrmsParamPolicy policy = new CreateBrmsParamPolicy(adapter);
93         String ruleContents = "contents";
94
95         assertThatCode(() -> policy.saveConfigurations("name.xml", "rules")).doesNotThrowAnyException();
96         try {
97             policy.prepareToSave();
98             policy.savePolicies();
99         } catch (Exception ex) {
100             // Ignore
101         }
102
103         assertThatThrownBy(() -> policy.expandConfigBody(ruleContents, brmsParamBody))
104             .isInstanceOf(NullPointerException.class);
105         assertTrue(policy.validateConfigForm());
106         policy.getAdviceExpressions(1, "name.1.xml");
107         assertNotNull(policy.getCorrectPolicyDataObject());
108     }
109
110     @Test
111     public void testRead() {
112         Charset encoding = Charset.defaultCharset();
113         assertThatCode(() -> CreateBrmsParamPolicy.readFile("xacml.pap.properties", encoding))
114             .doesNotThrowAnyException();
115     }
116 }