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
40 changes: 35 additions & 5 deletions school/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,50 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!-- <scope>runtime</scope>-->
<!-- <scope>runtime</scope>-->
</dependency>

<!-- PostgreSQL -->
<!-- <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
<!-- <artifactId>postgresql</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
<!-- <artifactId>postgresql</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>

<!-- 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 @@ -12,4 +12,6 @@ public interface CourseService
ArrayList<CountStudentsInCourses> getCountStudentsInCourse();

void delete(long id);

Course save(Course course);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public void delete(long id) throws EntityNotFoundException
throw new EntityNotFoundException(Long.toString(id));
}
}

@Override
public Course save(Course course) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.lambdaschool.school.service;

import com.lambdaschool.school.model.Instructor;

public interface InstructorService
{
Instructor findInstructorById(long id);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lambdaschool.school.service;

import com.lambdaschool.school.model.Instructor;
import com.lambdaschool.school.repository.InstructorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -9,4 +10,9 @@ public class InstructorServiceImpl implements InstructorService
{
@Autowired
private InstructorRepository instructrepos;

@Override
public Instructor findInstructorById(long id) {
return null;
}
}
86 changes: 86 additions & 0 deletions school/src/test/java/com/lambdaschool/school/SeedData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.lambdaschool.school;

import com.lambdaschool.school.model.Course;
import com.lambdaschool.school.model.Instructor;
import com.lambdaschool.school.model.Student;
import com.lambdaschool.school.repository.CourseRepository;
import com.lambdaschool.school.repository.InstructorRepository;
import com.lambdaschool.school.repository.StudentRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Component
public class SeedData implements CommandLineRunner {
private CourseRepository courserepos;
private StudentRepository studentrepos;
private InstructorRepository instructorrepos;

public SeedData(CourseRepository courserepos, StudentRepository studentrepos, InstructorRepository instructorrepos) {
this.courserepos = courserepos;
this.studentrepos = studentrepos;
this.instructorrepos = instructorrepos;
}

@Override
public void run(String[] args) throws Exception {
Instructor i1 = new Instructor("Sally");
Instructor i2 = new Instructor("Lucy");
Instructor i3 = new Instructor("Charlie");

Student s1 = new Student("John");
Student s2 = new Student("Julian");
Student s3 = new Student("Mary");
Student s4 = new Student("Julian");
Student s5 = new Student("Tyler");
Student s6 = new Student("Kim");
Student s7 = new Student("Juan");
Student s8 = new Student("Robby");
Student s9 = new Student("Roberto");
Student s10 = new Student("Bob");
Student s11 = new Student("Liz");
Student s12 = new Student("June");
Student s13 = new Student("April");

Course c1 = new Course("Data Science", i1);
Course c2 = new Course("JavaScript", i1);
Course c3 = new Course("Node.js", i1);
Course c4 = new Course("Java Back End", i2);
Course c5 = new Course("Mobile IOS", i2);
Course c6 = new Course("Mobile Android", i3);

s1.getCourses().add(c1);
s1.getCourses().add(c4);
s2.getCourses().add(c2);
s3.getCourses().add(c3);
s3.getCourses().add(c1);
s3.getCourses().add(c6);


instructorrepos.save(i1);
instructorrepos.save(i2);
instructorrepos.save(i3);

studentrepos.save(s1);
studentrepos.save(s2);
studentrepos.save(s3);
studentrepos.save(s4);
studentrepos.save(s5);
studentrepos.save(s6);
studentrepos.save(s7);
studentrepos.save(s8);
studentrepos.save(s9);
studentrepos.save(s10);
studentrepos.save(s11);
studentrepos.save(s12);
studentrepos.save(s13);

courserepos.save(c1);
courserepos.save(c2);
courserepos.save(c3);
courserepos.save(c4);
courserepos.save(c5);
courserepos.save(c6);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.lambdaschool.school.controllers;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lambdaschool.school.model.Course;
import com.lambdaschool.school.model.Instructor;
import com.lambdaschool.school.service.InstructorService;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
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.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
import static org.hamcrest.number.OrderingComparison.lessThan;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CourseControllerIntegrateTest {
@Autowired
private WebApplicationContext webApplicationContext;

@Autowired
private InstructorService instructorService;

@Before
public void initialiseRestAssuredMockMvcWebApplicationContext() {
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
}

@Test
public void courseAllResponseTime() {
given().when().get("/courses/courses").then().time(lessThan(3000L));
}

@Test
public void givenPostACourse() throws Exception {
Instructor s1 = instructorService.findInstructorById(1);
Course c7 = new Course("Java Fundamentals", s1);


ObjectMapper mapper = new ObjectMapper();
String stringC7 = mapper.writeValueAsString(c7);

given().contentType("application/json").body(stringC7).when().post("/courses/course/add").then().statusCode(201);
System.out.println("C7String " + stringC7);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.lambdaschool.school.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lambdaschool.school.controller.CourseController;
import com.lambdaschool.school.model.Course;
import com.lambdaschool.school.model.Instructor;
import com.lambdaschool.school.service.CourseService;
import org.junit.After;
import org.junit.Before;
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 static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebMvcTest(value = CourseController.class,
secure = false)
public class CourseControllerUnitTest {
@Autowired
private MockMvc mockMvc;

@MockBean
private CourseService courseService;

private ArrayList<Course> courseList = new ArrayList<>();

private ArrayList<Instructor> instructorList = new ArrayList<>();

@Before
public void setUp() throws Exception {


Instructor i1 = new Instructor("Sally");
i1.setInstructid(1);
Instructor i2 = new Instructor("Lucy");
i2.setInstructid(2);
Instructor i3 = new Instructor("Charlie");
i3.setInstructid(3);

Course c1 = new Course("Data Science", i1);
c1.setCourseid(1);
Course c2 = new Course("JavaScript", i1);
c2.setCourseid(2);
Course c3 = new Course("Node.js", i1);
c3.setCourseid(3);
Course c4 = new Course("Java Back End", i2);
c4.setCourseid(4);
Course c5 = new Course("Mobile IOS", i2);
c5.setCourseid(5);
Course c6 = new Course("Mobile Android", i3);
c6.setCourseid(6);

instructorList.add(i1);

courseList.add(c1);
courseList.add(c2);
courseList.add(c3);
courseList.add(c4);
courseList.add(c5);
courseList.add(c6);


}

@After
public void tearDown() throws Exception {
}

@Test
public void listAllCourses() throws Exception {
String apiUrl = "/courses/courses";

Mockito.when(courseService.findAll()).thenReturn(courseList);

RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);
MvcResult r = mockMvc.perform(rb).andReturn(); // this could throw an exception
String tr = r.getResponse().getContentAsString();

ObjectMapper mapper = new ObjectMapper();
String er = mapper.writeValueAsString(courseList);

System.out.println("Expected" + tr);
System.out.println("Actual " + er);

assertEquals("Rest API Returns List", er, tr);
}

@Test
public void addNewCourse() throws Exception {
String apiUrl = "/courses/course/add";

Instructor s1 = instructorList.get(0);
Course c7 = new Course("Java Fundamentals", s1);

ObjectMapper mapper = new ObjectMapper();
String c7String = mapper.writeValueAsString(c7);

Mockito.when(courseService.save(any(Course.class))).thenReturn(c7);

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

System.out.println("C7 Sting" + c7String);
System.out.println("Instructor " + s1);
System.out.println("Request Builder" + rb);
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.lambdaschool.school.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SchoolApplicationTests {

@Test
public void contextLoads() {
}

}