org.homiefund.test.it.RegistrationServiceIT.java Source code

Java tutorial

Introduction

Here is the source code for org.homiefund.test.it.RegistrationServiceIT.java

Source

/**
 * Copyright  2016 REPLACE ME OWNER (REPLACE ME YEAR)
 *
 * 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.
 */
package org.homiefund.test.it;

import org.homiefund.api.dto.InvitationDTO;
import org.homiefund.api.dto.UserDTO;
import org.homiefund.api.service.HomeService;
import org.homiefund.api.service.RegistrationService;
import org.homiefund.exceptions.FieldException;
import org.homiefund.test.config.IntegrationTest;
import org.homiefund.test.config.WithUser;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Optional;

import static org.junit.Assert.fail;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 30.9.2016.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@IntegrationTest
public class RegistrationServiceIT {
    @Autowired
    private RegistrationService registrationService;
    @Autowired
    private HomeService homeService;

    @Test(expected = IllegalArgumentException.class)
    public void registerNull() throws FieldException {
        registrationService.register(null, Optional.empty());
    }

    @Test(expected = FieldException.class)
    public void registerWithID() throws FieldException {
        UserDTO user = new UserDTO();
        user.setId(Long.valueOf(1L));

        registrationService.register(user, Optional.empty());
    }

    @Test(expected = FieldException.class)
    public void emptyUsername() throws FieldException {
        UserDTO user = build(null, "test", "test", "test");
        try {
            registrationService.register(user, Optional.empty());
            fail(FieldException.class.getSimpleName());
        } catch (FieldException ex) {
            //ok
        }

        user.setUsername("");

        registrationService.register(user, Optional.empty());
    }

    @Test(expected = FieldException.class)
    public void emptyName() throws FieldException {
        UserDTO user = build("Test", "test", null, "test");
        try {
            registrationService.register(user, Optional.empty());
            fail(FieldException.class.getSimpleName());
        } catch (FieldException ex) {
            //ok
        }

        user.setName("");

        registrationService.register(user, Optional.empty());
    }

    @Test(expected = FieldException.class)
    public void emptyEmail() throws FieldException {
        UserDTO user = build("test", "test", "test", null);
        try {
            registrationService.register(user, Optional.empty());
            fail(FieldException.class.getSimpleName());
        } catch (FieldException ex) {

        }

        user.setEmail("");

        registrationService.register(user, Optional.empty());
    }

    @Test(expected = FieldException.class)
    public void emptyPassword() throws FieldException {
        UserDTO user = build("test", null, "test", "test");
        try {
            registrationService.register(user, Optional.empty());
            fail(FieldException.class.getSimpleName());
        } catch (FieldException ex) {

        }

        user.setPassword("");

        registrationService.register(user, Optional.empty());
    }

    @Test(expected = FieldException.class)
    public void usernameExists() throws FieldException {

        registrationService.register(build("emptak", "here", "can", "be anything"), Optional.empty());
    }

    @Test(expected = FieldException.class)
    public void emailExists() throws FieldException {
        registrationService.register(build("pista", "lifts", "paper", "admin@gmail.com"), Optional.empty());
    }

    @Test(expected = IllegalArgumentException.class)
    public void nullOptional() throws FieldException {
        registrationService.register(build("this", "man", "is", "valid"), null);
    }

    @Test
    public void validWithoutInvite() throws FieldException {
        UserDTO user = build("pista", "oi m8", "huehue", "admin@localhost");

        registrationService.register(user, Optional.empty());

        Assert.assertNotNull(user.getId());
    }

    // user is 2 because getHome requres user to be logged
    @Test
    @WithUser(id = 2L)
    public void validWithInvite() throws FieldException {
        // from dbunit.xml
        // invite is binded to home 1
        InvitationDTO invite = new InvitationDTO();
        invite.setHash("40aba0cadd26be424cab69ddd350ac1a");
        invite.setEmail("example@localhost");

        UserDTO user = build("pista", "oi m8", "huehue", "example@localhost");
        registrationService.register(user, Optional.of(invite));

        Assert.assertNotNull(user.getId());
        Assert.assertTrue(homeService.getByID(1L).getMembers().contains(user));
    }

    /**
     * helper method to setup user
     *
     * @param username username
     * @param password password
     * @param name     name
     * @param email    email
     * @return user with values given as method parameters
     */
    private UserDTO build(String username, String password, String name, String email) {
        UserDTO user = new UserDTO();
        user.setPassword(password);
        user.setEmail(email);
        user.setUsername(username);
        user.setName(name);

        return user;
    }
}