X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=PolicyEngineAPI%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fstd%2Ftest%2FNotificationUnMarshalTest.java;h=d26a1b25402add945879761eba87e9a337e8e6fa;hb=5450bdbfb94fb5217617da6c41971fd26f7e81b5;hp=fb42f98aae96922272a2daa7e95ff07631a18a3c;hpb=a9710cb3b80c73c98d257c676ba6ecf9e30ef758;p=policy%2Fengine.git diff --git a/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/NotificationUnMarshalTest.java b/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/NotificationUnMarshalTest.java index fb42f98aa..d26a1b254 100644 --- a/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/NotificationUnMarshalTest.java +++ b/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/NotificationUnMarshalTest.java @@ -20,237 +20,304 @@ package org.onap.policy.std.test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.onap.policy.api.UpdateType; import org.onap.policy.std.NotificationUnMarshal; +import org.onap.policy.std.StdLoadedPolicy; import org.onap.policy.std.StdPDPNotification; +import org.onap.policy.std.StdRemovedPolicy; + +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; /** - * The class NotificationUnMarshalTest contains tests for the class {@link NotificationUnMarshal}. + * The class NotificationUnMarshalTest contains tests for the class + * {@link NotificationUnMarshal}. * * @generatedBy CodePro at 6/1/16 1:40 PM * @version $Revision: 1.0 $ */ public class NotificationUnMarshalTest { - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_1() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_2() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_3() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_4() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_5() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_6() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_7() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_8() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_9() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Run the StdPDPNotification notificationJSON(String) method test. - * - * @throws Exception - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) - public void testNotificationJSON_10() - throws Exception { - String json = ""; - - StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); - - // add additional test code here - assertNotNull(result); - } - - /** - * Perform pre-test initialization. - * - * @throws Exception - * if the initialization fails for some reason - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @Before - public void setUp() - throws Exception { - // add additional set up code here - } - - /** - * Perform post-test clean-up. - * - * @throws Exception - * if the clean-up fails for some reason - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - @After - public void tearDown() - throws Exception { - // Add additional tear down code here - } - - /** - * Launch the test. - * - * @param args the command line arguments - * - * @generatedBy CodePro at 6/1/16 1:40 PM - */ - public static void main(String[] args) { - new org.junit.runner.JUnitCore().run(NotificationUnMarshalTest.class); - } + private static final String EMPTY_STRING = ""; + + private static final String POLICY_NAME = "ONAP"; + + private static final String POLICY_VERSION = "1.0.0"; + + /** + * Run the StdPDPNotification notificationJSON(String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 6/1/16 1:40 PM + */ + @Test(expected = JsonMappingException.class) + public void testNotificationJSON_EmptyString_ShouldThrowException() throws Exception { + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(EMPTY_STRING); + + assertNotNull(result); + } + + @Test + public void testNotificationJSON_StdPDPNotificationJsonStringWithEmptyLoadedAndRemovedPolicies_emptyLoadedPolices() + throws Exception { + final String json = getPDPNotificationAsJsonString(new StdPDPNotification()); + + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); + + assertNotNull(result); + assertTrue(result.getLoadedPolicies().isEmpty()); + } + + @Test + public void testNotificationJSON_StdPDPNotificationJsonStringWithEmptyLoadedpolicy_emptyLoadedPolices() + throws Exception { + final StdPDPNotification notification = getPDPNotification( + Arrays.asList(getRemovedPolicy(POLICY_VERSION, POLICY_NAME)), Collections.emptyList()); + final String json = getPDPNotificationAsJsonString(notification); + + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); + + assertNotNull(result); + assertTrue(result.getLoadedPolicies().isEmpty()); + } + + @Test + public void testNotificationJSON_validPDPNotificationJsonStringWithRemovedAndLoadedPolicies_UpdateTypeUpdateAndLoadedPolicyAdded() + throws Exception { + final List removedPolicies = Arrays.asList(getRemovedPolicy(POLICY_VERSION, POLICY_NAME)); + final List loadedPolicies = Arrays.asList(getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME)); + final StdPDPNotification notification = getPDPNotification(removedPolicies, loadedPolicies); + + final String json = getPDPNotificationAsJsonString(notification); + + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); + + assertNotNull(result); + assertFalse(result.getLoadedPolicies().isEmpty()); + + final StdLoadedPolicy actualPolicy = (StdLoadedPolicy) result.getLoadedPolicies().iterator().next(); + assertEquals(POLICY_VERSION, actualPolicy.getVersionNo()); + assertEquals(POLICY_NAME, actualPolicy.getPolicyName()); + assertEquals(UpdateType.UPDATE, actualPolicy.getUpdateType()); + } + + /** + * Run the StdPDPNotification notificationJSON(String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 6/1/16 1:40 PM + */ + @Test + public void testNotificationJSON_validPDPNotificationJsonStringWithRemovedAndLoadedPolicies_UpdateTypeNewAndLoadedPolicyAdded() + throws Exception { + final List removedPolicies = Arrays.asList(getRemovedPolicy(POLICY_VERSION, "Something")); + final List loadedPolices = Arrays.asList(getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME)); + final StdPDPNotification notification = getPDPNotification(removedPolicies, loadedPolices); + + final String json = getPDPNotificationAsJsonString(notification); + + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); + + assertNotNull(result); + assertFalse(result.getLoadedPolicies().isEmpty()); + + final StdLoadedPolicy actualPolicy = (StdLoadedPolicy) result.getLoadedPolicies().iterator().next(); + assertEquals(POLICY_VERSION, actualPolicy.getVersionNo()); + assertEquals(POLICY_NAME, actualPolicy.getPolicyName()); + assertEquals(UpdateType.NEW, actualPolicy.getUpdateType()); + } + + @Test + public void testNotificationJSON_validPDPNotificationJsonStringLoadedPoliciesAndNullRemovedPolicies_UpdateTypeNewAndLoadedPolicyAdded() + throws Exception { + final List loadedPolices = Arrays.asList(getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME)); + final StdPDPNotification notification = getPDPNotification(null, loadedPolices); + + final String json = getPDPNotificationAsJsonString(notification); + + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); + + assertNotNull(result); + assertFalse(result.getLoadedPolicies().isEmpty()); + + final StdLoadedPolicy actualPolicy = (StdLoadedPolicy) result.getLoadedPolicies().iterator().next(); + assertEquals(POLICY_VERSION, actualPolicy.getVersionNo()); + assertEquals(POLICY_NAME, actualPolicy.getPolicyName()); + assertEquals(UpdateType.NEW, actualPolicy.getUpdateType()); + } + + /** + * Run the StdPDPNotification notificationJSON(String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 6/1/16 1:40 PM + */ + @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) + public void testNotificationJSON_7() throws Exception { + final String json = EMPTY_STRING; + + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); + + // add additional test code here + assertNotNull(result); + } + + /** + * Run the StdPDPNotification notificationJSON(String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 6/1/16 1:40 PM + */ + @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) + public void testNotificationJSON_8() throws Exception { + final String json = EMPTY_STRING; + + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); + + // add additional test code here + assertNotNull(result); + } + + /** + * Run the StdPDPNotification notificationJSON(String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 6/1/16 1:40 PM + */ + @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) + public void testNotificationJSON_9() throws Exception { + final String json = EMPTY_STRING; + + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); + + // add additional test code here + assertNotNull(result); + } + + /** + * Run the StdPDPNotification notificationJSON(String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 6/1/16 1:40 PM + */ + @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class) + public void testNotificationJSON_10() throws Exception { + final String json = EMPTY_STRING; + + final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json); + + // add additional test code here + assertNotNull(result); + } + + private String getPDPNotificationAsJsonString(final StdPDPNotification notification) { + final ObjectMapper mapper = new ObjectMapper(); + + try { + return mapper.writeValueAsString(notification); + } catch (final Exception expection) { + throw new RuntimeException(expection); + } + + } + + private StdPDPNotification getPDPNotification(final Collection removedPolicies, + final Collection loadedPolicies) { + final StdPDPNotification oldNotification = new StdPDPNotification(); + oldNotification.setLoadedPolicies(loadedPolicies); + oldNotification.setRemovedPolicies(removedPolicies); + return oldNotification; + } + + private StdRemovedPolicy getRemovedPolicy(final String version, final String policyName) { + return new StdRemovedPolicy() { + + @Override + public String getVersionNo() { + return version; + } + + @Override + public String getPolicyName() { + return policyName; + } + }; + } + + private StdLoadedPolicy getStdLoadedPolicy(final String version, final String policyName) { + return new StdLoadedPolicy() { + + @Override + public String getVersionNo() { + return version; + } + + @Override + public String getPolicyName() { + return policyName; + } + + @Override + public Map getMatches() { + return Collections.emptyMap(); + } + }; + } + + /** + * Perform pre-test initialization. + * + * @throws Exception + * if the initialization fails for some reason + * + * @generatedBy CodePro at 6/1/16 1:40 PM + */ + @Before + public void setUp() throws Exception { + // add additional set up code here + } + + /** + * Perform post-test clean-up. + * + * @throws Exception + * if the clean-up fails for some reason + * + * @generatedBy CodePro at 6/1/16 1:40 PM + */ + @After + public void tearDown() throws Exception { + // Add additional tear down code here + } + + /** + * Launch the test. + * + * @param args + * the command line arguments + * + * @generatedBy CodePro at 6/1/16 1:40 PM + */ + public static void main(final String[] args) { + new org.junit.runner.JUnitCore().run(NotificationUnMarshalTest.class); + } }