1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*-
* ============LICENSE_START=======================================================
* ONAP-PAP-REST
* ================================================================================
* Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.pap.xacml.rest.policycontroller;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import com.mockrunner.mock.web.MockHttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.mockito.Mockito;
import org.onap.policy.rest.adapter.PolicyRestAdapter;
import org.onap.policy.rest.dao.CommonClassDao;
import org.onap.policy.rest.jpa.PolicyVersion;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.mock.web.MockHttpServletResponse;
public class PolicyCreationTest {
@Test
public void testCreation() {
CommonClassDao dao = Mockito.mock(CommonClassDao.class);
PolicyVersion version = new PolicyVersion();
version.setPolicyName("policyname");
Mockito.when(dao.getEntityItem(eq(PolicyVersion.class), eq("policyName"), anyString())).thenReturn(version);
PolicyCreation.setCommonClassDao(dao);
assertEquals(dao, PolicyCreation.getCommonClassDao());
PolicyCreation creation = new PolicyCreation(dao);
assertEquals(dao, PolicyCreation.getCommonClassDao());
HttpServletRequest req = new MockHttpServletRequest();
Exception cause = new Exception("oops");
HttpMessageNotReadableException exception = new HttpMessageNotReadableException("oops", cause);
assertEquals(HttpStatus.BAD_REQUEST,
creation.messageNotReadableExceptionHandler(req, exception).getStatusCode());
HttpServletResponse response = new MockHttpServletResponse();
PolicyRestAdapter policyData = new PolicyRestAdapter();
policyData.setPolicyType("Config");
policyData.setConfigPolicyType("ClosedLoop_Fault");
policyData.setDomainDir("domain");
policyData.setPolicyName("policyname");
assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
version.setHigherVersion(1);
assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
policyData.setEditPolicy(true);
assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
policyData.setEditPolicy(false);
version.setHigherVersion(0);
assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
policyData.setEditPolicy(true);
assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
version.setHigherVersion(1);
policyData.setConfigPolicyType("Firewall Config");
assertThatThrownBy(() -> creation.savePolicy(policyData, response))
.isInstanceOf(IllegalArgumentException.class);
policyData.setConfigPolicyType("BRMS_Raw");
assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
policyData.setConfigPolicyType("BRMS_Param");
assertThatThrownBy(() -> creation.savePolicy(policyData, response))
.isInstanceOf(IllegalArgumentException.class);
policyData.setConfigPolicyType("Base");
Mockito.when(policyData.getRuleData()).thenReturn(new LinkedHashMap<>());
assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
policyData.setConfigPolicyType("ClosedLoop_PM");
assertThatThrownBy(() -> creation.savePolicy(policyData, response))
.isInstanceOf(IllegalArgumentException.class);
policyData.setConfigPolicyType("Micro Service");
assertThatThrownBy(() -> creation.savePolicy(policyData, response))
.isInstanceOf(IllegalArgumentException.class);
policyData.setConfigPolicyType("Optimization");
assertThatThrownBy(() -> creation.savePolicy(policyData, response))
.isInstanceOf(IllegalArgumentException.class);
policyData.setPolicyType("Action");
List<Object> choices = new ArrayList<Object>();
policyData.setRuleAlgorithmschoices(choices);
assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException();
policyData.setPolicyType("Decision");
List<Object> settings = new ArrayList<Object>();
policyData.setSettings(settings);
assertThatThrownBy(() -> creation.savePolicy(policyData, response))
.isInstanceOf(IllegalArgumentException.class);
}
}
|