spring-boot-realworld-examp.../src/test/java/io/spring/api/ProfileApiTest.java

89 lines
3.5 KiB
Java
Raw Normal View History

2017-08-16 14:58:51 +07:00
package io.spring.api;
2017-08-24 09:39:09 +07:00
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import io.spring.JacksonCustomizations;
import io.spring.api.security.WebSecurityConfig;
2017-08-18 16:08:27 +07:00
import io.spring.application.ProfileQueryService;
2017-08-24 09:39:09 +07:00
import io.spring.application.data.ProfileData;
2017-08-16 16:13:24 +07:00
import io.spring.core.user.FollowRelation;
2017-08-16 14:58:51 +07:00
import io.spring.core.user.User;
import org.junit.Before;
import org.junit.Test;
2017-08-24 09:39:09 +07:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
2017-08-16 14:58:51 +07:00
import org.springframework.boot.test.mock.mockito.MockBean;
2017-08-24 09:39:09 +07:00
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
2017-08-16 14:58:51 +07:00
import java.util.Optional;
2017-08-24 09:39:09 +07:00
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
2017-08-16 14:58:51 +07:00
import static org.hamcrest.core.IsEqual.equalTo;
2018-05-10 13:50:44 +07:00
import static org.mockito.ArgumentMatchers.eq;
2017-08-16 16:13:24 +07:00
import static org.mockito.Mockito.verify;
2017-08-16 14:58:51 +07:00
import static org.mockito.Mockito.when;
2017-08-24 09:39:09 +07:00
@WebMvcTest(ProfileApi.class)
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
2017-08-16 14:58:51 +07:00
public class ProfileApiTest extends TestWithCurrentUser {
private User anotherUser;
2017-08-24 09:39:09 +07:00
@Autowired
private MockMvc mvc;
2017-08-16 14:58:51 +07:00
@MockBean
private ProfileQueryService profileQueryService;
2017-08-16 15:25:08 +07:00
2017-08-16 14:58:51 +07:00
private ProfileData profileData;
@Before
public void setUp() throws Exception {
2017-08-24 09:39:09 +07:00
super.setUp();
RestAssuredMockMvc.mockMvc(mvc);
2017-08-16 15:25:08 +07:00
anotherUser = new User("username@test.com", "username", "123", "", "");
profileData = new ProfileData(anotherUser.getId(), anotherUser.getUsername(), anotherUser.getBio(), anotherUser.getImage(), false);
2017-08-16 16:13:24 +07:00
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
2017-08-16 14:58:51 +07:00
}
@Test
public void should_get_user_profile_success() throws Exception {
when(profileQueryService.findByUsername(eq(profileData.getUsername()), eq(null)))
.thenReturn(Optional.of(profileData));
2017-08-24 09:39:09 +07:00
RestAssuredMockMvc.when()
2017-08-16 14:58:51 +07:00
.get("/profiles/{username}", profileData.getUsername())
.prettyPeek()
.then()
.statusCode(200)
.body("profile.username", equalTo(profileData.getUsername()));
}
2017-08-16 15:25:08 +07:00
@Test
public void should_follow_user_success() throws Exception {
when(profileQueryService.findByUsername(eq(profileData.getUsername()), eq(user))).thenReturn(Optional.of(profileData));
given()
.header("Authorization", "Token " + token)
.when()
.post("/profiles/{username}/follow", anotherUser.getUsername())
.prettyPeek()
.then()
.statusCode(200);
2017-08-16 16:13:24 +07:00
verify(userRepository).saveRelation(new FollowRelation(user.getId(), anotherUser.getId()));
}
@Test
public void should_unfollow_user_success() throws Exception {
FollowRelation followRelation = new FollowRelation(user.getId(), anotherUser.getId());
when(userRepository.findRelation(eq(user.getId()), eq(anotherUser.getId()))).thenReturn(Optional.of(followRelation));
when(profileQueryService.findByUsername(eq(profileData.getUsername()), eq(user))).thenReturn(Optional.of(profileData));
given()
.header("Authorization", "Token " + token)
.when()
.delete("/profiles/{username}/follow", anotherUser.getUsername())
.prettyPeek()
.then()
.statusCode(200);
2017-08-16 15:25:08 +07:00
2017-08-16 16:13:24 +07:00
verify(userRepository).removeRelation(eq(followRelation));
2017-08-16 15:25:08 +07:00
}
2017-08-16 14:58:51 +07:00
}