Skip to content
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
7 changes: 7 additions & 0 deletions usermodel-initial/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
<version>2.9.2</version>
</dependency>
<!-- Swagger Dependencies End -->
<!-- https://mvnrepository.com/artifact/io.rest-assured/spring-mock-mvc -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package com.lambdaschool.usermodel.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lambdaschool.usermodel.models.Role;
import com.lambdaschool.usermodel.models.User;
import com.lambdaschool.usermodel.models.UserRoles;
import com.lambdaschool.usermodel.services.HelperFunctions;
import com.lambdaschool.usermodel.services.UserService;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringRunner.class)
@WebMvcTest(value = UserController.class)
public class UserControllerTest
{
@Autowired
private MockMvc mockMvc;

@MockBean
private HelperFunctions helperFunctions;
@MockBean
private UserService userService;

private List<User> userList;

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

// Role r1 = new Role("admin");

User u1 = new User("admin",
"password",
"admin@lambdaschool.local");
// u1.getRoles()
// .add(new UserRoles(u1, r1));
u1.setUserid(10);
userList.add(u1);
User u2 = new User("cinnamon",
"1234567",
"cinnamon@lambdaschool.local");
u2.setUserid(20);
userList.add(u2);
User u3 = new User("barnbarn",
"ILuvM4th!",
"barnbarn@lambdaschool.local");
u3.setUserid(30);
userList.add(u3);
User u4 = new User("puttat",
"password",
"puttat@school.lambda");
u4.setUserid(40);
userList.add(u4);
User u5 = new User("misskitty",
"password",
"misskitty@school.lambda");
u5.setUserid(50);
userList.add(u5);
}

@After
public void tearDown() throws Exception
{
}

@Test
public void listAllUsers() throws Exception
{
String apiUrl = "/users/users";
Mockito.when(userService.findAll()).thenReturn(userList);

RequestBuilder rb =
MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);
MvcResult r = mockMvc.perform(rb).andReturn();
String tr = r.getResponse().getContentAsString();
ObjectMapper mapper = new ObjectMapper();
String er = mapper.writeValueAsString(userList);

assertEquals(er, tr);
}

@Test
public void getUserById() throws Exception
{
String apiUrl = "/users/user/10";
Mockito.when(userService.findUserById(10)).thenReturn(userList.get(0));

RequestBuilder rb =
MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);
MvcResult r = mockMvc.perform(rb).andReturn();
String tr = r.getResponse().getContentAsString();
ObjectMapper mapper = new ObjectMapper();
String er = mapper.writeValueAsString(userList.get(0));

assertEquals(er, tr);
}

@Test
public void getUserByName() throws Exception
{
String apiUrl = "/users/user/name/cinnamon";
Mockito.when(userService.findByName("cinnamon")).thenReturn(userList.get(1));

RequestBuilder rb =
MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);
MvcResult r = mockMvc.perform(rb).andReturn();
String tr = r.getResponse().getContentAsString();
ObjectMapper mapper = new ObjectMapper();
String er = mapper.writeValueAsString(userList.get(1));

assertEquals(er, tr);
}

@Test
public void getUserLikeName() throws Exception
{
List<User> resList = userList.stream().filter(a ->
a.getUsername().contains("b")).collect(Collectors.toList());
String apiUrl = "/users/user/name/like/b";
Mockito.when(userService.findByNameContaining("b"))
.thenReturn(resList);

RequestBuilder rb =
MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);
MvcResult r = mockMvc.perform(rb).andReturn();
String tr = r.getResponse().getContentAsString();
ObjectMapper mapper = new ObjectMapper();
String er = mapper.writeValueAsString(resList);

assertEquals(er, tr);
}

@Test
public void addNewUser() throws Exception
{
String apiUrl = "/users/user";

User u9 = new User("testuser",
"password",
"test@lambdaschool.local");
u9.setUserid(90);

ObjectMapper mapper = new ObjectMapper();
String userString = mapper.writeValueAsString(u9);

Mockito.when(userService.save(any(User.class))).thenReturn(u9);

RequestBuilder rb =
MockMvcRequestBuilders.post(apiUrl).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(userString);
mockMvc.perform(rb).andExpect(status().isCreated()).andDo(MockMvcResultHandlers.print());
}

