Skip to content

Tests for User Model #7

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

Open
wants to merge 5 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
8 changes: 8 additions & 0 deletions usermodel-initial/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
<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
Expand Up @@ -13,23 +13,14 @@
import java.util.ArrayList;
import java.util.List;

/**
* Implements UserService Interface
*/
@Transactional
@Service(value = "userService")
public class UserServiceImpl
implements UserService
{
/**
* Connects this service to the User table.
*/
@Autowired
private UserRepository userrepos;

/**
* Connects this service to the Role table
*/
@Autowired
private RoleService roleService;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package com.lambdaschool.usermodel;

import com.github.javafaker.Faker;
import com.github.javafaker.service.FakeValuesService;
import com.github.javafaker.service.RandomService;
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.services.RoleService;
import com.lambdaschool.usermodel.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Locale;

/**
* SeedData puts both known and random data into the database. It implements CommandLineRunner.
* <p>
* CoomandLineRunner: Spring Boot automatically runs the run method once and only once
* after the application context has been loaded.
*/
@Transactional
@Component
public class SeedData
implements CommandLineRunner
{
/**
* Connects the Role Service to this process
*/
@Autowired
RoleService roleService;

/**
* Connects the user service to this process
*/
@Autowired
UserService userService;

/**
* Generates test, seed data for our application
* First a set of known data is seeded into our database.
* Second a random set of data using Java Faker is seeded into our database.
* Note this process does not remove data from the database. So if data exists in the database
* prior to running this process, that data remains in the database.
*
* @param args The parameter is required by the parent interface but is not used in this process.
*/
@Transactional
@Override
public void run(String[] args) throws
Exception
{
userService.deleteAll();
roleService.deleteAll();
Role r1 = new Role("admin");
Role r2 = new Role("user");
Role r3 = new Role("data");

r1 = roleService.save(r1);
r2 = roleService.save(r2);
r3 = roleService.save(r3);

// admin, data, user
User u1 = new User("admin",
"password",
"[email protected]");
u1.getRoles()
.add(new UserRoles(u1,
r1));
u1.getRoles()
.add(new UserRoles(u1,
r2));
u1.getRoles()
.add(new UserRoles(u1,
r3));
u1.getUseremails()
.add(new Useremail(u1,
"[email protected]"));
u1.getUseremails()
.add(new Useremail(u1,
"[email protected]"));

userService.save(u1);

// data, user
User u2 = new User("cinnamon",
"1234567",
"[email protected]");
u2.getRoles()
.add(new UserRoles(u2,
r2));
u2.getRoles()
.add(new UserRoles(u2,
r3));
u2.getUseremails()
.add(new Useremail(u2,
"[email protected]"));
u2.getUseremails()
.add(new Useremail(u2,
"[email protected]"));
u2.getUseremails()
.add(new Useremail(u2,
"[email protected]"));
userService.save(u2);

// user
User u3 = new User("barnbarn",
"ILuvM4th!",
"[email protected]");
u3.getRoles()
.add(new UserRoles(u3,
r2));
u3.getUseremails()
.add(new Useremail(u3,
"[email protected]"));
userService.save(u3);

User u4 = new User("puttat",
"password",
"[email protected]");
u4.getRoles()
.add(new UserRoles(u4,
r2));
userService.save(u4);

User u5 = new User("misskitty",
"password",
"[email protected]");
u5.getRoles()
.add(new UserRoles(u5,
r2));
userService.save(u5);

if (false)
{
// using JavaFaker create a bunch of regular users
// https://www.baeldung.com/java-faker
// https://www.baeldung.com/regular-expressions-java

FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-US"),
new RandomService());
Faker nameFaker = new Faker(new Locale("en-US"));

for (int i = 0; i < 25; i++)
{
new User();
User fakeUser;

fakeUser = new User(nameFaker.name()
.username(),
"password",
nameFaker.internet()
.emailAddress());
fakeUser.getRoles()
.add(new UserRoles(fakeUser,
r2));
fakeUser.getUseremails()
.add(new Useremail(fakeUser,
fakeValuesService.bothify("????##@gmail.com")));
userService.save(fakeUser);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com.lambdaschool.usermodel.controllers;

import com.lambdaschool.usermodel.UserModelApplication;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = UserModelApplication.class)
@AutoConfigureMockMvc
public class UserControllerIntegrationTest
{
@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;


@Before
public void setUp() throws Exception
{
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);

mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.build();
}

@After
public void tearDown() throws Exception
{
}

@Test
public void whenMeasuredResponseTime() throws
Exception
{
long time = System.currentTimeMillis();
this.mockMvc.perform(get("/users/users"))
.andDo(print());
long responseTime = (System.currentTimeMillis() - time);

assertTrue("timestamp",
(responseTime < 5000L));
}

@Test
public void getAllUsers() throws
Exception
{
this.mockMvc.perform(get("/users/users"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("cinnamon")));
}

@Test
public void getUserLikeName() throws
Exception
{
this.mockMvc.perform(get("/users/user/name/like/{userName}",
"kitty"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("misskitty")));
}

@Test
public void getUserById() throws
Exception
{
this.mockMvc.perform(get("/users/user/{userid}",
4))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("admin")));
}

@Test
public void getUserByIdNotFound() throws
Exception
{
this.mockMvc.perform(get("/users/user/{userid}",
100))
.andDo(print())
.andExpect(status().is4xxClientError())
.andExpect(content().string(containsString("ResourceNotFoundException")));
}

@Test
public void getUserByName() throws
Exception
{
this.mockMvc.perform(get("/users/user/name/{userName}",
"admin"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("admin")));
}

@Test
public void getUserByNameNotFound() throws
Exception
{
this.mockMvc.perform(get("/users/user/name/{userName}",
"rabbit"))
.andDo(print())
.andExpect(status().is4xxClientError())
.andExpect(content().string(containsString("ResourceNotFoundException")));
}

@Test
public void givenPostAUser() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.post("/users/user")
.content("{\"username\": \"Ginger\", \"password\": \"EATEATEAT\", \"primaryemail\" : \"[email protected]\"}")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(MockMvcResultMatchers.header()
.exists("location"));
}

@Test
public void givenPutAUser() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.put("/users/user/11")
.content("{\"username\": \"stumps\", \"password\": \"EATEATEAT\", \"primaryemail\" : \"[email protected]\"}")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}

@Test
public void deleteUserById() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.delete("/users/user/{id}",
13))
.andDo(print())
.andExpect(status().is2xxSuccessful());
}

@Test
public void deleteUserByIdNotFound() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.delete("/users/user/{id}",
100))
.andDo(print())
.andExpect(status().is4xxClientError());
}

@Test
public void UpdateUser() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.patch("/users/user/{userid}",
7)
.content("{\"password\": \"EATEATEAT\"}")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
}
Loading