<commit_before>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.web.test;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;

import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.openmrs.module.BaseModuleActivatorTest;
import org.openmrs.module.Module;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.web.controller.ModuleListController;
import org.openmrs.util.OpenmrsClassLoader;
import org.openmrs.web.WebConstants;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.mock.web.MockMultipartHttpServletRequest;
import org.springframework.test.annotation.NotTransactional;
import org.springframework.test.context.ContextConfiguration;

/**
 * Tests all actions in ModuleListController. When i move this class to the
 * org.openmrs.module.web.controller package, it fails when run under maven, though passes in
 * eclipse. And i just do not know why. :)
 */
@ContextConfiguration(locations = { "classpath*:webModuleApplicationContext.xml" }, loader = TestContextLoader.class)
public class WebModuleListControllerTest extends BaseModuleActivatorTest {
	
	@Test
	@NotTransactional
	public void shouldStopModule() throws Exception {
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		request.setParameter("action", "stop");
		request.setParameter("moduleId", MODULE3_ID);
		
		ModuleListController controller = (ModuleListController) applicationContext.getBean("moduleListController");
		controller.handleRequest(request, response);
		
		assertTrue("Module.stopped".equals(request.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR)));
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR) == null);
	}
	
	@Test
	@NotTransactional
	public void shouldStartModule() throws Exception {
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		request.setParameter("action", "start");
		request.setParameter("moduleId", MODULE3_ID);
		
		ModuleListController controller = (ModuleListController) applicationContext.getBean("moduleListController");
		controller.handleRequest(request, response);
		
		assertTrue("Module.started".equals(request.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR)));
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR) == null);
	}
	
	@Test
	@NotTransactional
	public void shouldUnloadModule() throws Exception {
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		request.setParameter("action", "unload");
		request.setParameter("moduleId", MODULE3_ID);
		
		ModuleListController controller = (ModuleListController) applicationContext.getBean("moduleListController");
		controller.handleRequest(request, response);
		
		assertTrue("Module.unloaded".equals(request.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR)));
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR) == null);
	}
	
	@Test
	@NotTransactional
	public void shouldUpdateModule() throws Exception {
		
		Module module = ModuleFactory.getModuleById(MODULE3_ID);
		
		assertTrue(module.getVersion().equals("1.0-SNAPSHOT"));
		
		URL url = OpenmrsClassLoader.getInstance().getResource("org/openmrs/module/include/test3-2.0-SNAPSHOT.omod");
		module.setDownloadURL("file:" + url.getFile());
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		request.setParameter("action", "Module.installUpdate");
		request.setParameter("moduleId", MODULE3_ID);
		
		ModuleListController controller = (ModuleListController) applicationContext.getBean("moduleListController");
		controller.handleRequest(request, response);
		
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR) == null);
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR) == null);
	}
	
	@Test
	@NotTransactional
	public void shouldUploadDownloadedModule() throws Exception {
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		URL url = OpenmrsClassLoader.getInstance().getResource("org/openmrs/module/include/test3-1.0-SNAPSHOT.omod");
		request.setParameter("action", "upload");
		request.setParameter("download", "true");
		request.setParameter("downloadURL", "file:" + url.getFile());
		<commit_msg>Adding an assert for module version after update - TRUNK-4134<commit_after>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.web.test;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;

import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.openmrs.module.BaseModuleActivatorTest;
import org.openmrs.module.Module;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.web.controller.ModuleListController;
import org.openmrs.util.OpenmrsClassLoader;
import org.openmrs.web.WebConstants;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.mock.web.MockMultipartHttpServletRequest;
import org.springframework.test.annotation.NotTransactional;
import org.springframework.test.context.ContextConfiguration;

/**
 * Tests all actions in ModuleListController. When i move this class to the
 * org.openmrs.module.web.controller package, it fails when run under maven, though passes in
 * eclipse. And i just do not know why. :)
 */
@ContextConfiguration(locations = { "classpath*:webModuleApplicationContext.xml" }, loader = TestContextLoader.class)
public class WebModuleListControllerTest extends BaseModuleActivatorTest {
	
	@Test
	@NotTransactional
	public void shouldStopModule() throws Exception {
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		request.setParameter("action", "stop");
		request.setParameter("moduleId", MODULE3_ID);
		
		ModuleListController controller = (ModuleListController) applicationContext.getBean("moduleListController");
		controller.handleRequest(request, response);
		
		assertTrue("Module.stopped".equals(request.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR)));
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR) == null);
	}
	
	@Test
	@NotTransactional
	public void shouldStartModule() throws Exception {
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		request.setParameter("action", "start");
		request.setParameter("moduleId", MODULE3_ID);
		
		ModuleListController controller = (ModuleListController) applicationContext.getBean("moduleListController");
		controller.handleRequest(request, response);
		
		assertTrue("Module.started".equals(request.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR)));
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR) == null);
	}
	
	@Test
	@NotTransactional
	public void shouldUnloadModule() throws Exception {
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		request.setParameter("action", "unload");
		request.setParameter("moduleId", MODULE3_ID);
		
		ModuleListController controller = (ModuleListController) applicationContext.getBean("moduleListController");
		controller.handleRequest(request, response);
		
		assertTrue("Module.unloaded".equals(request.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR)));
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR) == null);
	}
	
	@Test
	@NotTransactional
	public void shouldUpdateModule() throws Exception {
		
		Module module = ModuleFactory.getModuleById(MODULE3_ID);
		
		assertTrue(module.getVersion().equals("1.0-SNAPSHOT"));
		
		URL url = OpenmrsClassLoader.getInstance().getResource("org/openmrs/module/include/test3-2.0-SNAPSHOT.omod");
		module.setDownloadURL("file:" + url.getFile());
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		request.setParameter("action", "Module.installUpdate");
		request.setParameter("moduleId", MODULE3_ID);
		
		ModuleListController controller = (ModuleListController) applicationContext.getBean("moduleListController");
		controller.handleRequest(request, response);
		
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR) == null);
		assertTrue(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR) == null);
		
		assertTrue(ModuleFactory.getModuleById(MODULE3_ID).getVersion().equals("2.0-SNAPSHOT"));
	}
	
	@Test
	@NotTransactional
	public void shouldUploadDownloadedModule() throws Exception {
		
		init();
		createWebInfFolderIfNotExist();
		
		MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
		HttpServletResponse response = new MockHttpServletResponse();
		
		URL url = OpenmrsClassLoader.getInstance().getResource("org/openmrs/module/include/test3-1.0-SNAPSHOT.omod");
		request.setParameter("action", "upload");
		request.setParameter("download", "true");
		request.setParameter("downloadURL", "file:" + url.getFile());
		
{'fraction_non_alphanumeric': 0.10875331564986737, 'fraction_numerical': 0.003271441202475685, 'mean_word_length': 6.190718372536554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2016844', 'n_tokens_mistral': 3245, 'n_tokens_neox': 3053, 'n_words': 643}