X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontroller%2FPolicyExportAndImportControllerTest.java;h=84e9ccf845aaad0a3a9f0d2f0ea77680ff85bcd9;hb=dfd9c0a09c35e4b5b4b61be08b8424e4a3d0d500;hp=358108fa1b6e118336a21d44cf109baf31bad566;hpb=80290eeafd9c685d1ad550747855227b02bb0232;p=policy%2Fengine.git 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..84e9ccf84 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-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. @@ -17,18 +17,26 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.controller; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.mockito.Mockito.when; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; + +import com.mockrunner.mock.web.MockHttpServletRequest; +import com.mockrunner.mock.web.MockHttpServletResponse; + import java.io.File; import java.io.IOException; import java.util.Collections; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + import org.json.JSONObject; import org.junit.Test; import org.junit.runner.RunWith; @@ -42,67 +50,72 @@ import org.onap.portalsdk.core.web.support.UserUtils; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import com.mockrunner.mock.web.MockHttpServletRequest; -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); + } + + @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); + + // 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); - // 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()); + // Another negative case + file = new File(classLoader.getResource("PolicyExport.xls").getFile()).getAbsolutePath(); + json = controller.importRepositoryFile(file, request); + assertNull(json); - // 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 validation + String jsonString = "{ configName:\"abc\", uuid:\"someone\", location:\"somewhere\", policyScope:\"test\", " + + "service:\"sdnc\", version:\"1810\"}"; + String errorMsg = controller.validatMatchRequiredFields("TestPolicy", jsonString); + assertTrue(errorMsg != null); + } }