Search in sources :

Example 1 with Car

use of de.spring.webservices.domain.Car in project JavaForFun by gumartinm.

the class CarControllerIntegrationTest method testWhenCreateNewCarThenRetrieveJsonValue.

@Test
public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
    Car car = new Car(null, "nothing");
    given(awesomeBusinessLogic.create(car)).willReturn(new Car(1L, String.format(TEMPLATE, 1)));
    mockMvc.perform(post("/api/cars/").contentType(MediaType.APPLICATION_JSON_UTF8).content(asJsonString(car)).accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(status().isCreated()).andExpect(jsonPath("id", any(Integer.class))).andExpect(jsonPath("content", is("Car: 1"))).andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1")).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
}
Also used : Car(de.spring.webservices.domain.Car) Test(org.junit.Test)

Example 2 with Car

use of de.spring.webservices.domain.Car in project JavaForFun by gumartinm.

the class CarControllerIntegrationTest method testWhenGetAllCarsThenRetrieveJsonValues.

@Test
public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
    final List<Car> cars = new ArrayList<>();
    cars.add(new Car(1L, String.format(TEMPLATE, 1)));
    given(awesomeBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))).willReturn(new PageImpl<>(cars));
    mockMvc.perform(get("/api/cars/").accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(jsonPath("$.content[0].id", any(Integer.class))).andExpect(jsonPath("$.content[0].content", is("Car: 1"))).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Car(de.spring.webservices.domain.Car) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with Car

use of de.spring.webservices.domain.Car in project JavaForFun by gumartinm.

the class CompletableFutureCarControllerIntegrationTest method testWhenGetAllCarsThenRetrieveJsonValues.

@Test
public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
    final List<Car> cars = new ArrayList<>();
    cars.add(new Car(1L, String.format(TEMPLATE, 1)));
    CompletableFuture<Page<Car>> future = CompletableFuture.supplyAsync(() -> new PageImpl<>(cars));
    given(completableFutureBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))).willReturn(future);
    MvcResult result = mockMvc.perform(get("/api/completablefuture/cars/").accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(request().asyncStarted()).andExpect(request().asyncResult(instanceOf(Page.class))).andReturn();
    mockMvc.perform(asyncDispatch(result)).andExpect(status().isOk()).andExpect(jsonPath("$.content[0].id", any(Integer.class))).andExpect(jsonPath("$.content[0].id", any(Integer.class))).andExpect(jsonPath("$.content[0].content", is("Car: 1"))).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Car(de.spring.webservices.domain.Car) ArrayList(java.util.ArrayList) Page(org.springframework.data.domain.Page) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test)

Example 4 with Car

use of de.spring.webservices.domain.Car in project JavaForFun by gumartinm.

the class CompletableFutureCarControllerIntegrationTest method testWhenGetOneCarThenRetrieveJsonValue.

@Test
public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
    CompletableFuture<Car> expected = CompletableFuture.supplyAsync(() -> new Car(1L, String.format(TEMPLATE, 1)));
    given(completableFutureBusinessLogic.findById(1L)).willReturn(expected);
    MvcResult result = mockMvc.perform(get("/api/completablefuture/cars/{id}", 1L).accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(request().asyncStarted()).andExpect(request().asyncResult(instanceOf(Car.class))).andReturn();
    mockMvc.perform(asyncDispatch(result)).andExpect(status().isOk()).andExpect(jsonPath("id", any(Integer.class))).andExpect(jsonPath("content", is("Car: 1"))).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
}
Also used : Car(de.spring.webservices.domain.Car) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test)

Example 5 with Car

use of de.spring.webservices.domain.Car in project JavaForFun by gumartinm.

the class RxJavaCarControllerIntegrationTest method testWhenCreateNewCarThenRetrieveJsonValue.

// THIS GUY IS USING MY de.spring.webservices.rest.controller.adapters.RxJavaAdapter AND IT DOES NOT NEED to call onCompleted()
// I DO NOT THINK MY de.spring.webservices.rest.controller.adapters.RxJavaAdapter SHOULD BE USED. YOU'D BETTER USE THE spring netflix IMPLEMENTATION :/
@Test
public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
    Car car = new Car(null, "nothing");
    Observable<Car> observable = Observable.create(observer -> observer.onNext(new Car(1L, String.format(TEMPLATE, 1))));
    given(rxJavaBusinessLogic.createThrowable(car)).willReturn(observable);
    MvcResult result = mockMvc.perform(post("/api/rxjava/cars/").contentType(MediaType.APPLICATION_JSON_UTF8).content(asJsonString(car)).accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(request().asyncStarted()).andExpect(request().asyncResult(instanceOf(ResponseEntity.class))).andReturn();
    mockMvc.perform(asyncDispatch(result)).andExpect(status().isCreated()).andExpect(jsonPath("id", any(Integer.class))).andExpect(jsonPath("content", is("Car: 1"))).andExpect(header().string(HttpHeaders.LOCATION, "/api/rxjava/cars/1")).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Car(de.spring.webservices.domain.Car) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test)

Aggregations

Car (de.spring.webservices.domain.Car)12 Test (org.junit.Test)9 MvcResult (org.springframework.test.web.servlet.MvcResult)6 ArrayList (java.util.ArrayList)5 ResponseEntity (org.springframework.http.ResponseEntity)5 PageRequest (org.springframework.data.domain.PageRequest)3 Page (org.springframework.data.domain.Page)2 PageImpl (org.springframework.data.domain.PageImpl)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 HttpHeaders (org.springframework.http.HttpHeaders)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1