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
12 changes: 12 additions & 0 deletions school/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<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 @@ -4,10 +4,16 @@
import com.lambdaschool.school.service.CourseService;
import com.lambdaschool.school.view.CountStudentsInCourses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

@RestController
Expand Down Expand Up @@ -36,4 +42,21 @@ public ResponseEntity<?> deleteCourseById(@PathVariable long courseid)
courseService.delete(courseid);
return new ResponseEntity<>(HttpStatus.OK);
}

@PostMapping(value = "/course/add",
consumes = {"application/json"},
produces = {"application/json"})
public ResponseEntity<?> addNewCourse(@Valid
@RequestBody
Course newCourse) throws URISyntaxException
{
newCourse = courseService.save(newCourse);

// set the location header for the newly created resource
HttpHeaders responseHeaders = new HttpHeaders();
URI newRestaurantURI = ServletUriComponentsBuilder.fromCurrentRequest().path("/{courseid}").buildAndExpand(newCourse.getCourseid()).toUri();
responseHeaders.setLocation(newRestaurantURI);

return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public interface CourseService
ArrayList<CountStudentsInCourses> getCountStudentsInCourse();

void delete(long id);

Course findCourseById(long id);

Course save(Course course);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lambdaschool.school.service;

import com.lambdaschool.school.model.Course;
import com.lambdaschool.school.model.Student;
import com.lambdaschool.school.repository.CourseRepository;
import com.lambdaschool.school.view.CountStudentsInCourses;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -43,4 +44,30 @@ public void delete(long id) throws EntityNotFoundException
throw new EntityNotFoundException(Long.toString(id));
}
}

@Override
public Course findCourseById(long id) throws EntityNotFoundException
{
return courserepos.findById(id).orElseThrow(() -> new EntityNotFoundException(Long.toString(id)));

}

@Transactional
@Override
public Course save(Course course)
{
Course newCourse = new Course();

newCourse.setCoursename(course.getCoursename());
newCourse.setInstructor(course.getInstructor());

// ArrayList<Student> newStudent = new ArrayList<>();
// for (Student s : course.getStudents())
// {
// newStudent.add(new Student(s.getStudname()));
// }
// newCourse.setStudents(newStudent);

return courserepos.save(newCourse);
}
}
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 getInstructorById (long id);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
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;

import javax.persistence.EntityNotFoundException;

@Service(value = "instructorService")
public class InstructorServiceImpl implements InstructorService
{
@Autowired
private InstructorRepository instructrepos;

@Override
public Instructor getInstructorById(long id) throws EntityNotFoundException
{
return instructrepos.findById(id).orElseThrow(() -> new EntityNotFoundException(Long.toString(id)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.lambdaschool.school;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SchoolApplication
{

public static void main(String[] args)
{
SpringApplication.run(SchoolApplication.class, args);
}

}

This file was deleted.

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

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 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 io.restassured.module.mockmvc.RestAssuredMockMvc;
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 CourseControllerIntegrationTest
{
@Autowired
private WebApplicationContext webApplicationContext;

@Autowired
private InstructorService instructorService;
@Before
public void initialiseRestAssuredMockMvcWebApplicationContext()
{
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
}

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

@Test
public void givenPostACourse() throws Exception
{
Instructor sally = instructorService.getInstructorById(1);
Course c7 = new Course("Algebra", sally);

ObjectMapper mapper = new ObjectMapper();
String stringC7 = mapper.writeValueAsString(c7);
given().contentType("application/json").body(stringC7).when().post("/courses/course/add").then().statusCode(201);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.lambdaschool.school.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lambdaschool.school.model.Course;
import com.lambdaschool.school.model.Instructor;
import com.lambdaschool.school.model.Student;
import com.lambdaschool.school.service.CourseService;
import com.lambdaschool.school.service.InstructorService;
import com.lambdaschool.school.service.StudentService;
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 org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

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

@MockBean
private CourseService courseService;
@MockBean
private StudentService studentService;
@MockBean
private InstructorService instructorService;

private ArrayList<Course> courseList;


@Before
public void setUp()
{

courseList = new ArrayList<>();
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("Android", i3);
c6.setCourseid(6);
Student s1 = new Student("John");
s1.setStudid(1);
courseList.add(c1);
courseList.add(c2);
courseList.add(c3);
courseList.add(c4);
courseList.add(c5);
courseList.add(c6);
c1.getStudents().add(new Student("John"));
System.out.println(c1.getCourseid() + c1.getCoursename());

}
@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();
String tr = r.getResponse().getContentAsString();
ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
String er = mapper.writeValueAsString(courseList);

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

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

RequestBuilder rb = MockMvcRequestBuilders.delete(apiUrl).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
mockMvc.perform(rb).andExpect(status().isOk()).andDo(MockMvcResultHandlers.print());
}

@Test
public void addNewCourse() throws Exception
{
String apiUrl = "/courses/course/add";
Instructor sally = instructorService.getInstructorById(1);
Course c9 = new Course("Calculus", sally);
c9.setCourseid(99);
ObjectMapper mapper = new ObjectMapper();
String courseString = mapper.writeValueAsString(c9);
Mockito.when(courseService.save(any(Course.class))).thenReturn(c9);

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

}
Loading