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