2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.xacml.rest.components;
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;
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;
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;
43 public class BRMSPolicyTest {
45 public ExpectedException thrown = ExpectedException.none();
48 public void testConstructor1() {
49 CreateBRMSRuleTemplate template = new CreateBRMSRuleTemplate();
50 assertNotNull(template);
54 public void testConstructor2() {
55 CommonClassDao commonClassDao = null;
56 CreateBRMSRuleTemplate template = new CreateBRMSRuleTemplate(commonClassDao);
57 assertNotNull(template);
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);
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());
80 public void testCreateBrmsParamPolicyAdapter() throws PAPException {
81 Map<String, String> brmsParamBody = new HashMap<String, String>();
82 brmsParamBody.put("key", "value");
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";
95 assertThatCode(() -> policy.saveConfigurations("name.xml", "rules")).doesNotThrowAnyException();
97 policy.prepareToSave();
98 policy.savePolicies();
99 } catch (Exception ex) {
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());
111 public void testRead() {
112 Charset encoding = Charset.defaultCharset();
113 assertThatCode(() -> CreateBrmsParamPolicy.readFile("xacml.pap.properties", encoding))
114 .doesNotThrowAnyException();