diff --git a/src/main/java/io/codechobo/member/.gitkeep b/src/main/java/io/codechobo/member/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/io/codechobo/member/application/.gitkeep b/src/main/java/io/codechobo/member/application/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/io/codechobo/member/application/MemberService.java b/src/main/java/io/codechobo/member/application/MemberService.java index bc6d10b..2551389 100644 --- a/src/main/java/io/codechobo/member/application/MemberService.java +++ b/src/main/java/io/codechobo/member/application/MemberService.java @@ -2,74 +2,108 @@ import io.codechobo.member.domain.Member; import io.codechobo.member.domain.PointPerLevel; +import io.codechobo.member.domain.Social; import io.codechobo.member.domain.repository.MemberRepository; import io.codechobo.member.domain.support.MemberDto; +import io.codechobo.member.domain.util.EntityDtoConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import javax.persistence.EntityNotFoundException; import javax.validation.constraints.NotNull; +import java.util.ArrayList; import java.util.Calendar; import java.util.List; -import java.util.stream.Collectors; +import java.util.Objects; /** * @author loustler * @since 10/02/2016 10:12 */ @Service +@Transactional public class MemberService { @Autowired private MemberRepository memberRepository; + /** + * Get mebmer list. + * + * @return MemberDto list + */ public List getMembers() { List member = memberRepository.findAll(); - return member.stream() - .map(m -> new MemberDto(m.getSeq(), m.getId(), m.getPassword(), m.getEmail(), m.getNickName(), m.getRegistrationDate(), m.getPoint())) - .collect(Collectors.toList()); + return EntityDtoConverter.memberListConvertToDtoList(member); } + /** + * Get one Member using member's sequence. + * + * @param sequence + * @return Null | MemberDto + */ public MemberDto getMember(final Long sequence) { - Member member = memberRepository.findOne(sequence); + Member member = memberRepository.findOne(Objects.requireNonNull(sequence)); - if(member == null) return null; + if(Objects.isNull(member)) return null; - return new MemberDto(member.getSeq(), member.getId(), member.getPassword(), member.getEmail(), member.getNickName(), member.getRegistrationDate(), member.getPoint()); + return EntityDtoConverter.memberConvertToDto(member); } + /** + * Create member using memberDTO. + * + * @param memberDto + * @return MemberDto + */ public MemberDto createMember(final MemberDto memberDto) { - Member member = new Member(memberDto); + Member member = new Member(Objects.requireNonNull(memberDto)); - if(member.getPoint() == null) + if(Objects.isNull(member.getPoint())) member.setPoint(new Integer(0)); - if(member.getRegistrationDate() == null) + if(Objects.isNull(member.getRegistrationDate())) member.setRegistrationDate(Calendar.getInstance().getTime()); member.setLevel(PointPerLevel.valueOf(member.getPoint())); - member = memberRepository.save(member); + member.setSocials(new ArrayList()); - return new MemberDto(member.getSeq(), member.getId(), member.getPassword(), member.getEmail(), member.getNickName(), member.getRegistrationDate(), member.getPoint()); + Member createdMember = memberRepository.save(member); + + return EntityDtoConverter.memberConvertToDto(createdMember); } + /** + * Update member using memberDTO. + * + * @param memberDto + * @return MemberDto + * @throws EntityNotFoundException member is not exist. + * @throws NullPointerException in case member sequence is null/ + */ public MemberDto updateMember(MemberDto memberDto) { - if(memberDto.getSequence() == null || !memberRepository.exists(memberDto.getSequence())) { - throw new IllegalArgumentException(); + + if(!memberRepository.exists(Objects.requireNonNull(memberDto.getSequence()))) { + throw new EntityNotFoundException(); } memberDto.setLevel(PointPerLevel.valueOf(memberDto.getPoint())); Member member = memberRepository.save(new Member(memberDto)); - return new MemberDto(member.getSeq(), member.getId(), member.getPassword(), member.getEmail(), member.getNickName(), member.getRegistrationDate(), member.getPoint()); + return EntityDtoConverter.memberConvertToDto(member); } + /** + * Delete member using member sequence. + * + * @param memberSequence + * @throws NullPointerException in case member sequence is null. + */ public void deleteMember(@NotNull final Long memberSequence) { - if(memberSequence == null) { - throw new IllegalArgumentException(); - } - - memberRepository.delete(memberSequence); + memberRepository.delete(Objects.requireNonNull(memberSequence)); } } diff --git a/src/main/java/io/codechobo/member/application/SocialService.java b/src/main/java/io/codechobo/member/application/SocialService.java new file mode 100644 index 0000000..70ce340 --- /dev/null +++ b/src/main/java/io/codechobo/member/application/SocialService.java @@ -0,0 +1,113 @@ +package io.codechobo.member.application; + +import io.codechobo.member.domain.Member; +import io.codechobo.member.domain.Social; +import io.codechobo.member.domain.repository.MemberRepository; +import io.codechobo.member.domain.repository.SocialRepository; +import io.codechobo.member.domain.support.SocialDto; +import io.codechobo.member.domain.util.EntityDtoConverter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityNotFoundException; +import java.util.List; +import java.util.Objects; + +/** + * @author loustler + * @since 10/24/2016 23:42 + */ +@Service +@Transactional +public class SocialService { + @Autowired + private SocialRepository socialRepository; + @Autowired + private MemberRepository memberRepository; + + public List all() { + List socialList = socialRepository.findAll(); + + return EntityDtoConverter.socialListConvertToDtoList(socialList); + } + + /** + * Get multi social using member sequence. + * + * @param memberSequence + * @return List if not found return empty size + * @throws NullPointerException in case not found any social + */ + public List getSocials(final Long memberSequence) { + List socialList = socialRepository.findByMemberSeq(Objects.requireNonNull(memberSequence)); + + if(Objects.isNull(socialList)) return null; + + return EntityDtoConverter.socialListConvertToDtoList(socialList); + } + + /** + * Get single social using social sequence. + * + * @param socialSequence + * @return SocialDto + */ + public SocialDto getSocial(final Long socialSequence) { + Social foundSocial = socialRepository.findOne(Objects.requireNonNull(socialSequence)); + + return EntityDtoConverter.socialConvertToDto(foundSocial); + } + + /** + * Create SocialDto using member sequence in socialDto. + * + * @param socialDto + * @return SocialDto + * @throws NullPointerException in case socialDto's member sequence is null + */ + public SocialDto createSocial(final SocialDto socialDto) { + Member foundMember = memberRepository.findOne(Objects.requireNonNull(socialDto.getMemberSequence())); + + Social social = EntityDtoConverter.socialDtoConvertToEntity(socialDto); + + social.setMember(foundMember); + + Social created = socialRepository.save(social); + + return EntityDtoConverter.socialConvertToDto(created); + } + + /** + * Update Social using Social Sequence. + * + * @param socialDto + * @return SocialDto + * @throws EntityNotFoundException in case social sequence is null or not exist. + * @throws NullPointerException in case social sequence is null. + */ + public SocialDto updateSocial(final SocialDto socialDto) { + if (!socialRepository.exists(Objects.requireNonNull(socialDto.getSequence()))) + throw new EntityNotFoundException(socialDto.getSequence()+" is not exist."); + + Member foundMember = memberRepository.findOne(Objects.requireNonNull(socialDto.getMemberSequence())); + + Social updateSocial = new Social(socialDto); + + updateSocial.setMember(foundMember); + + Social updated = socialRepository.save(updateSocial); + + return EntityDtoConverter.socialConvertToDto(updated); + } + + /** + * Delete social using social sequence. + * + * @param socialSequence NotNull + * @throws NullPointerException in case social sequence is null. + */ + public void deleteSocial(final Long socialSequence) { + socialRepository.delete(Objects.requireNonNull(socialSequence)); + } +} diff --git a/src/main/java/io/codechobo/member/domain/.gitkeep b/src/main/java/io/codechobo/member/domain/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/io/codechobo/member/domain/Member.java b/src/main/java/io/codechobo/member/domain/Member.java index 1792a39..41d089b 100644 --- a/src/main/java/io/codechobo/member/domain/Member.java +++ b/src/main/java/io/codechobo/member/domain/Member.java @@ -2,6 +2,7 @@ import io.codechobo.member.domain.support.MemberDto; +import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -21,12 +22,13 @@ import java.util.List; /** - * Created by Loustler on 8/7/16. + * @author loustler + * @since 08/07/2016 */ @Entity @Setter @Getter -@NoArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) public class Member { @Id @@ -65,7 +67,7 @@ public class Member { /* * social은 member에 종속적이므로 cascade */ - @OneToMany(fetch = FetchType.EAGER, mappedBy = "seq") + @OneToMany(fetch = FetchType.EAGER, mappedBy = "member") private List socials; public Member(final MemberDto memberDto) { @@ -78,5 +80,4 @@ public Member(final MemberDto memberDto) { this.level = memberDto.getLevel(); this.registrationDate = memberDto.getRegiDate(); } - } diff --git a/src/main/java/io/codechobo/member/domain/PointPerLevel.java b/src/main/java/io/codechobo/member/domain/PointPerLevel.java index 22f5f83..1d9dfba 100644 --- a/src/main/java/io/codechobo/member/domain/PointPerLevel.java +++ b/src/main/java/io/codechobo/member/domain/PointPerLevel.java @@ -3,7 +3,8 @@ import lombok.Getter; /** - * Created by Loustler on 8/16/16. + * @author loustler + * @since 08/16/2016 */ @Getter public enum PointPerLevel { diff --git a/src/main/java/io/codechobo/member/domain/Social.java b/src/main/java/io/codechobo/member/domain/Social.java index 8c122ba..e0b4b3c 100644 --- a/src/main/java/io/codechobo/member/domain/Social.java +++ b/src/main/java/io/codechobo/member/domain/Social.java @@ -2,24 +2,25 @@ import io.codechobo.member.domain.support.SocialDto; +import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; +import java.util.List; /** * Created by Loustler on 8/7/16. */ @Entity -@NoArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) @Setter @Getter public class Social { @@ -35,8 +36,15 @@ public class Social { @Column(name = "SOCIAL_ACCESS_TOKEN") private String token; // accessToken 등 token정보 - @ManyToOne(optional = false, fetch = FetchType.LAZY) - @JoinColumn(name = "MEMBER_SEQ") + /** + * Transactional 범위밖에서 벗어나서 LAZY에서 EAGER(default)로 수정 + * Why? List일 때 proxy initialize fail + * @see io.codechobo.member.domain.util.EntityDtoConverter#socialListConvertToDtoList(List) + * + * FK update 못하게 false 처리 + */ + @ManyToOne + @JoinColumn(name = "MEMBER_SEQ", updatable = false) private Member member; public Social(final SocialDto socialDto) { diff --git a/src/main/java/io/codechobo/member/domain/repository/.gitkeep b/src/main/java/io/codechobo/member/domain/repository/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/io/codechobo/member/domain/repository/MemberRepository.java b/src/main/java/io/codechobo/member/domain/repository/MemberRepository.java index 2caa2f3..908cb82 100644 --- a/src/main/java/io/codechobo/member/domain/repository/MemberRepository.java +++ b/src/main/java/io/codechobo/member/domain/repository/MemberRepository.java @@ -6,10 +6,11 @@ /** - * Created by Loustler on 8/21/16. + * @author loustler + * @since 08/21/2016 */ @Repository public interface MemberRepository extends JpaRepository { - Member findByNickName(String nickname); - Member findByEmail(String email); + Member findByNickName(final String nickname); + Member findByEmail(final String email); } diff --git a/src/main/java/io/codechobo/member/domain/repository/SocialRepository.java b/src/main/java/io/codechobo/member/domain/repository/SocialRepository.java index 0eb4677..e2a66ad 100644 --- a/src/main/java/io/codechobo/member/domain/repository/SocialRepository.java +++ b/src/main/java/io/codechobo/member/domain/repository/SocialRepository.java @@ -2,9 +2,15 @@ import io.codechobo.member.domain.Social; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; /** - * Created by Loustler on 8/21/16. + * @author loustler + * @since 08/21/2016 */ +@Repository public interface SocialRepository extends JpaRepository { + List findByMemberSeq(Long memberSeq); } diff --git a/src/main/java/io/codechobo/member/domain/support/MemberDto.java b/src/main/java/io/codechobo/member/domain/support/MemberDto.java index 34c2df7..575cd24 100644 --- a/src/main/java/io/codechobo/member/domain/support/MemberDto.java +++ b/src/main/java/io/codechobo/member/domain/support/MemberDto.java @@ -1,19 +1,24 @@ package io.codechobo.member.domain.support; import io.codechobo.member.domain.PointPerLevel; -import lombok.Getter; +import lombok.AccessLevel; +import lombok.Data; import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.experimental.Accessors; +import javax.validation.constraints.NotNull; +import java.util.ArrayList; import java.util.Date; +import java.util.List; +import java.util.Objects; /** * @author loustler * @since 10/02/2016 10:13 */ -@Getter -@Setter -@NoArgsConstructor +@Accessors(chain = true) +@Data +@NoArgsConstructor(access = AccessLevel.PUBLIC) public class MemberDto { private Long sequence; private String id; @@ -23,27 +28,89 @@ public class MemberDto { private Date regiDate; private Integer point; private PointPerLevel level; + private List socialDtoList; - public MemberDto(final Long sequence) { - this.sequence = sequence; + private MemberDto(Builder builder) { + this.sequence = builder.sequence; + this.id = builder.id; + this.password = builder.password; + this.email = builder.email; + this.nickName = builder.nickName; + this.regiDate = builder.regiDate; + this.point = builder.point; + this.level = builder.level; + this.socialDtoList = builder.socialDtoList; } - public MemberDto(final String id, final String password, final String email, final String nickName) { - this(null, id, password, email, nickName, null, 0); - } + public static class Builder { + private Long sequence = null; + private String id = null; + private String password = null; + private String email = null; + private String nickName = null; + private Date regiDate = null; + private Integer point = null; + private PointPerLevel level = null; + private List socialDtoList = null; - public MemberDto(final String id, final String password, final String email, final String nickName, final Date regiDate, final Integer point) { - this(null, id, password, email, nickName, regiDate, point); - } + public Builder() {} + + public Builder sequence(@NotNull final Long sequence) { + this.sequence = sequence; + return this; + } + + public Builder id(@NotNull final String id) { + this.id = id; + return this; + } + + public Builder password(@NotNull final String password) { + this.password = password; + return this; + } + + public Builder email(@NotNull final String email) { + this.email = email; + return this; + } + + public Builder nickName(@NotNull final String nickName) { + this.nickName = nickName; + return this; + } + + public Builder regiDate(@NotNull final Date regiDate) { + this.regiDate = regiDate; + return this; + } + + public Builder point(@NotNull final Integer point) { + this.point = point; + return this; + } + + public Builder level(@NotNull final PointPerLevel level) { + this.level = level; + return this; + } + + public Builder socials(@NotNull final List socialDtoList) { + this.socialDtoList = socialDtoList; + return this; + } + + public Builder social(@NotNull final SocialDto socialDto) { + if(Objects.isNull(this.socialDtoList)) + this.socialDtoList = new ArrayList<>(); + + this.socialDtoList.add(socialDto); + + return this; + } - public MemberDto(final Long sequence, final String id, final String password, final String email, final String nickName, final Date regiDate, final Integer point) { - this.sequence = sequence; - this.id = id; - this.password = password; - this.email = email; - this.nickName = nickName; - this.regiDate = regiDate; - this.point = point; - this.level = PointPerLevel.valueOf(this.point); + public MemberDto build() { + return new MemberDto(this); + } } } diff --git a/src/main/java/io/codechobo/member/domain/support/SocialDto.java b/src/main/java/io/codechobo/member/domain/support/SocialDto.java index 10da410..fd69a76 100644 --- a/src/main/java/io/codechobo/member/domain/support/SocialDto.java +++ b/src/main/java/io/codechobo/member/domain/support/SocialDto.java @@ -1,34 +1,67 @@ package io.codechobo.member.domain.support; -import lombok.Getter; +import lombok.AccessLevel; +import lombok.Data; import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotNull; /** * @author loustler * @since 10/02/2016 10:15 */ -@Getter -@Setter -@NoArgsConstructor +@Accessors(chain = true) +@Data +@NoArgsConstructor(access = AccessLevel.PUBLIC) public class SocialDto { private Long sequence; private String type; private String token; private Long memberSequence; - public SocialDto(final Long sequence) { - this.sequence = sequence; + /** + * Builder pattern + * + * @param builder + */ + private SocialDto(final Builder builder) { + this.sequence = builder.sequence; + this.type = builder.type; + this.token = builder.token; + this.memberSequence = builder.memberSequence; } - public SocialDto(final String type, final String token, final Long memberSequence) { - this(null, type, token, memberSequence); - } + public static class Builder { + private Long sequence = null; + private String type = null; + private String token = null; + private Long memberSequence = null; + + public Builder() {} + + public Builder sequence(@NotNull final Long sequence) { + this.sequence = sequence; + return this; + } + + public Builder type(@NotNull final String type) { + this.type = type; + return this; + } + + public Builder token(@NotNull final String token) { + this.token = token; + return this; + } + + public Builder memberSequence(@NotNull final Long memberSequence) { + this.memberSequence = memberSequence; + return this; + } - public SocialDto(final Long sequence, final String type, final String token, final Long memberSequence) { - this.sequence = sequence; - this.type = type; - this.token = token; - this.memberSequence = memberSequence; + public SocialDto build() { + return new SocialDto(this); + } } } diff --git a/src/main/java/io/codechobo/member/domain/util/EntityDtoConverter.java b/src/main/java/io/codechobo/member/domain/util/EntityDtoConverter.java new file mode 100644 index 0000000..80413df --- /dev/null +++ b/src/main/java/io/codechobo/member/domain/util/EntityDtoConverter.java @@ -0,0 +1,138 @@ +package io.codechobo.member.domain.util; + +import io.codechobo.member.domain.Member; +import io.codechobo.member.domain.Social; +import io.codechobo.member.domain.support.MemberDto; +import io.codechobo.member.domain.support.SocialDto; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * @author loustler + * @since 10/25/2016 13:08 + */ +public class EntityDtoConverter { + + /** + * Social Entity convert to SocialDto + * + * @param social + * @return SocialDto + * @throws NullPointerException in case social is null + */ + public static SocialDto socialConvertToDto(Social social) { + social = Objects.requireNonNull(social); + + return new SocialDto.Builder() + .sequence(social.getSeq()) + .type(social.getType()) + .token(social.getToken()) + .memberSequence(social.getMember().getSeq()) + .build(); + } + + /** + * SocialDto convert to Social + * + * @param socialDto + * @return Social + * @throws NullPointerException in case socialDto is null + */ + public static Social socialDtoConvertToEntity(SocialDto socialDto) { + Social social = new Social(Objects.requireNonNull(socialDto)); + Member member = new Member(new MemberDto()); + + member.setSeq(socialDto.getMemberSequence()); + social.setMember(member); + + return social; + } + + /** + * Member Entity convert to MemberDto + * + * @param member + * @return MemberDto + * @throws NullPointerException in case member is null + */ + public static MemberDto memberConvertToDto(Member member) { + member = Objects.requireNonNull(member); + + if(Objects.isNull(member.getSocials())) + member.setSocials(new ArrayList<>()); + + return new MemberDto.Builder() + .sequence(member.getSeq()) + .id(member.getId()) + .password(member.getPassword()) + .email(member.getEmail()) + .nickName(member.getNickName()) + .point(member.getPoint()) + .regiDate(member.getRegistrationDate()) + .level(member.getLevel()) + .socials(socialListConvertToDtoList(member.getSocials())) + .build(); + } + + /** + * MemberDto convert to Member + * + * @param memberDto + * @return Member + * @throws NullPointerException in case memberdto is null + */ + public static Member memberDtoConvertToEntity(MemberDto memberDto) { + return new Member(Objects.requireNonNull(memberDto)); + } + + public static List memberListConvertToDtoList(final List memberList) { + return memberList.stream().map(m -> EntityDtoConverter.memberConvertToDto(m)).collect(Collectors.toList()); + } + + public static List memberDtoListConvertToEntityList(final List memberDtoList) { + return memberDtoList.stream().map(md -> EntityDtoConverter.memberDtoConvertToEntity(md)).collect(Collectors.toList()); + } + + public static List socialListConvertToDtoList(final List socialList) { + return socialList.stream().map(s -> EntityDtoConverter.socialConvertToDto(s)).collect(Collectors.toList()); + } + + public static List socialDtoListConvertToEntityList(final List socialDtoList) { + return socialDtoList.stream().map(sd -> EntityDtoConverter.socialDtoConvertToEntity(sd)).collect(Collectors.toList()); + } + + /* + public static List entityListConvertToDtoList(List entityList, T t) { + Class entityC = EntityDtoConverter.class; + String methodName = null; + + if (t instanceof Member) methodName = "memberConvertToDto"; + else if (t instanceof Social) methodName = "socialConvertToDto"; + else throw new IllegalArgumentException(); + + Method method = null; + try { + method = entityC.getMethod(methodName, new Class[]{t.getClass()}); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + final Method realMethod = method; + + List result = new ArrayList(entityList.size()); + + try { + result = Collections.synchronizedList(entityList).parallelStream() +// .map(c -> realMethod.invoke(null, c)) + .collect(Collectors.toList()); + } catch (Exception invoE) { + result = null; + } + + return result; + } + */ +} diff --git a/src/main/java/io/codechobo/member/interfaces/api/MemberRestController.java b/src/main/java/io/codechobo/member/interfaces/api/MemberRestController.java index 7a04957..eb34702 100644 --- a/src/main/java/io/codechobo/member/interfaces/api/MemberRestController.java +++ b/src/main/java/io/codechobo/member/interfaces/api/MemberRestController.java @@ -5,7 +5,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; /** * @author loustler diff --git a/src/main/java/io/codechobo/member/interfaces/api/SocialRestController.java b/src/main/java/io/codechobo/member/interfaces/api/SocialRestController.java new file mode 100644 index 0000000..f3e723b --- /dev/null +++ b/src/main/java/io/codechobo/member/interfaces/api/SocialRestController.java @@ -0,0 +1,50 @@ +package io.codechobo.member.interfaces.api; + +import io.codechobo.member.application.SocialService; +import io.codechobo.member.domain.support.SocialDto; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author loustler + * @since 10/24/2016 23:53 + */ +@RestController +@RequestMapping("/api/social") +public class SocialRestController { + @Autowired + private SocialService socialService; + + @RequestMapping(value = "/{memberSeq}", method = RequestMethod.GET) + public ResponseEntity show(@PathVariable Long memberSeq) { + return new ResponseEntity(socialService.getSocials(memberSeq), HttpStatus.OK); + } + + @RequestMapping(value = "/one/{socialSeq}", method = RequestMethod.GET) + public ResponseEntity one(@PathVariable Long socialSeq) { + return new ResponseEntity(socialService.getSocial(socialSeq), HttpStatus.OK); + } + + @RequestMapping(value = {"", "/"}, method = RequestMethod.POST) + public ResponseEntity save(@RequestBody SocialDto socialDto) { + return new ResponseEntity(socialService.createSocial(socialDto), HttpStatus.CREATED); + } + + @RequestMapping(value = "{seq}", method = RequestMethod.PUT) + public ResponseEntity update(@RequestBody SocialDto socialDto, @PathVariable Long seq) { + socialDto.setSequence(seq); + return new ResponseEntity(socialService.updateSocial(socialDto), HttpStatus.OK); + } + + @RequestMapping(value = "{seq}", method = RequestMethod.DELETE) + public ResponseEntity delete(@PathVariable Long seq) { + socialService.deleteSocial(seq); + return new ResponseEntity(HttpStatus.OK); + } +} diff --git a/src/test/java/io/codechobo/event/domain/JoiningIntegrationTest.java b/src/test/java/io/codechobo/event/domain/JoiningIntegrationTest.java index 9cdab71..f9928cf 100644 --- a/src/test/java/io/codechobo/event/domain/JoiningIntegrationTest.java +++ b/src/test/java/io/codechobo/event/domain/JoiningIntegrationTest.java @@ -52,8 +52,8 @@ public class JoiningIntegrationTest { @Before public void setUp() { - member1 = new Member(new MemberDto("member1", "password", "이메일1@gmail.com", "닉네임1")); - member2 = new Member(new MemberDto("member2", "password", "이메일2@gmail.com", "닉네임2")); + member1 = new Member(new MemberDto.Builder().id("member1").password("password").email("이메일1@gmail.com").nickName("닉네임1").build()); + member2 = new Member(new MemberDto.Builder().id("member2").password("password").email("이메일2@gmail.com").nickName("닉네임2").build()); memberRepository.save(member1); memberRepository.save(member2); diff --git a/src/test/java/io/codechobo/event/domain/WinningIntegrationTest.java b/src/test/java/io/codechobo/event/domain/WinningIntegrationTest.java index 75013af..ea7b848 100644 --- a/src/test/java/io/codechobo/event/domain/WinningIntegrationTest.java +++ b/src/test/java/io/codechobo/event/domain/WinningIntegrationTest.java @@ -58,9 +58,23 @@ public class WinningIntegrationTest { @Before public void setUp() { - 응모자1 = new Member(new MemberDto("member1", "password", "이메일1@gmail.com", "닉네임1")); + 응모자1 = new Member( + new MemberDto.Builder() + .id("member1") + .password("password") + .email("이메일1@gmail.com") + .nickName("닉네임1") + .build() + ); memberRepository.save(응모자1); - 응모자2 = new Member(new MemberDto("member2", "password", "이메일2@gmail.com", "닉네임2")); + 응모자2 = new Member( + new MemberDto.Builder() + .id("member2") + .password("password") + .email("이메일2@gmail.com") + .nickName("닉네임2") + .build() + ); memberRepository.save(응모자2); category = new EventCategory("온라인이벤트"); diff --git a/src/test/java/io/codechobo/event/interfaces/JoiningControllerIntegrationTest.java b/src/test/java/io/codechobo/event/interfaces/JoiningControllerIntegrationTest.java index c5b3dd1..fb88c90 100644 --- a/src/test/java/io/codechobo/event/interfaces/JoiningControllerIntegrationTest.java +++ b/src/test/java/io/codechobo/event/interfaces/JoiningControllerIntegrationTest.java @@ -73,7 +73,7 @@ public void setUp() { anEvent = eventBuilder.build(); eventRepository.save(anEvent); - aMember = new Member(new MemberDto("member", "password", "email@gmail.com", "nickname")); + aMember = new Member(new MemberDto.Builder().id("member").password("password").email("password").nickName("nickname").build()); aMember = memberRepository.save(aMember); } diff --git a/src/test/java/io/codechobo/event/interfaces/WinningControllerIntegrationTest.java b/src/test/java/io/codechobo/event/interfaces/WinningControllerIntegrationTest.java index 1d6456c..8a13a6e 100644 --- a/src/test/java/io/codechobo/event/interfaces/WinningControllerIntegrationTest.java +++ b/src/test/java/io/codechobo/event/interfaces/WinningControllerIntegrationTest.java @@ -101,8 +101,8 @@ public void setUp() { .with이벤트상태(EventStatus.OPEN).build(); anEvent = eventRepository.save(anEvent); - 응모자1 = new Member(new MemberDto("member1", "password1", "email1@gmail.com", "nickname1")); - 응모자2 = new Member(new MemberDto("member2", "password2", "email2@gmail.com", "nickname2")); + 응모자1 = new Member(new MemberDto.Builder().id("member1").password("password1").email("email1@gmail.com").nickName("nickname1").build()); + 응모자2 = new Member(new MemberDto.Builder().id("member2").password("password2").email("email2@gmail.com").nickName("nickName2").build()); memberRepository.save(응모자1); memberRepository.save(응모자2); diff --git a/src/test/java/io/codechobo/member/application/MemberServiceIntegrationTest.java b/src/test/java/io/codechobo/member/application/MemberServiceIntegrationTest.java index 204955b..e1c67ad 100644 --- a/src/test/java/io/codechobo/member/application/MemberServiceIntegrationTest.java +++ b/src/test/java/io/codechobo/member/application/MemberServiceIntegrationTest.java @@ -3,9 +3,14 @@ import io.codechobo.member.domain.PointPerLevel; import io.codechobo.member.domain.support.MemberDto; import org.junit.Test; +import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; import java.util.Calendar; +import java.util.List; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertNotNull; @@ -16,30 +21,45 @@ * @author loustler * @since 10/02/2016 10:19 */ +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles(value = "test") public class MemberServiceIntegrationTest { @Autowired MemberService memberService; @Test public void 멤버등록_테스트_crate_member() { + // given & when final MemberDto dto = generateData(); final MemberDto member = memberService.createMember(dto); + // then assertNotNull(member); assertThat(member.getNickName(), is("nickName")); - System.out.println(member.getLevel()); } @Test public void 멤버_업데이트_update_member() { + // given final MemberDto dto = generateData(); final MemberDto member = memberService.createMember(dto); - final MemberDto updateDto = new MemberDto(member.getSequence(), member.getId(), "another password", "loustler@gmail.com", "loustler", Calendar.getInstance().getTime(), new Integer(100)); + final MemberDto updateDto = new MemberDto.Builder() + .sequence(member.getSequence()) + .id(member.getId()) + .password("another password") + .email("loustler@gmail.com") + .nickName("loustler") + .regiDate(Calendar.getInstance().getTime()) + .point(new Integer(100)) + .build(); memberService.updateMember(updateDto); + // when final MemberDto updateMember = memberService.getMember(member.getSequence()); + // then assertNotNull(updateMember); assertThat(updateMember.getNickName(), is("loustler")); assertThat(updateMember.getLevel(), is(PointPerLevel.STANDARD)); @@ -47,17 +67,40 @@ public class MemberServiceIntegrationTest { @Test public void 멤버_삭제_remove_member() { + // given final MemberDto dto = generateData(); final MemberDto member = memberService.createMember(dto); memberService.deleteMember(member.getSequence()); + // when MemberDto after = memberService.getMember(member.getSequence()); + // then assertNull(after); } + @Test + public void 멤버_리스트_get_members() { + // given + List memberDtoList = memberService.getMembers(); + + // when + + // then + assertThat(memberDtoList.size(), is(0)); + assertNotNull(memberDtoList); + + } + private MemberDto generateData() { - return new MemberDto("id", "password", "email@gmail.com", "nickName", Calendar.getInstance().getTime(), new Integer(0)); + return new MemberDto.Builder() + .id("id") + .password("password") + .email("email@gmail.com") + .nickName("nickName") + .regiDate(Calendar.getInstance().getTime()) + .point(new Integer(0)) + .build(); } } diff --git a/src/test/java/io/codechobo/member/application/SocialServiceIntegrationTest.java b/src/test/java/io/codechobo/member/application/SocialServiceIntegrationTest.java new file mode 100644 index 0000000..0f75ca7 --- /dev/null +++ b/src/test/java/io/codechobo/member/application/SocialServiceIntegrationTest.java @@ -0,0 +1,125 @@ +package io.codechobo.member.application; + +import io.codechobo.member.domain.support.MemberDto; +import io.codechobo.member.domain.support.SocialDto; +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.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; +import java.util.Objects; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; + +/** + * @author loustler + * @since 10/25/2016 23:46 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles(value = "test") +public class SocialServiceIntegrationTest { + @Autowired + private SocialService socialService; + + @Autowired + private MemberService memberService; + + private MemberDto memberDto; + private SocialDto socialDto; + + @Before + public void set_up() { + this.memberDto = memberDtoFactory(); + this.socialDto = socialDtoFactory(); + } + + @Test + public void config_test() { + } + + /** + * @see SocialService#createSocial(SocialDto) + */ + @Test + public void create_social() { + // given + SocialDto createdSocial = socialDtoFactory(new SocialDto.Builder().type("google").token("sldkfjsdklf123b45k4dkie").memberSequence(this.memberDto.getSequence()).build()); + assertNotNull(createdSocial.getMemberSequence()); + + // then + assertNotNull(createdSocial); + assertThat(createdSocial.getMemberSequence(), is(this.memberDto.getSequence())); + } + + @Test + public void get_socials() { + // given + socialDtoFactory(new SocialDto.Builder().memberSequence(this.memberDto.getSequence()).token("asfasdfsdf12313adf").type("twitter").build()); + + // when + List dtoList = socialService.getSocials(this.memberDto.getSequence()); + + // then + assertNotNull(dtoList); + } + + @Test + public void update_social() { + // given + SocialDto update = this.socialDto; + final String token = "ajs7ds8f6sdf6s5df4szds6ffsdf4"; + update.setType("twitter").setToken(token); + + if(Objects.isNull(update.getMemberSequence())) + update.setMemberSequence(this.memberDto.getSequence()); + + // when + SocialDto updated = socialService.updateSocial(update); + + // then + assertNotNull(updated); + assertThat(updated.getToken(), is(token)); + } + + @Test(expected = NullPointerException.class) + public void delete_social() { + // given + SocialDto delete = this.socialDto; + + // when + socialService.deleteSocial(delete.getSequence()); + + // then + // throw NullPointException is success. + SocialDto deleted = socialService.getSocial(delete.getSequence()); + assertNull(deleted); + + } + + private MemberDto memberDtoFactory() { + return memberService.createMember(new MemberDto.Builder() + .id("loustler") + .password("loustlerPw") + .email("dev.loustler@gmail.com") + .point(new Integer(500)) + .nickName("loustler") + .build() + ); + } + + private SocialDto socialDtoFactory() { + return socialDtoFactory(new SocialDto.Builder().type("github").token("2kd8fd7s9xkcfsl22kldkfysdf1").memberSequence(this.memberDto.getSequence()).build()); + } + + private SocialDto socialDtoFactory(SocialDto socialDto) { + return socialService.createSocial(socialDto.setMemberSequence(this.memberDto.getSequence())); + } +} diff --git a/src/test/java/io/codechobo/member/domain/MemberIntegrationTest.java b/src/test/java/io/codechobo/member/domain/MemberIntegrationTest.java index bafbd8c..b12d7e0 100644 --- a/src/test/java/io/codechobo/member/domain/MemberIntegrationTest.java +++ b/src/test/java/io/codechobo/member/domain/MemberIntegrationTest.java @@ -46,15 +46,24 @@ public void cleanUp() { @Test public void 멤버생성_create_new_member() { // given - Member member = new Member(new MemberDto("test", "password", "dev.loustler@gmail.com", "loustler", Calendar.getInstance().getTime(), new Integer(0))); + Member member = new Member( + new MemberDto.Builder() + .id("test") + .password("password") + .email("dev.loustler@gmail.com") + .nickName("loustler") + .regiDate(Calendar.getInstance().getTime()) + .point(new Integer(0)) + .build() + ); - //when + // when Member saveMember = memberRepository.save(member); // then assertThat(saveMember.getLevel(), is(PointPerLevel.BASIC)); + assertNotNull(saveMember); assertNotNull(saveMember.getSeq()); - System.out.println(saveMember); } @Test @@ -87,7 +96,16 @@ public void cleanUp() { @Test public void 멤버레벨업_level_up_member() { // given - Member newMember = new Member(new MemberDto("any2", "password", "email2@gmail.com", "anonymouse2", Calendar.getInstance().getTime(), new Integer(500))); + Member newMember = new Member( + new MemberDto.Builder() + .id("any2") + .password("password") + .email("email2@gmail.com") + .nickName("anonymouse2") + .regiDate(Calendar.getInstance().getTime()) + .point(new Integer(500)) + .build() + ); // when memberRepository.save(newMember); @@ -114,7 +132,16 @@ public void cleanUp() { } private Member memberFactory() { - Member member = new Member(new MemberDto("any1", "password", "email@gmail.com", "anonymouse", Calendar.getInstance().getTime(), new Integer(0))); + Member member = new Member( + new MemberDto.Builder() + .id("any1") + .password("password") + .email("email@gmail.com") + .nickName("anonymouse") + .regiDate(Calendar.getInstance().getTime()) + .point(new Integer(0)) + .build() + ); return memberRepository.save(member); } } diff --git a/src/test/java/io/codechobo/member/domain/SocialIntegrationTest.java b/src/test/java/io/codechobo/member/domain/SocialIntegrationTest.java index 81ae656..4c4eac7 100644 --- a/src/test/java/io/codechobo/member/domain/SocialIntegrationTest.java +++ b/src/test/java/io/codechobo/member/domain/SocialIntegrationTest.java @@ -56,17 +56,13 @@ public void setUp() { @Test public void 설정테스트_config_test() { - this.member.getSocials().stream() - .forEach(e -> System.out.println(e.getMember().getSeq())); - - System.out.println(this.member.getSeq()); } @Test public void 소셜추가_another_social_add() { // given Social anotherSocial = new Social(); - anotherSocial.setMember(this.member); + anotherSocial.setMember(memberRepository.findOne(this.member.getSeq())); anotherSocial.setToken("accessToken2"); anotherSocial.setType("google"); @@ -87,34 +83,54 @@ public void setUp() { public void 소셜가져오기_social_find() { // given - Member newMember = new Member(new MemberDto("id1", "password2", "email@gmail.com", "nickName3", this.now, new Integer(0))); - - this.memberRepository.save(newMember); + Member newMember = new Member( + new MemberDto.Builder() + .id("id1") + .password("password2") + .email("email@gmail.com") + .nickName("nickName3") + .regiDate(this.now) + .point(new Integer(0)) + .build() + ); + + newMember = this.memberRepository.save(newMember); + newMember.setSocials(new ArrayList<>()); + // then Social social1 = new Social(); social1.setMember(newMember); social1.setType("stackoverflow"); social1.setToken("accessToken3"); newMember.getSocials().add(social1); - social1 = this.socialRepository.save(social1); + // then assertNotNull(social1); Social find = this.socialRepository.findOne(social1.getSeq()); - System.out.println(find); - + assertNotNull(find); + assertThat(social1.getSeq(), is(find.getSeq())); } private Member memberFactory() { - return memberRepository.save(new Member(new MemberDto("anyone", "password", "email@provider.com", "anonymouse", this.now, new Integer(0)))); + return memberRepository.save(new Member( + new MemberDto.Builder() + .id("anyone") + .password("password") + .email("email@provider.com") + .nickName("anonymouse") + .regiDate(this.now) + .point(new Integer(0)) + .build()) + ); } private Social socialFactory() { Social social = new Social(); social.setMember(this.member); - social.setToken("accessToken"); + social.setToken("2kd8fd7s9xkcfsl22kldkfysdf1"); social.setType("github"); return socialRepository.save(social); diff --git a/src/test/java/io/codechobo/member/domain/support/MemberDtoIntegrationTest.java b/src/test/java/io/codechobo/member/domain/support/MemberDtoIntegrationTest.java new file mode 100644 index 0000000..838d3fa --- /dev/null +++ b/src/test/java/io/codechobo/member/domain/support/MemberDtoIntegrationTest.java @@ -0,0 +1,20 @@ +package io.codechobo.member.domain.support; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author loustler + * @since 10/25/2016 12:53 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles(value = "test") +public class MemberDtoIntegrationTest { + @Test + public void builder_pattern_test(){ + } +} diff --git a/src/test/java/io/codechobo/member/domain/support/SocialDtoIntegrationTest.java b/src/test/java/io/codechobo/member/domain/support/SocialDtoIntegrationTest.java new file mode 100644 index 0000000..b811790 --- /dev/null +++ b/src/test/java/io/codechobo/member/domain/support/SocialDtoIntegrationTest.java @@ -0,0 +1,35 @@ +package io.codechobo.member.domain.support; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +/** + * @author loustler + * @since 10/25/2016 12:54 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles(value = "test") +public class SocialDtoIntegrationTest { + + @Test + public void builder_pattern_test() { + SocialDto dto = new SocialDto.Builder() + .sequence(new Long(5)) + .token("token") + .type("type") + .memberSequence(new Long(5)) + .build(); + + + assertNotNull(dto); + assertThat(dto.getSequence(), is(new Long(5))); + } +} diff --git a/src/test/java/io/codechobo/member/domain/util/EntityDtoConverterIntegrationTest.java b/src/test/java/io/codechobo/member/domain/util/EntityDtoConverterIntegrationTest.java new file mode 100644 index 0000000..04e55a8 --- /dev/null +++ b/src/test/java/io/codechobo/member/domain/util/EntityDtoConverterIntegrationTest.java @@ -0,0 +1,16 @@ +package io.codechobo.member.domain.util; + +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author loustler + * @since 10/26/2016 00:28 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles(value = "test") +public class EntityDtoConverterIntegrationTest { +} diff --git a/src/test/java/io/codechobo/member/interfaces/MemberRestControllerIntegrationTest.java b/src/test/java/io/codechobo/member/interfaces/MemberRestControllerIntegrationTest.java index dd3283e..a03f360 100644 --- a/src/test/java/io/codechobo/member/interfaces/MemberRestControllerIntegrationTest.java +++ b/src/test/java/io/codechobo/member/interfaces/MemberRestControllerIntegrationTest.java @@ -16,7 +16,10 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -47,7 +50,7 @@ public void setUp() { @Test public void 멤버_등록_create_member_via_create() throws Exception{ - MemberDto memberDto = new MemberDto("id", "password", "email@gmail.com", "닉네임"); + MemberDto memberDto = new MemberDto.Builder().id("id").password("password").email("email@gmail.com").nickName("닉네임").build(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(memberDto); System.out.println(json); @@ -59,7 +62,7 @@ public void setUp() { @Test public void 멤버_가져오기_get_member() throws Exception { - MemberDto memberDto = new MemberDto("id", "password", "email@gmail.com", "닉네임"); + MemberDto memberDto = new MemberDto.Builder().id("id").password("password").email("email@gmail.com").nickName("닉네임").build(); MemberDto member = memberService.createMember(memberDto); mvc.perform(get("/api/member/"+member.getSequence())) @@ -68,7 +71,7 @@ public void setUp() { @Test public void 멤버_업데이트_update_member() throws Exception { - MemberDto memberDto = new MemberDto("id", "password", "email@gmail.com", "닉네임"); + MemberDto memberDto = new MemberDto.Builder().id("id").password("password").email("email@gmail.com").nickName("닉네임").build(); MemberDto member = memberService.createMember(memberDto); member.setPassword("pwd"); @@ -86,7 +89,7 @@ public void setUp() { @Test public void 멤버_삭제_delete_member() throws Exception { - MemberDto memberDto = new MemberDto("id", "password", "email@gmail.com", "닉네임"); + MemberDto memberDto = new MemberDto.Builder().id("id").password("password").email("email@gmail.com").nickName("닉네임").build(); MemberDto member = memberService.createMember(memberDto); mvc.perform(delete("/api/member/"+member.getSequence())) diff --git a/src/test/java/io/codechobo/member/interfaces/SocialRestControllerIntegrationTest.java b/src/test/java/io/codechobo/member/interfaces/SocialRestControllerIntegrationTest.java new file mode 100644 index 0000000..ba6e167 --- /dev/null +++ b/src/test/java/io/codechobo/member/interfaces/SocialRestControllerIntegrationTest.java @@ -0,0 +1,118 @@ +package io.codechobo.member.interfaces; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.codechobo.member.application.MemberService; +import io.codechobo.member.application.SocialService; +import io.codechobo.member.domain.support.MemberDto; +import io.codechobo.member.domain.support.SocialDto; +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.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * @author loustler + * @since 10/30/2016 18:46 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@WebAppConfiguration +@ActiveProfiles(profiles = "test") +public class SocialRestControllerIntegrationTest { + @Autowired + private WebApplicationContext context; + + private MockMvc mvc; + + @Autowired + private MemberService memberService; + + @Autowired + private SocialService socialService; + + private MemberDto memberDto; + + @Before + public void setUp() { + this.mvc = MockMvcBuilders.webAppContextSetup(context).build(); + MemberDto memberDto = new MemberDto.Builder().id("id").password("password").email("email@gmail.com").nickName("닉네임").build(); + this.memberDto = memberService.createMember(memberDto); + } + + @Test + public void 설정_테스트_config_test() {} + + @Test + public void create_social() throws Exception { + // when + SocialDto createData = socialDtoFactory(); + ObjectMapper mapper = new ObjectMapper(); + + // given + String json = mapper.writeValueAsString(createData); + + // then + mvc.perform(post("/api/social/") + .content(json) + .contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect(status().isCreated()); + } + + @Test + public void get_social() throws Exception { + // given & when + create_social(); + + // then + mvc.perform(get("/api/social/"+this.memberDto.getSequence())).andExpect(status().isOk()); + } + + @Test + public void update_social() throws Exception { + // given + SocialDto created = new SocialDto.Builder().memberSequence(this.memberDto.getSequence()).token("sdfj12k3j12j3b13j5j4543k42kjljklbjdsf").type("github").build(); + created = socialService.createSocial(created); + SocialDto update = created.setToken("sdfjs7dfdsfdsf8ds6fd5s8fsdf92934").setType("facebook"); + + // when + update = socialService.updateSocial(update); + ObjectMapper mapper = new ObjectMapper(); + String json = mapper.writeValueAsString(update); + + // then + mvc.perform(put("/api/social/"+update.getSequence()) + .content(json) + .contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect(status().isOk()); + } + + @Test + public void delete_social() throws Exception { + // given + SocialDto created = socialDtoFactory(); + created = socialService.createSocial(created); + + // when & then + mvc.perform(delete("/api/social/"+created.getSequence())) + .andExpect(status().isOk()); + } + + private SocialDto socialDtoFactory() { + return new SocialDto.Builder().memberSequence(this.memberDto.getSequence()).token("sdfj12k3j12j3b13j5j4543k42kjljklbjdsf").type("github").build(); + } + +}