io.curly.tagger.TagControllerTests.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.tagger.TagControllerTests.java

Source

/*
 *        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.tagger;

import io.curly.tagger.model.Tag;
import org.junit.After;
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.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
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.security.oauth2.client.test.OAuth2ContextConfiguration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
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 java.util.Set;

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;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Tests for {@link io.curly.tagger.controller.TagController}
 *
 * @author Joo Evangelista
 */
@DirtiesContext
@RunWith(SpringJUnit4ClassRunner.class)
@OAuth2ContextConfiguration
@SpringApplicationConfiguration(classes = { TaggerApplication.class })
@WebIntegrationTest
public class TagControllerTests extends TagHelper {

    private Tag tag;
    private MockMvc mockMvc;
    @Autowired
    private WebApplicationContext webApplicationContext;
    @Autowired
    private MongoTemplate mongoTemplate;

    private static String withControllerPath(String fragment) {
        return "/tags" + fragment;
    }

    @Before
    public void setUp() throws Exception {
        this.tag = createTag(mongoTemplate);
        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).alwaysDo(print())
                .alwaysExpect(status().is2xxSuccessful()).build();
    }

    @After
    public void tearDown() throws Exception {
        mongoTemplate.findAllAndRemove(new Query(), Tag.class);
    }

    @Test
    public void testSave() throws Exception {
        Set<Tag> tagSet = new HashSet<>(1);
        tagSet.add(new Tag(null, "something"));

        mockMvc.perform(asyncDispatch(mockMvc
                .perform(post("/tags").content(json(tagSet, new MappingJackson2HttpMessageConverter()))
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(request().asyncStarted())
                .andExpect(request().asyncResult(new ResponseEntity<>(HttpStatus.CREATED))).andReturn()));

    }

    @Test
    public void testGet() throws Exception {
        mockMvc.perform(asyncDispatch(
                mockMvc.perform(get(withControllerPath("/{tag}"), "love")).andExpect(request().asyncStarted())
                        .andExpect(request().asyncResult(ResponseEntity.ok(tag))).andReturn()));
        //.andExpect(content().contentType(jsonMediaType()))
        //.andExpect(content().json(json(tag, new MappingJackson2HttpMessageConverter())));
    }

    @Test
    public void testSearch() throws Exception {
        mockMvc.perform(asyncDispatch(mockMvc.perform(get(withControllerPath("/search")).param("q", "lov"))
                .andExpect(request().asyncStarted()).andReturn()));

    }
}