diff options
author | 2019-11-25 14:56:57 -0500 | |
---|---|---|
committer | 2019-11-26 14:25:15 -0500 | |
commit | 2f41a033ed0eae09aa1624d8287687de8a972a5d (patch) | |
tree | db73c0bdfc0a9684907d99d2433f2f8f75fc2127 /ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java | |
parent | 9bad71c7150ea6f94b2a08b68f356b4ee96c3d66 (diff) |
Couple of JUnit additions for PAP-REST
Issue-ID: POLICY-2130
Change-Id: Id7252df4a244e579d599a5d2631c60ddb9ccfab3
Signed-off-by: bobbymander <bobby.mander@att.com>
Diffstat (limited to 'ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java')
-rw-r--r-- | ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java new file mode 100644 index 000000000..08c1659f9 --- /dev/null +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java @@ -0,0 +1,115 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PAP-REST + * ================================================================================ + * Copyright (C) 2019 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.Matchers.anyString; +import static org.mockito.Matchers.eq; + +import com.mockrunner.mock.web.MockHttpServletRequest; +import java.util.ArrayList; +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"); + 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); + } +} |