X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fpolicycontroller%2FPolicyCreationTest.java;fp=ONAP-PAP-REST%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fpolicycontroller%2FPolicyCreationTest.java;h=08c1659f9fe40ef540608530d2476afe1bb4c7bb;hp=0000000000000000000000000000000000000000;hb=2f41a033ed0eae09aa1624d8287687de8a972a5d;hpb=9bad71c7150ea6f94b2a08b68f356b4ee96c3d66 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 choices = new ArrayList(); + policyData.setRuleAlgorithmschoices(choices); + assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException(); + + policyData.setPolicyType("Decision"); + List settings = new ArrayList(); + policyData.setSettings(settings); + assertThatThrownBy(() -> creation.savePolicy(policyData, response)) + .isInstanceOf(IllegalArgumentException.class); + } +}