diff options
author | 2019-11-18 12:36:28 +0000 | |
---|---|---|
committer | 2019-11-18 15:27:15 +0000 | |
commit | 779125e31adbcc59a9864843b523bd6ed2751cbb (patch) | |
tree | 55d42bf962d5a684efeaf0025d90e6adc2551ab5 /ONAP-REST/src/test | |
parent | a2ab61f0ad39970ad35c3e47ff8429f59850c469 (diff) |
Unit/SONAR/Checkstyle in ONAP-REST
Util package of ONAP-REST, with JUnit added and
SONAR/Checkstyle issues addressed. In cases where a class name change
caused an update in another package, the license header on files for
those knock on changes are not updated.
Issue-ID: POLICY-2131
Change-Id: Ic134408efe76b9838f5607a07f1735d12bd41032
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'ONAP-REST/src/test')
10 files changed, 494 insertions, 202 deletions
diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/XacmlRestTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/XacmlRestTest.java index 19800b42e..3b03bdcc8 100644 --- a/ONAP-REST/src/test/java/org/onap/policy/rest/XacmlRestTest.java +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/XacmlRestTest.java @@ -26,9 +26,6 @@ import static org.junit.Assert.fail; import com.att.research.xacml.util.XACMLProperties; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -40,7 +37,6 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import javax.servlet.ReadListener; import javax.servlet.ServletConfig; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; @@ -199,8 +195,8 @@ public class XacmlRestTest extends Mockito { } @Test - public void testConstructorIsPrivate() - throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + public void testConstructorIsPrivate() throws NoSuchMethodException, IllegalAccessException, + InvocationTargetException, InstantiationException { Constructor<XacmlRestProperties> constructor = XacmlRestProperties.class.getDeclaredConstructor(); assertTrue(Modifier.isPrivate(constructor.getModifiers())); constructor.setAccessible(true); @@ -289,9 +285,12 @@ public class XacmlRestTest extends Mockito { XacmlRest.dumpRequest(httpServletRequest); Map<String, String[]> parameterMap = new LinkedHashMap<>(); - String[] mapValue0 = {"MapValue0"}; - String[] mapValue1 = {"MapValue0"}; - String[] mapValue2 = {}; + String[] mapValue0 = + { "MapValue0" }; + String[] mapValue1 = + { "MapValue0" }; + String[] mapValue2 = + {}; parameterMap.put("Key0", mapValue0); parameterMap.put("Key1", mapValue1); parameterMap.put("Key2", mapValue2); diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/jpa/ModelsTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/jpa/GetterSetterToStringTest.java index fb1b8a527..62d486c63 100644 --- a/ONAP-REST/src/test/java/org/onap/policy/rest/jpa/ModelsTest.java +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/jpa/GetterSetterToStringTest.java @@ -33,7 +33,7 @@ import org.onap.policy.common.utils.test.ToStringTester; /** * Class to perform unit testing of POJOs. */ -public class ModelsTest { +public class GetterSetterToStringTest { private static final String POJO_PACKAGE = "org.onap.policy.rest.jpa"; @Test diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/DummyPolicyContainer.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/DummyPolicyContainer.java new file mode 100644 index 000000000..385576744 --- /dev/null +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/DummyPolicyContainer.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.rest.util; + +import java.util.Collection; + +public class DummyPolicyContainer implements PolicyContainer { + private static final long serialVersionUID = 1L; + + @Override + public Collection<?> getContainerPropertyIds() { + return null; + } + + @Override + public Collection<?> getItemIds() { + return null; + } + + @Override + public Class<?> getType(Object propertyId) { + return null; + } + + @Override + public int size() { + return 0; + } + + @Override + public boolean containsId(Object itemId) { + return false; + } + + @Override + public Object addItem() { + return null; + } + + @Override + public boolean removeItem(Object itemId) { + return false; + } + + @Override + public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) { + return false; + } + + @Override + public boolean removeContainerProperty(Object propertyId) { + return false; + } + + @Override + public boolean removeAllItems() { + return false; + } +} diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/MSAttributeObjectTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/MsAttributeObjectTest.java index a39eb4996..8ec47fb0b 100644 --- a/ONAP-REST/src/test/java/org/onap/policy/rest/util/MSAttributeObjectTest.java +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/MsAttributeObjectTest.java @@ -3,6 +3,7 @@ * ONAP-REST * ================================================================================ * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,14 +22,16 @@ package org.onap.policy.rest.util; import static org.junit.Assert.assertTrue; + import java.util.HashMap; + import org.junit.Test; -public class MSAttributeObjectTest { +public class MsAttributeObjectTest { @Test - public void testMSAttributeObject() { - MSAttributeObject data = new MSAttributeObject(); + public void testMsAttributeObject() { + MsAttributeObject data = new MsAttributeObject(); data.setClassName("Test"); assertTrue("Test".equals(data.getClassName())); data.setRefAttribute(new HashMap<>()); @@ -60,8 +63,8 @@ public class MSAttributeObjectTest { } @Test - public void testMSAttributeValue() { - MSAttributeValue data = new MSAttributeValue(); + public void testMsAttributeValue() { + MsAttributeValue data = new MsAttributeValue(); data.setName("Test"); assertTrue("Test".equals(data.getName())); data.setType("Test"); diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/MSModelUtilsTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/MsModelUtilsTest.java index 34d555062..59115b959 100644 --- a/ONAP-REST/src/test/java/org/onap/policy/rest/util/MSModelUtilsTest.java +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/MsModelUtilsTest.java @@ -3,6 +3,7 @@ * ONAP-REST * ================================================================================ * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +26,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; + import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -32,6 +34,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; + import org.apache.commons.lang.StringUtils; import org.junit.Before; import org.junit.Test; @@ -39,12 +42,17 @@ import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.DictionaryData; -import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE; +import org.onap.policy.rest.util.MsModelUtils.ModelType; -public class MSModelUtilsTest { - private static Logger logger = FlexLogger.getLogger(MSModelUtilsTest.class); +public class MsModelUtilsTest { + private static Logger logger = FlexLogger.getLogger(MsModelUtilsTest.class); private static CommonClassDao commonClassDao; + /** + * Set tests up. + * + * @throws Exception on setup failures + */ @Before public void setUp() throws Exception { List<Object> dictionaryData = new ArrayList<Object>(); @@ -55,28 +63,28 @@ public class MSModelUtilsTest { logger.info("setUp: Entering"); commonClassDao = mock(CommonClassDao.class); when(commonClassDao.getDataById(DictionaryData.class, "dictionaryName", "GocVNFType")) - .thenReturn(dictionaryData); + .thenReturn(dictionaryData); } @Test - public void testMSModelUtils() { - HashMap<String, MSAttributeObject> classMap = new HashMap<>(); + public void testMsModelUtils() { + HashMap<String, MsAttributeObject> classMap = new HashMap<>(); ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("DKaTVESPolicy-v1802.xmi").getFile()); - MSModelUtils utils = new MSModelUtils("http://org.onap", "http://org.onap.policy"); - Map<String, MSAttributeObject> tempMap = - utils.processEpackage(file.getAbsolutePath().toString(), MODEL_TYPE.XMI); + MsModelUtils utils = new MsModelUtils("http://org.onap", "http://org.onap.policy"); + Map<String, MsAttributeObject> tempMap = utils.processEpackage(file.getAbsolutePath().toString(), + ModelType.XMI); classMap.putAll(tempMap); - MSAttributeObject mainClass = classMap.get("StandardDeviationThreshold"); - String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[] {"[", "]", " "}, - new String[] {"", "", ""}); + MsAttributeObject mainClass = classMap.get("StandardDeviationThreshold"); + String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[] + { "[", "]", " " }, new String[] + { "", "", "" }); List<String> dependency = new ArrayList<String>(Arrays.asList(dependTemp.split(","))); dependency = utils.getFullDependencyList(dependency, classMap); String subAttribute = utils.createSubAttributes(dependency, classMap, "StandardDeviationThreshold"); assertTrue(subAttribute != null); } - /** * Run the void stringBetweenDots(String, String) method test. */ @@ -85,25 +93,24 @@ public class MSModelUtilsTest { public void testStringBetweenDots() { // expect: uniqueKeys should contain a string value - MSModelUtils controllerA = new MSModelUtils(); + MsModelUtils controllerA = new MsModelUtils(); String str = "testing\\.byCorrectWay\\.OfDATA"; assertEquals(1, controllerA.stringBetweenDots(str)); // expect: uniqueKeys should not contain a string value str = "testing\byWrongtWay.\\OfDATA"; - MSModelUtils controllerB = new MSModelUtils(); + MsModelUtils controllerB = new MsModelUtils(); assertEquals(0, controllerB.stringBetweenDots(str)); } /** - * Run the Map<String,String> load(String) method test. + * Run the load method test. */ - @Test public void testLoad() { boolean isLocalTesting = true; - MSModelUtils controller = new MSModelUtils(); + MsModelUtils controller = new MsModelUtils(); String fileName = null; Map<String, String> result = null; try { @@ -146,7 +153,7 @@ public class MSModelUtilsTest { logger.error("Exception Occured while loading file" + e1); } - MSModelUtils controller = new MSModelUtils(commonClassDao); + MsModelUtils controller = new MsModelUtils(commonClassDao); if (isLocalTesting) { try { controller.parseTosca(fileName); diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PDPPolicyContainerTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PDPPolicyContainerTest.java deleted file mode 100644 index 9302df3b5..000000000 --- a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PDPPolicyContainerTest.java +++ /dev/null @@ -1,138 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Engine - * ================================================================================ - * Copyright (C) 2018 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.rest.util; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import java.util.HashSet; -import java.util.Set; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.rest.util.PDPPolicyContainer.PDPPolicyItem; -import org.onap.policy.xacml.std.pap.StdPDP; -import org.onap.policy.xacml.std.pap.StdPDPGroup; -import org.onap.policy.xacml.std.pap.StdPDPPolicy; -import com.att.research.xacml.api.pap.PDP; -import com.att.research.xacml.api.pap.PDPPolicy; - -public class PDPPolicyContainerTest { - StdPDPGroup group; - PDPPolicyContainer container; - StdPDPPolicy policy; - - @Before - public void setUp() { - group = new StdPDPGroup(); - group.setDefault(true); - group.setDefaultGroup(true); - group.setDescription("Test"); - group.setId("Test"); - group.setName("Test"); - group.setOnapPdps(new HashSet<>()); - group.setOperation("Test"); - group.setPipConfigs(new HashSet<>()); - HashSet<PDPPolicy> policies = new HashSet<>(); - policy = new StdPDPPolicy(); - policy.setName("Config_test.1.xml"); - policy.setId("Config_test"); - policy.setVersion("1.0"); - policy.setDescription("testDescription"); - policies.add(policy); - group.setPolicies(policies); - group.setSelectedPolicies(new HashSet<>()); - container = new PDPPolicyContainer(group); - } - - @Test - public void testPDPPolicyContainer() { - container.nextItemId(policy); - container.prevItemId(policy); - container.firstItemId(); - container.lastItemId(); - container.isFirstId(policy); - container.isLastId(policy); - container.addItemAfter(policy); - container.getContainerPropertyIds(); - container.getItemIds(); - container.getType("Id"); - assertTrue(String.class.equals(String.class)); - container.getType("Name"); - assertTrue(String.class.equals(String.class)); - container.getType("Version"); - assertTrue(String.class.equals(String.class)); - container.getType("Description"); - assertTrue(String.class.equals(String.class)); - container.getType("Root"); - assertTrue(Boolean.class.equals(Boolean.class)); - assertTrue(container.size() == 1); - container.containsId(policy); - container.removeItem(policy); - container.addContainerProperty(null, null, null); - container.removeContainerProperty(policy); - container.removeAllItems(); - container.addItemAt(0); - } - - @Test(expected = NullPointerException.class) - public void testConstructor() { - // Test PDP based constructor - PDP pdp = new StdPDP(); - PDPPolicyContainer container1 = new PDPPolicyContainer(pdp); - assertNotNull(container1); - - // Test set based constructor - Set<PDPPolicy> set = new HashSet<PDPPolicy>(); - PDPPolicyContainer container2 = new PDPPolicyContainer(set); - assertNotNull(container2); - - // Test object based constructor - PDPPolicyContainer container3 = new PDPPolicyContainer("testObject"); - assertNotNull(container3); - } - - @Test(expected = UnsupportedOperationException.class) - public void testAddItem() { - container.addItem(); - } - - @Test - public void testGetters() { - assertNull(container.nextItemId("testItem")); - assertNull(container.prevItemId("testItem")); - assertNotNull(container.firstItemId()); - assertNotNull(container.lastItemId()); - assertEquals(container.indexOfId("testItem"), -1); - assertNotNull(container.getIdByIndex(0)); - assertNotNull(container.getItemIds(0, 1)); - } - - @Test - public void testPDPPolicyItem() { - PDPPolicyItem item = container.new PDPPolicyItem(policy); - assertEquals("Config_test", item.getId()); - assertEquals("Config_test.1.xml", item.getName()); - assertEquals("1.0", item.getVersion()); - assertEquals("testDescription", item.getDescription()); - item.setRoot(true); - assertEquals(true, item.getRoot()); - } -} diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/ParserExceptionTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/ParserExceptionTest.java new file mode 100644 index 000000000..92b56a146 --- /dev/null +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/ParserExceptionTest.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.rest.util; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ParserExceptionTest { + + @Test + public void test() { + ParserException pe = new ParserException("A Message"); + assertEquals("A Message", pe.getMessage()); + } +} diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PdpPolicyContainerTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PdpPolicyContainerTest.java new file mode 100644 index 000000000..32d9e5255 --- /dev/null +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PdpPolicyContainerTest.java @@ -0,0 +1,267 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.rest.util; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFLogger.Level; +import com.att.eelf.configuration.EELFManager; +import com.att.research.xacml.api.pap.PDP; +import com.att.research.xacml.api.pap.PDPPolicy; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.logging.eelf.PolicyLogger; +import org.onap.policy.rest.util.PdpPolicyContainer.PdpPolicyItem; +import org.onap.policy.xacml.std.pap.StdPDP; +import org.onap.policy.xacml.std.pap.StdPDPGroup; +import org.onap.policy.xacml.std.pap.StdPDPPolicy; + +public class PdpPolicyContainerTest { + StdPDPGroup group; + PdpPolicyContainer container; + StdPDPPolicy policy; + + /** + * Set test up. + */ + @Before + public void setUp() { + group = new StdPDPGroup(); + group.setDefault(true); + group.setDefaultGroup(true); + group.setDescription("Test"); + group.setId("Test"); + group.setName("Test"); + group.setOnapPdps(new HashSet<>()); + group.setOperation("Test"); + group.setPipConfigs(new HashSet<>()); + policy = new StdPDPPolicy(); + policy.setName("Config_test.1.xml"); + policy.setId("Config_test"); + policy.setVersion("1.0"); + policy.setDescription("testDescription"); + + HashSet<PDPPolicy> policies = new HashSet<>(); + policies.add(policy); + group.setPolicies(policies); + group.setSelectedPolicies(new HashSet<>()); + container = new PdpPolicyContainer(group); + } + + @Test + public void testPdpPolicyContainer() { + container.nextItemId(policy); + container.prevItemId(policy); + container.firstItemId(); + container.lastItemId(); + container.isFirstId(policy); + container.isLastId(policy); + container.addItemAfter(policy); + container.getContainerPropertyIds(); + container.getItemIds(); + container.getType("Id"); + assertNull(container.getType("NonExistant")); + assertTrue(String.class.equals(String.class)); + container.getType("Name"); + assertTrue(String.class.equals(String.class)); + container.getType("Version"); + assertTrue(String.class.equals(String.class)); + container.getType("Description"); + assertTrue(String.class.equals(String.class)); + container.getType("Root"); + assertTrue(Boolean.class.equals(Boolean.class)); + assertTrue(container.size() == 1); + container.containsId(policy); + container.removeItem(policy); + container.addContainerProperty(null, null, null); + container.removeContainerProperty(policy); + container.removeAllItems(); + + assertFalse(container.isFirstId(policy)); + assertFalse(container.isLastId(policy)); + assertNull(container.firstItemId()); + assertNull(container.lastItemId()); + assertNull(container.prevItemId(policy)); + + assertThatThrownBy(() -> container.getItemIds(0, -1)).isInstanceOf(IllegalArgumentException.class); + + container.addItemAt(0); + } + + @Test + public void testPdpPolicyContainerWithTrace() { + EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger(); + + final Level currentLevel = PolicyLogger.getDebugLevel(); + debugLogger.setLevel(Level.TRACE); + + testPdpPolicyContainer(); + debugLogger.setLevel(currentLevel); + } + + @Test(expected = NullPointerException.class) + public void testConstructor() { + // Test PDP based constructor + PDP pdp = new StdPDP(); + PdpPolicyContainer container1 = new PdpPolicyContainer(pdp); + assertNotNull(container1); + + // Test set based constructor + Set<PDPPolicy> set = new HashSet<PDPPolicy>(); + PdpPolicyContainer container2 = new PdpPolicyContainer(set); + assertNotNull(container2); + + // Test object based constructor + PdpPolicyContainer container3 = new PdpPolicyContainer("testObject"); + assertNotNull(container3); + } + + @Test(expected = UnsupportedOperationException.class) + public void testAddItem() { + container.addItem(); + } + + @Test + public void testGettersWithTrace() { + EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger(); + + final Level currentLevel = PolicyLogger.getDebugLevel(); + debugLogger.setLevel(Level.TRACE); + + testGetters(); + debugLogger.setLevel(currentLevel); + } + + @Test + public void testGetters() { + assertNull(container.nextItemId("testItem")); + assertNull(container.prevItemId("testItem")); + assertNotNull(container.firstItemId()); + assertNotNull(container.lastItemId()); + assertEquals(container.indexOfId("testItem"), -1); + assertNotNull(container.getIdByIndex(0)); + assertNotNull(container.getItemIds(0, 1)); + } + + @Test + public void testPdpPolicyItemWithTrace() { + EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger(); + + final Level currentLevel = PolicyLogger.getDebugLevel(); + debugLogger.setLevel(Level.TRACE); + + testPdpPolicyItem(); + debugLogger.setLevel(currentLevel); + } + + @Test + public void testPdpPolicyItem() { + PdpPolicyItem item = container.new PdpPolicyItem(policy); + assertEquals("Config_test", item.getId()); + assertEquals("Config_test.1.xml", item.getName()); + assertEquals("1.0", item.getVersion()); + assertEquals("testDescription", item.getDescription()); + item.setRoot(true); + assertEquals(true, item.getRoot()); + } + + @Test + public void testContainerListOfPolicies() { + StdPDPGroup testGroup = new StdPDPGroup(); + testGroup.setDefault(true); + testGroup.setDefaultGroup(true); + testGroup.setDescription("Test"); + testGroup.setId("Test"); + testGroup.setName("Test"); + testGroup.setOnapPdps(new HashSet<>()); + testGroup.setOperation("Test"); + testGroup.setPipConfigs(new HashSet<>()); + + StdPDPPolicy testPolicy0 = new StdPDPPolicy(); + testPolicy0.setName("Config_test.0.xml"); + testPolicy0.setId("Config_test0"); + testPolicy0.setVersion("1.0"); + testPolicy0.setDescription("testDescription0"); + + StdPDPPolicy testPolicy1 = new StdPDPPolicy(); + testPolicy1.setName("Config_test.1.xml"); + testPolicy1.setId("Config_test1"); + testPolicy1.setVersion("1.0"); + testPolicy1.setDescription("testDescription1"); + + HashSet<PDPPolicy> policies = new HashSet<>(); + policies.add(testPolicy0); + policies.add(testPolicy1); + + testGroup.setPolicies(policies); + testGroup.setSelectedPolicies(new HashSet<>()); + PdpPolicyContainer testContainer = new PdpPolicyContainer(testGroup); + + assertEquals("Config_test0", ((PdpPolicyItem) testContainer.nextItemId(testPolicy1)).getId()); + assertEquals("Config_test1", ((PdpPolicyItem) testContainer.prevItemId(testPolicy0)).getId()); + + ObjectMapper mapper = new ObjectMapper(); + try { + mapper.writeValue(System.err, testPolicy0); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // @formatter:off + assertTrue(testContainer.removeItem("{" + + "\"PDPPolicyType\":\"StdPDPPolicy\"," + + "\"id\":\"Config_test0\"," + + "\"name\":\"Config_test.0.xml\"," + + "\"policyId\":null," + + "\"description\":\"testDescription0\"," + + "\"version\":\"1.0\"," + + "\"location\":null," + + "\"valid\":false," + + "\"root\":false" + + "}")); + assertFalse(testContainer.removeItem("{" + + "\"PDPPolicyType\":\"StdPDPPolicy\"," + + "\"id\":\"Config_test99\"," + + "\"name\":\"Config_test.0.xml\"," + + "\"policyId\":null," + + "\"description\":\"testDescription0\"," + + "\"version\":\"1.0\"," + + "\"location\":null," + + "\"valid\":false," + + "\"root\":false" + + "}")); + // @formatter:on + } +} diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyItemSetChangeNotifierTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyItemSetChangeNotifierTest.java index 9e5ff5d9f..5e42ec6f6 100644 --- a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyItemSetChangeNotifierTest.java +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyItemSetChangeNotifierTest.java @@ -3,13 +3,14 @@ * ONAP Policy Engine * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * 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. @@ -20,31 +21,57 @@ package org.onap.policy.rest.util; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; + import org.junit.Test; import org.mockito.Mockito; import org.onap.policy.rest.util.PolicyContainer.ItemSetChangeEvent; import org.onap.policy.rest.util.PolicyContainer.ItemSetChangeListener; public class PolicyItemSetChangeNotifierTest { - @Test - public void testNotifier() { - // Setup test data - ItemSetChangeListener listener = Mockito.mock(ItemSetChangeListener.class); - ItemSetChangeEvent event = Mockito.mock(ItemSetChangeEvent.class); - - // Test constructor - PolicyItemSetChangeNotifier notifier = new PolicyItemSetChangeNotifier(); - assertNotNull(notifier); - - // Test listener methods - try { - notifier.addItemSetChangeListener(listener); - notifier.fireItemSetChange(event); - notifier.removeItemSetChangeListener(listener); - } catch (Exception ex) { - fail("Not expecting any exceptions: " + ex); + @Test + public void testNotifier() { + + // Test constructor + PolicyItemSetChangeNotifier notifier = new PolicyItemSetChangeNotifier(); + assertNotNull(notifier); + + assertEquals(null, notifier.getItemSetChangeListeners()); + + ItemSetChangeListener listener1 = Mockito.mock(ItemSetChangeListener.class); + notifier.addItemSetChangeListener(listener1); + assertEquals(1, notifier.getItemSetChangeListeners().size()); + ItemSetChangeListener listener2 = Mockito.mock(ItemSetChangeListener.class); + notifier.addItemSetChangeListener(listener2); + assertEquals(2, notifier.getItemSetChangeListeners().size()); + + notifier.removeItemSetChangeListener(listener1); + assertEquals(1, notifier.getItemSetChangeListeners().size()); + notifier.addItemSetChangeListener(listener1); + assertEquals(2, notifier.getItemSetChangeListeners().size()); + + ItemSetChangeEvent event = Mockito.mock(ItemSetChangeEvent.class); + notifier.fireItemSetChange(event); + + notifier.removeItemSetChangeListener(listener1); + assertEquals(1, notifier.getItemSetChangeListeners().size()); + notifier.removeItemSetChangeListener(listener2); + assertEquals(0, notifier.getItemSetChangeListeners().size()); + notifier.removeItemSetChangeListener(listener2); + assertEquals(0, notifier.getItemSetChangeListeners().size()); + + notifier.setItemSetChangeListeners(null); + notifier.removeItemSetChangeListener(listener2); + assertEquals(null, notifier.getItemSetChangeListeners()); + + assertThatCode(() -> notifier.fireItemSetChange(event)).doesNotThrowAnyException(); + + notifier.setContainer(new DummyPolicyContainer()); + assertThatCode(() -> notifier.fireItemSetChange()).doesNotThrowAnyException(); + + PolicyContainer dummySource = new DummyPolicyContainer(); + assertEquals(dummySource, new PolicyItemSetChangeNotifier.BaseItemSetChangeEvent(dummySource).getContainer()); } - } } diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java index 6051db4c3..47b0f49b5 100644 --- a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java @@ -3,13 +3,14 @@ * ONAP-REST * ================================================================================ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * 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. @@ -17,12 +18,16 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.rest.util; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; + import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -41,9 +46,7 @@ public class PolicyValidationTest { } @Test - public void microServicePolicyTests() throws Exception{ - PolicyValidation validation = new PolicyValidation(); - PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper(); + public void microServicePolicyTests() throws Exception { PolicyParameters policyParameters = new PolicyParameters(); policyParameters.setPolicyConfigType(PolicyConfigType.MicroService); @@ -51,8 +54,20 @@ public class PolicyValidationTest { policyParameters.setPolicyDescription("This is a sample Micro Service policy Create example"); policyParameters.setOnapName("DCAE"); policyParameters.setPriority("1"); - String MSjsonString = "{\"service\":\"TOSCA_namingJenny\",\"location\":\"Test DictMSLoc\",\"uuid\":\"testDict DCAEUIID\",\"policyName\":\"testModelValidation\",\"description\":\"test\",\"configName\":\"testDict MSConfName\",\"templateVersion\":\"1607\",\"version\":\"gw12181031\",\"priority\":\"5\",\"policyScope\":\"resource=ResourcetypeVenktest1,service=ServiceName1707,type=Name1707,closedLoopControlName=Retest_retest1\",\"riskType\":\"Test\",\"riskLevel\":\"3\",\"guard\":\"True\",\"content\":{\"police-instance-name\":\"testing\",\"naming-models\":[{\"naming-properties\":[{\"property-value\":\"test\",\"source-endpoint\":\"test\",\"property-name\":\"testPropertyname\",\"increment-sequence\":{\"scope\":\"VNF\",\"start-value\":\"1\",\"length\":\"3\",\"increment\":\"2\"},\"source-system\":\"TOSCA\"}],\"naming-type\":\"testNamingType\",\"naming-recipe\":\"testNamingRecipe\"}]}}";; - policyParameters.setConfigBody(MSjsonString); + String msJsonString = "{\"service\":\"TOSCA_namingJenny\",\"location\":\"Test DictMSLoc\"," + + "\"uuid\":\"testDict DCAEUIID\",\"policyName\":\"testModelValidation\"," + + "\"description\":\"test\",\"configName\":\"testDict MSConfName\"," + + "\"templateVersion\":\"1607\",\"version\":\"gw12181031\",\"priority\":\"5\"," + + "\"policyScope\":\"resource=ResourcetypeVenktest1,service=ServiceName1707,type=Name1707," + + "closedLoopControlName=Retest_retest1\",\"riskType\":\"Test\",\"riskLevel\":\"3\"," + + "\"guard\":\"True\",\"content\":{\"police-instance-name\":\"testing\"," + + "\"naming-models\":[{\"naming-properties\":[{\"property-value\":\"test\"," + + "\"source-endpoint\":\"test\",\"property-name\":\"testPropertyname\"," + + "\"increment-sequence\":{\"scope\":\"VNF\",\"start-value\":\"1\",\"length\":\"3\"," + + "\"increment\":\"2\"},\"source-system\":\"TOSCA\"}],\"naming-type\":\"testNamingType\"," + + "\"naming-recipe\":\"testNamingRecipe\"}]}}"; + ; + policyParameters.setConfigBody(msJsonString); policyParameters.setRequestID(UUID.randomUUID()); SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy"); Date date = dateformat3.parse("15/10/2016"); @@ -62,8 +77,9 @@ public class PolicyValidationTest { policyParameters.setRiskType("TEST"); policyParameters.setRequestID(UUID.randomUUID()); - + PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper(); PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters); + PolicyValidation validation = new PolicyValidation(); StringBuilder responseString = validation.validatePolicy(policyData); assertNotSame("success", responseString.toString()); |