Java tutorial
/** * 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.contrib.metadatarepository.webapp.controller; import org.openmrs.contrib.metadatarepository.Constants; import org.openmrs.contrib.metadatarepository.model.Address; import org.openmrs.contrib.metadatarepository.model.User; import org.openmrs.contrib.metadatarepository.webapp.controller.SignupController; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.validation.BindingResult; import org.springframework.validation.DataBinder; import org.subethamail.wiser.Wiser; import org.openmrs.contrib.metadatarepository.webapp.controller.BaseControllerTestCase; import javax.servlet.http.HttpServletResponse; import static org.junit.Assert.*; public class SignupControllerTest extends BaseControllerTestCase { @Autowired private SignupController c = null; @Test public void testDisplayForm() throws Exception { User user = c.showForm(); assertNotNull(user); } @Test public void testSignupUser() throws Exception { MockHttpServletRequest request = newPost("/signup.html"); Address address = new Address(); address.setCity("Denver"); address.setProvince("Colorado"); address.setCountry("USA"); address.setPostalCode("80210"); User user = new User(); user.setAddress(address); user.setUsername("self-registered"); user.setPassword("Password1"); user.setConfirmPassword("Password1"); user.setFirstName("First"); user.setLastName("Last"); user.setEmail("self-registered@raibledesigns.com"); user.setWebsite("http://raibledesigns.com"); user.setPasswordHint("Password is one with you."); HttpServletResponse response = new MockHttpServletResponse(); // start SMTP Server Wiser wiser = new Wiser(); wiser.setPort(getSmtpPort()); wiser.start(); BindingResult errors = new DataBinder(user).getBindingResult(); c.onSubmit(user, errors, request, response); assertFalse("errors returned in model", errors.hasErrors()); // verify an account information e-mail was sent wiser.stop(); assertTrue(wiser.getMessages().size() == 1); // verify that success messages are in the request assertNotNull(request.getSession().getAttribute("successMessages")); assertNotNull(request.getSession().getAttribute(Constants.REGISTERED)); SecurityContextHolder.getContext().setAuthentication(null); } }