aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
blob: 1dec4b7cb6ae2433032abce9705b5b42dd48b192 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.juick.api.tests;

import com.juick.Message;
import com.juick.Tag;
import com.juick.User;
import com.juick.api.configuration.ApiAppConfiguration;
import com.juick.api.configuration.ApiMvcConfiguration;
import com.juick.api.configuration.ApiSecurityConfig;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.inject.Inject;
import java.util.Collections;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Created by vitalyster on 25.11.2016.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class MessagesTests {
    @Configuration
    @Import(value={ApiMvcConfiguration.class, ApiAppConfiguration.class, ApiSecurityConfig.class})
    static class Config {
        @Bean
        @Primary
        MessagesService messagesService() {
            return Mockito.mock(MessagesService.class);
        }
        @Bean
        @Primary
        UserService userService() {
            return Mockito.mock(UserService.class);
        }
    }

    private MockMvc mockMvc;
    @Inject
    WebApplicationContext webApplicationContext;

    @Inject
    MessagesService messagesService;
    @Inject
    UserService userService;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .build();
    }

    @Test
    public void messageTests() throws Exception {
        String ugnichName = "ugnich";
        String msgText = "Привет, я - Угнич";
        User user = new User();
        user.setName(ugnichName);
        user.setUid(1);
        Message msg = new Message();
        msg.setMid(1);
        msg.setUser(user);
        msg.setText(msgText);
        msg.setTags(Collections.singletonList(new Tag("yo")));
        mockMvc.perform(get("/home"))
                .andExpect(status().is4xxClientError());
        when(userService.getUIDByHttpAuth(null))
                .thenReturn(1);

        when(messagesService.getMyFeed(1, 0))
                .thenReturn(Collections.singletonList(1));
        when(messagesService.getMessages(Collections.singletonList(1)))
                .thenReturn(Collections.singletonList(msg));

        mockMvc.perform(get("/home"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$[0].mid", is(1)))
                .andExpect(jsonPath("$[0].body", is(msgText)));
    }
}