JUnit additions for BRMS,POLICY-SDK-APP
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / PolicyExportAndImportControllerTest.java
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
new file mode 100644 (file)
index 0000000..358108f
--- /dev/null
@@ -0,0 +1,108 @@
+/*-
+ * ============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.controller;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.when;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+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;
+import org.mockito.Mockito;
+import org.onap.policy.daoImp.CommonClassDaoImpl;
+import org.onap.policy.rest.dao.CommonClassDao;
+import org.onap.policy.rest.jpa.ConfigurationDataEntity;
+import org.onap.policy.rest.jpa.UserInfo;
+import org.onap.portalsdk.core.domain.User;
+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);
+
+               // 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
+               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);
+       }
+}