2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-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
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.pap.xacml.rest.policycontroller;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.ArgumentMatchers.eq;
29 import com.mockrunner.mock.web.MockHttpServletRequest;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.policy.rest.adapter.PolicyRestAdapter;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.PolicyVersion;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.converter.HttpMessageNotReadableException;
42 import org.springframework.mock.web.MockHttpServletResponse;
44 public class PolicyCreationTest {
46 public void testCreation() {
47 CommonClassDao dao = Mockito.mock(CommonClassDao.class);
48 PolicyVersion version = new PolicyVersion();
49 version.setPolicyName("policyname");
50 Mockito.when(dao.getEntityItem(eq(PolicyVersion.class), eq("policyName"), anyString())).thenReturn(version);
51 PolicyCreation.setCommonClassDao(dao);
52 assertEquals(dao, PolicyCreation.getCommonClassDao());
53 PolicyCreation creation = new PolicyCreation(dao);
54 assertEquals(dao, PolicyCreation.getCommonClassDao());
56 HttpServletRequest req = new MockHttpServletRequest();
57 Exception cause = new Exception("oops");
58 HttpMessageNotReadableException exception = new HttpMessageNotReadableException("oops", cause);
59 assertEquals(HttpStatus.BAD_REQUEST,
60 creation.messageNotReadableExceptionHandler(req, exception).getStatusCode());
62 HttpServletResponse response = new MockHttpServletResponse();
63 PolicyRestAdapter policyData = new PolicyRestAdapter();
64 policyData.setPolicyType("Config");
65 policyData.setConfigPolicyType("ClosedLoop_Fault");
66 policyData.setDomainDir("domain");
67 policyData.setPolicyName("policyname");
68 assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
70 version.setHigherVersion(1);
71 assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
73 policyData.setEditPolicy(true);
74 assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
76 policyData.setEditPolicy(false);
77 version.setHigherVersion(0);
78 assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
80 policyData.setEditPolicy(true);
81 assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
83 version.setHigherVersion(1);
84 policyData.setConfigPolicyType("Firewall Config");
85 assertThatThrownBy(() -> creation.savePolicy(policyData, response))
86 .isInstanceOf(IllegalArgumentException.class);
88 policyData.setConfigPolicyType("BRMS_Raw");
89 assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
90 policyData.setConfigPolicyType("BRMS_Param");
91 assertThatThrownBy(() -> creation.savePolicy(policyData, response))
92 .isInstanceOf(IllegalArgumentException.class);
93 policyData.setConfigPolicyType("Base");
94 Mockito.when(policyData.getRuleData()).thenReturn(new LinkedHashMap<>());
95 assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
96 policyData.setConfigPolicyType("ClosedLoop_PM");
97 assertThatThrownBy(() -> creation.savePolicy(policyData, response))
98 .isInstanceOf(IllegalArgumentException.class);
99 policyData.setConfigPolicyType("Micro Service");
100 assertThatThrownBy(() -> creation.savePolicy(policyData, response))
101 .isInstanceOf(IllegalArgumentException.class);
102 policyData.setConfigPolicyType("Optimization");
103 assertThatThrownBy(() -> creation.savePolicy(policyData, response))
104 .isInstanceOf(IllegalArgumentException.class);
106 policyData.setPolicyType("Action");
107 List<Object> choices = new ArrayList<Object>();
108 policyData.setRuleAlgorithmschoices(choices);
109 assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
111 policyData.setPolicyType("Decision");
112 List<Object> settings = new ArrayList<Object>();
113 policyData.setSettings(settings);
114 assertThatThrownBy(() -> creation.savePolicy(policyData, response))
115 .isInstanceOf(IllegalArgumentException.class);