cz.swi2.mendeluis.service.UserServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.swi2.mendeluis.service.UserServiceTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.swi2.mendeluis.service;

import cz.swi2.mendeluis.dataaccesslayer.core.DatabaseConfig;
import cz.swi2.mendeluis.dataaccesslayer.domain.Portlet;
import cz.swi2.mendeluis.dataaccesslayer.domain.Role;
import cz.swi2.mendeluis.dataaccesslayer.domain.User;
import cz.swi2.mendeluis.service.config.ServiceConfiguration;
import cz.swi2.mendeluis.service.exception.UniqueViolationException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.log4j.varia.NullAppender;
import static org.junit.Assert.*;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Ivo
 */
@ContextConfiguration(classes = ServiceConfiguration.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@Transactional
public class UserServiceTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private UserService userService;

    public UserServiceTest() {
        org.apache.log4j.BasicConfigurator.configure(new NullAppender());
    }

    @org.testng.annotations.Test
    public void testCreate() {
        User user;
        try {
            // Create user
            user = this.userService.createNewUser("aaa", "aaa", "aaa");

            // Find user
            User result = this.userService.getUserById(user.getId());

            // assert equal
            assertEquals(user, result);

        } catch (UniqueViolationException ex) {
            Logger.getLogger(PortletServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    @org.testng.annotations.Test
    public void testGetAll() {
        User user, user2;
        try {
            // Create users
            user = this.userService.createNewUser("aaa", "aaa", "aaa");
            user2 = this.userService.createNewUser("aaa2", "aaa2", "aaa");

            // Find users
            List<User> results = this.userService.getAllUsers();

            // assert equal
            List<User> users = new ArrayList();
            users.add(user);
            users.add(user2);

            // We do not use a different db for testing. Thefore we must test only matching items, not all. 
            assertTrue(results.containsAll(users));

        } catch (UniqueViolationException ex) {
            Logger.getLogger(PortletServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    @org.testng.annotations.Test
    public void testDelete() {
        User user;
        try {
            // Create user
            user = this.userService.createNewUser("aaa", "aaa", "aaa");

            // Delete user
            int id = user.getId();
            this.userService.deleteUser(id);

            // Find portlet
            User result = this.userService.getUserById(id);

            // assert equal
            assertEquals(result, null);

        } catch (UniqueViolationException ex) {
            Logger.getLogger(PortletServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    @org.testng.annotations.Test
    public void testGetRoleForUser() {
        User user;
        try {
            // Create user
            String username = "aaa";
            this.userService.createNewUser("aaa", username, "aaa");

            // Find user
            user = this.userService.getUserByUsername(username);

            // Get role
            Role role = userService.getRoleForUser(user);

            // assert equal
            assertEquals(role, Role.ROLE_USER);

        } catch (UniqueViolationException ex) {
            Logger.getLogger(PortletServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    @org.testng.annotations.Test
    public void testUserByCredentials() {
        User user, result;
        try {
            // Create user
            String username = "aaa";
            String password = "aaa";
            user = this.userService.createNewUser("aaa", username, password);

            // Find user
            result = this.userService.getUserByCredentials(username, password);

            // assert equal
            assertEquals(user, result);

        } catch (UniqueViolationException ex) {
            Logger.getLogger(PortletServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

}