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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2345,13 +2345,13 @@ Status 201 Created

## Instructions

* [ ] Please fork and clone this repository.
* [ ] This repository does have a starter project, so you must start with that application inside of the cloned repository folder. Regularly commit and push your code as appropriate.
* [x] Please fork and clone this repository.
* [x] This repository does have a starter project, so you must start with that application inside of the cloned repository folder. Regularly commit and push your code as appropriate.

### MVP

* [ ] Provide unit testing for the User Service. To reach MVP you must provide at least 75% coverage of the code in User Service as reported in IntelliJ
* [ ] Provide unit testing for the User Controller. To reach MVP you must provide at least 75% coverage of the code in User Controller as reported in IntelliJ
* [x] Provide unit testing for the User Service. To reach MVP you must provide at least 75% coverage of the code in User Service as reported in IntelliJ
* [x] Provide unit testing for the User Controller. To reach MVP you must provide at least 75% coverage of the code in User Controller as reported in IntelliJ

### Stretch Goal

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lambdaschool.usermodel.services;

import com.lambdaschool.usermodel.exceptions.ResourceFoundException;
import com.lambdaschool.usermodel.models.User;

import java.util.List;
Expand Down Expand Up @@ -34,7 +35,8 @@ public interface UserService
* @param id The primary key (long) of the user you seek.
* @return The given User or throws an exception if not found.
*/
User findUserById(long id);
User findUserById(long id) throws ResourceFoundException;


/**
* Returns the user with the given name
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("adminTEST",
"password",
"adminTEST@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,
"adminTEST@email.local"));
u1.getUseremails()
.add(new Useremail(u1,
"adminTEST@mymail.local"));

userService.save(u1);

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

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

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

User u5 = new User("misskittyTEST",
"password",
"misskittyTEST@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
@@ -1,52 +1,24 @@
package com.lambdaschool.usermodel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

/**
* Starting class for testing
* Main class to start the application.
*/
@EnableWebMvc
// @EnableJpaAuditing
//@EnableJpaAuditing
@SpringBootApplication
public class UserModelApplication
{
private static final Logger logger = LoggerFactory.getLogger(com.lambdaschool.usermodel.UserModelApplication.class);

private static boolean stop = false;

@Autowired
private static Environment env;

private static void checkEnvironmentVariable(String envvar)
{
if (System.getenv(envvar) == null)
{
logger.error("Environment Variable " + envvar + " missing");
stop = true;
}
}

/**
* Main method to start the application.
*
* @param args Not used in this application.
*/
public static void main(String[] args)
{
checkEnvironmentVariable("OAUTHCLIENTID");
checkEnvironmentVariable("OAUTHCLIENTSECRET");

if (!stop)
{
ApplicationContext ctx = SpringApplication.run(com.lambdaschool.usermodel.UserModelApplication.class,
args);

DispatcherServlet dispatcherServlet = (DispatcherServlet) ctx.getBean("dispatcherServlet");
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
}
SpringApplication.run(UserModelApplication.class,
args);
}

}
Loading