@Test
public void updateFullUser() throws Exception
{
// String apiUrl = "/users/user/10";
//
// User u9 = new User("testuser",
// "password",
// "test@lambdaschool.local");
//// u9.setUserid(90);
//
// ObjectMapper mapper = new ObjectMapper();
// String userString = mapper.writeValueAsString(u9);
//
// Mockito.when(userService.update(any(User.class),10)).thenReturn(u9);
//
// RequestBuilder rb =
// MockMvcRequestBuilders.patch(apiUrl).accept(MediaType.APPLICATION_JSON)
// .contentType(MediaType.APPLICATION_JSON)
// .content(userString);
// mockMvc.perform(rb).andExpect(status().isCreated()).andDo(MockMvcResultHandlers.print());
}

@Test
public void updateUser()
{
}

@Test
public void deleteUserById() throws Exception
{
// I can't get this to work, because userService.delete is a void
// method, and I can not figure out how to mock out a void method
// String apiUrl = "/users/user/10";
// Mockito.doReturn( userList.remove(0)).when(userService).delete(10);
//
// RequestBuilder rb =
// MockMvcRequestBuilders.delete(apiUrl).accept(MediaType.APPLICATION_JSON);
// MvcResult r = mockMvc.perform(rb).andReturn();
// String tr = r.getResponse().getContentAsString();
// ObjectMapper mapper = new ObjectMapper();
// String er = mapper.writeValueAsString(userList.get(0));
//
// assertEquals(0, userList.stream().filter(a ->
// ( a.getUserid() == 10 )).collect(Collectors.toList()).size());

String apiUrl = "/users/user/{userid}";

RequestBuilder rb = MockMvcRequestBuilders.delete(apiUrl, "10")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON);
mockMvc.perform(rb)
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.lambdaschool.usermodel.services;

import static org.junit.Assert.*;

import com.lambdaschool.usermodel.models.User;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UserServiceImplTest
{
@Autowired
private UserService userService;

@Before
public void setUp() throws Exception
{
MockitoAnnotations.initMocks(this);
List<User> myList = userService.findAll();
}

@After
public void tearDown() throws Exception
{
}

@Test
public void a_findUserById() throws Exception
{
assertEquals("admin", userService.findUserById(4).getUsername());
}

@Test
public void b_findByNameContaining()
{
assertEquals(1,
userService.findByNameContaining("miss").size());
}

@Test
public void c_findAll()
{
assertEquals(5, userService.findAll().size());
}

@Test
public void d_delete()
{
userService.delete(11);
assertEquals(4, userService.findAll().size());
}

@Test
public void e_findByName()
{
assertEquals(7, userService.findByName("cinnamon").getUserid());
}

@Test
public void f_save()
{
User nu = new User("testuser",
"password",
"test@lambdaschool.local");
User addUser = userService.save(nu);
assertNotNull(addUser);
assertEquals(nu.getUsername(), addUser.getUsername());

}

@Test
public void g_update()
{
}

@Test
public void h_deleteAll()
{
userService.deleteAll();
assertEquals(0, userService.findAll().size());
}
}
34 changes: 34 additions & 0 deletions usermodel-initial/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Configurations useful for working with H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
#
# We set a port that is not frequently used
server.port=${PORT:2019}
#
# Feature that determines what happens when no accessors are found for a type
# (and there are no annotations to indicate it is meant to be serialized).
spring.jackson.serialization.fail-on-empty-beans=false
#
# keeps a transaction inside of the same entity manager
# This property register an EntityManager to the current thread,
# so you will have the same EntityManager until the web request is finished.
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
#
# Good for production!
# spring.jpa.hibernate.ddl-auto=update
# since we have our data in SeedData, do not also load it from data.sql
# spring.datasource.initialization-mode=never
#
# Used to set the date format for JSON Output
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=America/Los_Angeles
#
# Turns off Spring Boot automatic exception handling
server.error.whitelabel.enabled=false
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false