save follow

This commit is contained in:
aisensiy
2017-08-16 16:25:08 +08:00
parent 13571f2f5e
commit daaf2070c7
8 changed files with 98 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ import io.spring.application.profile.ProfileData;
import io.spring.application.profile.ProfileQueryService;
import io.spring.core.article.Article;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -15,6 +16,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
@@ -29,13 +31,15 @@ public class ProfileApiTest extends TestWithCurrentUser {
@MockBean
private ProfileQueryService profileQueryService;
private ProfileData profileData;
@Before
public void setUp() throws Exception {
RestAssured.port = port;
userFixture();
profileData = new ProfileData("id", "username", "bio", "img", false);
anotherUser = new User("username@test.com", "username", "123", "", "");
profileData = new ProfileData(anotherUser.getId(), anotherUser.getUsername(), anotherUser.getBio(), anotherUser.getImage(), false);
}
@Test
@@ -49,4 +53,18 @@ public class ProfileApiTest extends TestWithCurrentUser {
.statusCode(200)
.body("profile.username", equalTo(profileData.getUsername()));
}
@Test
public void should_follow_user_success() throws Exception {
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
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);
}
}

View File

@@ -1,5 +1,6 @@
package io.spring.infrastructure.user;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import org.junit.Before;
@@ -54,4 +55,14 @@ public class MyBatisUserRepositoryTest {
assertThat(optional.get().getUsername(), is(newUsername));
assertThat(optional.get().getImage(), is(user.getImage()));
}
@Test
public void should_create_new_user_follow_success() throws Exception {
User other = new User("other@example.com", "other", "123", "", "");
userRepository.save(other);
FollowRelation followRelation = new FollowRelation(user.getId(), other.getId());
userRepository.saveRelation(followRelation);
assertThat(userRepository.findRelation(user.getId(), other.getId()).isPresent(), is(true));
}
}