Commit 1b1ea997 authored by Khalid Ali's avatar Khalid Ali
Browse files

Attempt to test the PUT request on the controller.

parent 71694bce
......@@ -40,7 +40,7 @@ public class ListingDto {
return dateFormat.parse(this.createDate);
}
public void setCreateDate(Date date) {
public void setCreateDateConverted(Date date) {
this.createDate = dateFormat.format(date);
}
}
......@@ -73,7 +73,7 @@ public class BookshareApiController {
private ListingDto convertToDto(ListingEntity listingEntity) {
ListingDto listingDto = modelMapper.map(listingEntity, ListingDto.class);
listingDto.setCreateDate(listingEntity.getCreateDate());
listingDto.setCreateDateConverted(listingEntity.getCreateDate());
return listingDto;
}
......
package com.gmu.bookshare;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.gmu.bookshare.entity.ListingEntity;
import com.gmu.bookshare.model.ListingDto;
import com.gmu.bookshare.service.BidService;
import com.gmu.bookshare.service.ListingService;
import com.gmu.bookshare.service.ShareUserService;
import com.gmu.bookshare.web.BookshareApiController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
......@@ -22,6 +27,7 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
......@@ -32,6 +38,9 @@ public class BookshareRestControllerIntegrationTest {
@Autowired
private MockMvc mvc;
@Autowired
private ModelMapper modelMapper;
@MockBean
private ListingService listingService;
......@@ -58,4 +67,54 @@ public class BookshareRestControllerIntegrationTest {
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].isbn", is(alex.getIsbn())));
}
@Test
public void givenListing_whenPutListings_thenReturnCorrectHttpStatus()
throws Exception {
ListingEntity listingInDB = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
ListingEntity listingNotInDB = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
ListingDto listingDtoInDB = modelMapper.map(listingInDB, ListingDto.class);
listingDtoInDB.setCreateDateConverted(listingInDB.getCreateDate());
ListingDto listingDtoNotInDB = modelMapper.map(listingNotInDB, ListingDto.class);
listingDtoNotInDB.setCreateDateConverted(listingNotInDB.getCreateDate());
// ListingDto listingDtoInDB = new ListingDto();
// listingDtoInDB.setIsbn(123456);
// listingDtoInDB.setCondition(3);
// listingDtoInDB.setPrice(14.99);
// listingDtoInDB.setCreateDateConverted(new Date());
// listingDtoInDB.setTitle("Title Calc 3");
// listingDtoInDB.setId(listingInDB.getId());
//
// ListingDto listingDtoNotInDB = new ListingDto();
// listingDtoInDB.setIsbn(123456);
// listingDtoInDB.setCondition(3);
// listingDtoInDB.setPrice(14.99);
// listingDtoInDB.setCreateDateConverted(new Date());
// listingDtoInDB.setTitle("Title Calc 3");
// listingDtoInDB.setId(listingNotInDB.getId());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String requestJson = ow.writeValueAsString(listingDtoInDB);
String requestJson2 = ow.writeValueAsString(listingDtoNotInDB);
given(listingService.updateListing(listingInDB)).willReturn(listingInDB);
given(listingService.updateListing(listingNotInDB)).willReturn(null);
mvc.perform(put("/bs/api/listing/" + listingInDB.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(requestJson))
.andExpect(status().isOk());
mvc.perform(put("/bs/api/listing/" + listingNotInDB.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(requestJson2))
.andExpect(status().isNotFound());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment