Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spring.jpa.open-in-view=true
# What do with the schema
# drop n create table again, good for testing
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always
spring.datasource.initialization-mode=never
#
# Good for production!
# spring.jpa.hibernate.ddl-auto=update
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package com.lambdaschool.usermodel.services;

import com.lambdaschool.usermodel.UserModelApplicationTesting;
import com.lambdaschool.usermodel.models.Role;
import com.lambdaschool.usermodel.models.User;
import com.lambdaschool.usermodel.models.UserRoles;
import com.lambdaschool.usermodel.models.Useremail;
import com.lambdaschool.usermodel.repository.RoleRepository;
import com.lambdaschool.usermodel.repository.UserRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserModelApplicationTesting.class,
properties = {
"command.line.runner.enabled=false"})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UserServiceImplTestNoDB {
@Autowired
private RoleService roleService;
@Autowired
private UserService userService;

@MockBean
private UserRepository userRepository;

@MockBean
private RoleRepository rolerepos;

@Autowired
private HelperFunctions helperFunctions;

private List<User> userList;

@Before
public void setUp() throws Exception {userList = new ArrayList<>();

Role role1 = new Role("admin");
Role role2 = new Role("user");
Role role3 = new Role("data");

role1.setRoleid(1);;
role2.setRoleid(2);
role3.setRoleid(3);

User u1 = new User("admin","password",
"[email protected]");
u1.setUserid(10);
u1.getRoles()
.add(new UserRoles(u1,
role1));
u1.getRoles()
.add(new UserRoles(u1,
role2));
u1.getRoles()
.add(new UserRoles(u1,
role3));
u1.getUseremails()
.add(new Useremail(u1,
"[email protected]"));
u1.getUseremails()
.add(new Useremail(u1,
"[email protected]"));

userList.add(u1);

// data, user
User u2 = new User("cinnamon",
"1234567",
"[email protected]");
u2.setUserid(20);
u2.getRoles()
.add(new UserRoles(u2,
role2));
u2.getRoles()
.add(new UserRoles(u2,
role3));
u2.getUseremails()
.add(new Useremail(u2,
"[email protected]"));
u2.getUseremails()
.add(new Useremail(u2,
"[email protected]"));
u2.getUseremails()
.add(new Useremail(u2,
"[email protected]"));
userList.add(u2);

// user
User u3 = new User("barnbarn",
"ILuvM4th!",
"[email protected]");
u3.setUserid(30);
u3.getRoles()
.add(new UserRoles(u3,
role2));
u3.getUseremails()
.add(new Useremail(u3,
"[email protected]"));
userList.add(u3);

User u4 = new User("puttat",
"password",
"[email protected]");
u4.setUserid(40);
u4.getRoles()
.add(new UserRoles(u4,
role2));
userList.add(u4);


User u5 = new User("misskitty",
"password",
"[email protected]");
u5.setUserid(50);
u5.getRoles()
.add(new UserRoles(u5,
role2));
userList.add(u5);
MockitoAnnotations.initMocks(this);

}

@After
public void tearDown() throws Exception {
}

@Test
public void findUserById() {
Mockito.when(userRepository.findById(20L))
.thenReturn(Optional.of(userList.get(1)));

assertEquals("cinnamon",
userService.findUserById(20)
.getUsername());


}

@Test
public void findByNameContaining () {
Mockito.when(userRepository.findByUsernameContainingIgnoreCase("barnbarn"))
.thenReturn(userList);

assertEquals(5, userService.findByNameContaining("barnbarn").size());

}
@Test
public void findAll() {
Mockito.when(userRepository.findAll())
.thenReturn(userList);
assertEquals(5,userService.findAll().size());

}

@Test
public void delete() {
Mockito.when(userRepository.findById(30L))
.thenReturn(Optional.of(userList.get(0)));
Mockito.doNothing()
.when(userRepository)
.deleteById(30L);
}

// @Test
// public void findByName () {
// Mockito.when(userRepository.findByUsernameContainingIgnoreCase("barnbarn"))
// .thenReturn( userList.get(0));
// assertEquals("barnbarn",userService.findByName("barnbarn").getUsername());
// }
@Test
public void save() {
String userName3 = "Marley";
User user3 = new User(userName3,
"cingaderosa",
"[email protected]");
user3.setUserid(50L);
Role roleType1 = new Role("Turtle");
roleType1.setRoleid(1);

user3.getRoles()
.add(new UserRoles(user3,roleType1));

Mockito.when(userRepository.save(any(User.class)))
.thenReturn(user3);
Mockito.when(rolerepos.findById(1L))
.thenReturn(Optional.of(roleType1));

User addUser = userService.save(user3);
assertNotNull(addUser);
assertEquals(userName3,
addUser.getUsername());

}

@Test
public void update() {
}

@Test
public void deleteAll() {
}
}