Java tutorial
/* * Copyright 2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.curly.gathering.item; import com.fasterxml.jackson.databind.ObjectMapper; import io.curly.commons.github.User; import io.curly.gathering.GatheringApplication; import io.curly.gathering.GatheringListRepository; import io.curly.gathering.list.GatheringList; import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.WebIntegrationTest; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.rules.SpringClassRule; import org.springframework.test.context.junit4.rules.SpringMethodRule; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import java.util.HashSet; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; /** * @author Joao Pedro Evangelista */ @SpringApplicationConfiguration(classes = GatheringApplication.class) @WebIntegrationTest public class ItemControllerTests { @ClassRule public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule(); @Rule public final SpringMethodRule SPRING_METHOD_RULE = new SpringMethodRule(); @Autowired private WebApplicationContext context; @Autowired private GatheringListRepository repository; private MockMvc mvc; private GatheringList list; private AddableItemBody body; @Before public void setUp() throws Exception { this.mvc = MockMvcBuilders.webAppContextSetup(context).alwaysDo(print()).build(); this.body = new AddableItemBody(); this.body.setItem("abc123"); this.list = new GatheringList(null, "6969", "Awful list", new HashSet<>(), new HashSet<>()); this.repository.save(list); } @Test public void testAddItem() throws Exception { this.mvc.perform(asyncDispatch(this.mvc .perform(post("/lists/{listId}/add/artifact", this.list.getId()) .principal(User.builder().id("6969").build()).accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) .content(new ObjectMapper().writeValueAsBytes(body))) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(new ResponseEntity<>(HttpStatus.CREATED))).andReturn())); } @Test public void testRemoveItem() throws Exception { this.mvc.perform( asyncDispatch(this.mvc .perform(delete("/lists/{listId}/delete/artifact/{artifact}", this.list.getId(), this.body.getItem()).principal(User.builder().id("6969").build())) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(new ResponseEntity<>(HttpStatus.NO_CONTENT))) .andReturn())); } }