diff options
author | Wang,Frank(gw1218) <gw1218@att.com> | 2018-09-13 21:09:47 -0500 |
---|---|---|
committer | Wang, Frank(gw1218) <gw1218@att.com> | 2018-09-17 16:04:58 -0500 |
commit | 017f36c7a74c52ca9abe05607920dcb3c524f0ad (patch) | |
tree | 17ac164e8efaf128e05528c351b662e5c5f769bb /POLICY-SDK-APP/src/test | |
parent | 55e795c171cabb7800effcbbb75bed0224b92691 (diff) |
Added validation on matching required fields
Import policy should report to end users if there are missing required
matching fields in policy template.
Issue-ID: POLICY-1117
Change-Id: Iac4485259b73784e641f42a22d5a12c524f4b8e2
Signed-off-by: Wang,Frank(gw1218) <gw1218@att.com>
Signed-off-by: Wang, Frank(gw1218) <gw1218@att.com>
Diffstat (limited to 'POLICY-SDK-APP/src/test')
-rw-r--r-- | POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java | 121 |
1 files changed, 65 insertions, 56 deletions
diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java index 358108fa1..1e4e62da1 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java @@ -21,6 +21,7 @@ package org.onap.policy.controller; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; @@ -47,62 +48,70 @@ import com.mockrunner.mock.web.MockHttpServletResponse; @RunWith(PowerMockRunner.class) public class PolicyExportAndImportControllerTest { - @Test - public void testSetAndGet(){ - PolicyExportAndImportController controller = new PolicyExportAndImportController(); - PolicyController policyController = new PolicyController(); - controller.setPolicyController(policyController); - assertEquals(controller.getPolicyController(), policyController); - CommonClassDao commonClassDao = new CommonClassDaoImpl(); - PolicyExportAndImportController.setCommonClassDao(commonClassDao); - assertEquals(PolicyExportAndImportController.getCommonClassDao(), commonClassDao); - } - - @Test - public void testExport() throws IOException { - PolicyExportAndImportController controller = new PolicyExportAndImportController(); - MockHttpServletRequest request = new MockHttpServletRequest(); - request.setBodyContent("{\n\"exportData\": {}\n}\n"); - MockHttpServletResponse response = new MockHttpServletResponse(); - - // Test negative case - controller.exportPolicy(request, response); - assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK); - } - - @PrepareForTest({UserUtils.class}) - @Test - public void testImport() throws IOException { - // Mock user utilities - PowerMockito.mockStatic(UserUtils.class); - User user = new User(); - when(UserUtils.getUserSession(any())).thenReturn(user); + @Test + public void testSetAndGet() { + PolicyExportAndImportController controller = new PolicyExportAndImportController(); + PolicyController policyController = new PolicyController(); + controller.setPolicyController(policyController); + assertEquals(controller.getPolicyController(), policyController); + CommonClassDao commonClassDao = new CommonClassDaoImpl(); + PolicyExportAndImportController.setCommonClassDao(commonClassDao); + assertEquals(PolicyExportAndImportController.getCommonClassDao(), commonClassDao); + } - // Mock dao - UserInfo info = new UserInfo(); - ConfigurationDataEntity configEntity = new ConfigurationDataEntity(); - CommonClassDao commonClassDao = Mockito.mock(CommonClassDaoImpl.class); - when(commonClassDao.getEntityItem(eq(UserInfo.class), any(), any())).thenReturn(info); - when(commonClassDao.getEntityItem(eq(ConfigurationDataEntity.class), any(), any())).thenReturn(configEntity); - when(commonClassDao.getDataById(any(), any(), any())).thenReturn(Collections.emptyList()); + @Test + public void testExport() throws IOException { + PolicyExportAndImportController controller = new PolicyExportAndImportController(); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setBodyContent("{\n\"exportData\": {}\n}\n"); + MockHttpServletResponse response = new MockHttpServletResponse(); - // Test import - ClassLoader classLoader = getClass().getClassLoader(); - PolicyController policyController = new PolicyController(); - PolicyController.setCommonClassDao(commonClassDao); - PolicyExportAndImportController controller = new PolicyExportAndImportController(); - PolicyExportAndImportController.setCommonClassDao(commonClassDao); - controller.setPolicyController(policyController); - HttpServletRequest request = new MockHttpServletRequest(); - - // Test negative case - String file = new File(classLoader.getResource("Config_BRMS_Raw_TestBRMSRawPolicy.1.xml").getFile()).getAbsolutePath(); - JSONObject json = controller.importRepositoryFile(file, request); - assertNull(json); - - // Another negative case - file = new File(classLoader.getResource("PolicyExport.xls").getFile()).getAbsolutePath(); - json = controller.importRepositoryFile(file, request); - assertNull(json); - } + // Test negative case + controller.exportPolicy(request, response); + assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK); + } + + @PrepareForTest({UserUtils.class}) + @Test + public void testImport() throws IOException { + // Mock user utilities + PowerMockito.mockStatic(UserUtils.class); + User user = new User(); + when(UserUtils.getUserSession(any())).thenReturn(user); + + // Mock dao + UserInfo info = new UserInfo(); + ConfigurationDataEntity configEntity = new ConfigurationDataEntity(); + CommonClassDao commonClassDao = Mockito.mock(CommonClassDaoImpl.class); + when(commonClassDao.getEntityItem(eq(UserInfo.class), any(), any())).thenReturn(info); + when(commonClassDao.getEntityItem(eq(ConfigurationDataEntity.class), any(), any())).thenReturn(configEntity); + when(commonClassDao.getDataById(any(), any(), any())).thenReturn(Collections.emptyList()); + + // Test import + PolicyController policyController = new PolicyController(); + PolicyController.setCommonClassDao(commonClassDao); + PolicyExportAndImportController controller = new PolicyExportAndImportController(); + PolicyExportAndImportController.setCommonClassDao(commonClassDao); + controller.setPolicyController(policyController); + HttpServletRequest request = new MockHttpServletRequest(); + ClassLoader classLoader = getClass().getClassLoader(); + + // Test negative case + String file = + new File(classLoader.getResource("Config_BRMS_Raw_TestBRMSRawPolicy.1.xml").getFile()) + .getAbsolutePath(); + JSONObject json = controller.importRepositoryFile(file, request); + assertNull(json); + + // Another negative case + file = new File(classLoader.getResource("PolicyExport.xls").getFile()).getAbsolutePath(); + json = controller.importRepositoryFile(file, request); + assertNull(json); + + // test validation + String jsonString = "{ configName:\"abc\", uuid:\"someone\", location:\"somewhere\", policyScope:\"test\", " + + "service:\"sdnc\", version:\"1810\"}"; + String errorMsg = controller.validatMatchRequiredFields("TestPolicy", jsonString); + assertTrue(errorMsg != null); + } } |