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 curly.artifactory.web; import curly.artifactory.ArtifactoryTestHelper; import io.curly.artifact.model.Artifact; import io.curly.commons.web.hateoas.MediaTypes; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; import static org.hamcrest.Matchers.instanceOf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; /** * @author Joao Pedro Evangelista */ public class ArtifactResourceControllerTests extends ArtifactoryTestHelper { @Autowired private MongoTemplate mongoTemplate; @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; private Artifact artifact; private MappingJackson2HttpMessageConverter messageConverter; @Value("${server.port}") private Integer port; @Before public void setUp() throws Exception { this.messageConverter = new MappingJackson2HttpMessageConverter(); this.artifact = createArtifact(mongoTemplate); this.mockMvc = webAppContextSetup(webApplicationContext).alwaysExpect(status().is2xxSuccessful()).build(); } @After public void tearDown() throws Exception { mongoTemplate.findAllAndRemove(new Query(), Artifact.class); } @Test public void testArtifactResources() throws Exception { mongoTemplate.findAllAndRemove(new Query(), Artifact.class); mockMvc.perform(asyncDispatch(mockMvc.perform(get("/artifacts")).andExpect(request().asyncStarted()) .andExpect(request().asyncResult(instanceOf(ResponseEntity.class))).andReturn())); } @Test public void testArtifactResource() throws Exception { String id = artifact.getId(); Artifact byId = mongoTemplate.findById(id, Artifact.class); mockMvc.perform(asyncDispatch(mockMvc.perform(get("/artifacts/{id}", id)).andExpect(status().isOk()) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(instanceOf(ResponseEntity.class))).andReturn())) .andExpect(content().contentType(MediaTypes.HAL_JSON)).andExpect(jsonPath("$").exists()); } @Test public void testSaveResource() throws Exception { mockMvc.perform(asyncDispatch(mockMvc .perform(post("/artifacts").contentType(MediaType.APPLICATION_JSON) .content(json(artifact, messageConverter)).principal(user())) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(new ResponseEntity<>(HttpStatus.CREATED))).andReturn())); } @Test public void testDeleteResource() throws Exception { String id = artifact.getId(); mockMvc.perform( asyncDispatch(mockMvc.perform(delete("/artifacts/{id}", id).principal(user())).andReturn())); } }