/**
* Copyright 2011 The Apache Software Foundation
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 idv.takeshi.persistence.dao.mybatis;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import idv.takeshi.model.Person;
import idv.takeshi.model.Role;
import idv.takeshi.model.RoleAlreadyAssignedException;
import idv.takeshi.model.RoleRelationship;
import idv.takeshi.persistence.dao.PersonDao;
import idv.takeshi.persistence.dao.RoleDao;
import idv.takeshi.test.AbstractSpringTest;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class PersonDaoImplTest extends AbstractSpringTest {
@Autowired
private PersonDao personDao;
@Autowired
private RoleDao roleDao;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testGetAll(){
List<Person> persons = this.personDao.getAll();
assertNotNull(persons);
assertTrue(persons.size() > 0);
}
@Test
public void testGetById(){
Person person = null;
Long id = new Long("-1");
person = this.personDao.getById(-1L);
assertNotNull(person);
assertEquals(id, person.getId());
}
@Test
public void testDeleteById(){
Long id = new Long("-1");
Person person = null;
this.personDao.deleteById(id);
person = this.personDao.getById(id);
assertNull(person);
}
@Test
public void testUpdate(){
Long id = new Long("-2");
String newLastName = "AAAA";
String lastName = null;
String newFirstName = "BBBB";
String firstName = null;
String newPassword = "abcdefg";
String password = null;
Person person = null;
List<RoleRelationship> roleRelationships = null;
person = this.personDao.getById(id);
lastName = person.getLastName();
firstName = person.getFirstName();
password = person.getPassword();
assertNotNull(lastName);
assertNotNull(firstName);
assertNotNull(password);
assertFalse(lastName.equals(newLastName));
assertFalse(firstName.equals(newFirstName));
assertFalse(password.equals(newPassword));
person.setLastName(newLastName);
person.setFirstName(newFirstName);
person.setPassword(newPassword);
roleRelationships = person.getRoleRelationships();
this.personDao.update(person);
person = null;
assertNull(person);
person = this.personDao.getById(id);
assertEquals(newFirstName, person.getFirstName());
assertEquals(newLastName, person.getLastName());
assertEquals(newPassword, person.getPassword());
assertEquals(roleRelationships, person.getRoleRelationships());
}
@Test
public void testAdd() throws RoleAlreadyAssignedException{
Person person = null;
List<Role> roles = null;
String firstName = "AAAA";
String lastName = "BBBBB";
String accountName = "ABCDEFG";
String password = "lmnopqrstu";
Long newPersonId = null;
List<RoleRelationship> roleRelationships = null;
roles = this.roleDao.getAll();
assertNotNull(roles);
assertTrue(roles.size() > 0);
person = new Person();
person.setFirstName(firstName);
person.setLastName(lastName);
person.setAccountName(accountName);
person.setPassword(password);
person.addRole(roles.get(1));
roleRelationships = person.getRoleRelationships();
this.personDao.add(person);
newPersonId = person.getId();
person = this.personDao.getById(newPersonId);
assertNotNull(person);
assertEquals(newPersonId, person.getId());
assertEquals(firstName, person.getFirstName());
assertEquals(lastName, person.getLastName());
assertEquals(accountName, person.getAccountName());
assertEquals(password, person.getPassword());
assertEquals(roleRelationships, person.getRoleRelationships());
}
@Test
public void testGetByAccountName(){
String accountName = null;
Person person = null;
//1. with upper-case
accountName = "TEST1";
person = this.personDao.getByAccountName(accountName);
assertNotNull(person);
//2. with lower-case
accountName = accountName.toLowerCase();
person = this.personDao.getByAccountName(accountName);
assertNotNull(person);
}
}