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 -->

<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,158 @@
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",
"admin@lambdaschool.local");
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,
"admin@email.local"));
u1.getUseremails()
.add(new Useremail(u1,
"admin@mymail.local"));

userService.save(u1);

// data, user
User u2 = new User("cinnamon",
"1234567",
"cinnamon@lambdaschool.local");
u2.getRoles()
.add(new UserRoles(u2, r2));
u2.getRoles()
.add(new UserRoles(u2, r3));
u2.getUseremails()
.add(new Useremail(u2,
"cinnamon@mymail.local"));
u2.getUseremails()
.add(new Useremail(u2,
"hops@mymail.local"));
u2.getUseremails()
.add(new Useremail(u2,
"bunny@email.local"));
userService.save(u2);

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

User u4 = new User("puttat",
"password",
"puttat@school.lambda");
u4.getRoles()
.add(new UserRoles(u4, r2));
userService.save(u4);

User u5 = new User("misskitty",
"password",
"misskitty@school.lambda");
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,61 @@
package com.lambdaschool.usermodel.controllers;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class UserControllerTest
{

@Before
public void setUp() throws Exception
{
}

@After
public void tearDown() throws Exception
{
}

@Test
public void listAllUsers()
{
}

@Test
public void getUserById()
{
}

@Test
public void getUserByName()
{
}

@Test
public void getUserLikeName()
{
}

@Test
public void addNewUser()
{
}

@Test
public void updateFullUser()
{
}

@Test
public void updateUser()
{
}

@Test
public void deleteUserById()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.lambdaschool.usermodel.services;

import com.lambdaschool.usermodel.UserModelApplication;
import com.lambdaschool.usermodel.models.Role;
import com.lambdaschool.usermodel.models.User;
import com.lambdaschool.usermodel.models.UserRoles;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.test.context.junit4.SpringRunner;

import javax.persistence.EntityNotFoundException;

import static org.junit.Assert.*;

//use the database

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserModelApplication.class)
public class UserServiceImplTest
{
@Autowired
private UserService userService;

@Autowired
private RoleService roleService;

@Before
public void setUp() throws Exception
{
//mocks -> fake data
//stubs -> fake methods
//java-> mocks
MockitoAnnotations.initMocks(this);
}

@After
public void tearDown() throws Exception
{
}

@Test
public void findUserById()
{
assertEquals("cinnamon", userService.findUserById(7).getUsername());
}

@Test
public void findByNameContaining()
{
assertEquals(2, userService.findByNameContaining("tt").size());
}

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

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

@Test
public void findByName()
{
assertEquals(14, userService.findByName("misskitty").getUserid());
}

@Test
public void save()
{
String username = "happy";
Role r1 = roleService.findByName("admin");
Role r2 = roleService.findByName("user");

User user = new User("happy",
"password",
"happy@example.com");
user.getRoles().add(new UserRoles(user, r1));
user.getRoles().add(new UserRoles(user, r2));

User addUser = userService.save(user);
assertNotNull(addUser);
assertEquals(username, addUser.getUsername());
}

@Test
public void update()
{
String username = "happy";
Role r2 = roleService.findByName("user");

User user = new User("happy",
"password",
"happy@example.com");
user.getRoles().add(new UserRoles(user, r2));
user.setUserid(13);
User addUser = userService.update(user, 13);

assertNotNull(addUser);
assertEquals(username, addUser.getUsername());
}

@Test
public void 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