Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
[#1] create account
Browse files Browse the repository at this point in the history
- Account create에 대한 Test code 작성
- Account create에 대한 Service, controller 비즈니스 로직 완성
  • Loading branch information
jjeda committed Sep 6, 2019
1 parent 062926e commit c243bef
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-security</artifactId>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/me/jjeda/mall/accounts/Service/AccountService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.jjeda.mall.accounts.Service;

import lombok.AllArgsConstructor;
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.accounts.repository.AccountRepository;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class AccountService {

AccountRepository accountRepository;

public Account saveAccount(AccountDto dto) {
//TODO : passwordEncoder로 비밀번호 hashing처리
return accountRepository.save(dto.toEntity());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
package me.jjeda.mall.accounts.controller;

import org.springframework.stereotype.Controller;
import lombok.AllArgsConstructor;
import me.jjeda.mall.accounts.Service.AccountService;
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.dto.AccountDto;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
import java.net.URI;

@RestController
@AllArgsConstructor
@RequestMapping(value = "/api/accounts", produces = MediaTypes.HAL_JSON_UTF8_VALUE)
public class AccountController {

AccountService accountService;

@PostMapping
public ResponseEntity createAccount(@RequestBody AccountDto requestAccount) {
Account account = accountService.saveAccount(requestAccount);
URI uri = ControllerLinkBuilder.linkTo(AccountController.class).slash(account.getId()).toUri();

return ResponseEntity.created(uri).body(account);
}
}
1 change: 0 additions & 1 deletion src/main/java/me/jjeda/mall/accounts/domain/Account.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.jjeda.mall.accounts.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/me/jjeda/mall/accounts/domain/Address.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package me.jjeda.mall.accounts.domain;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import javax.persistence.Embeddable;

@Embeddable
@AllArgsConstructor
@NoArgsConstructor
public class Address {

private String city;
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/me/jjeda/mall/accounts/dto/AccountDto.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package me.jjeda.mall.accounts.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.domain.AccountRole;
import me.jjeda.mall.accounts.domain.Address;

import java.time.LocalDateTime;

@JsonIgnoreProperties({"email", "password", "phone", "address", "accountRole"})
//@JsonIgnoreProperties({"email", "password", "phone", "address", "accountRole"})
@AllArgsConstructor
@Getter
@Builder
public class AccountDto {
private Long id;

private String userName;

/**
* email, password, phone, Role, address 등은 개인정보로 직렬화하여 메시지에 담지 않는다.
*/
Expand All @@ -25,7 +30,15 @@ public class AccountDto {

private Address address;

private LocalDateTime createdDate;

private LocalDateTime modifiedDate;
public Account toEntity() {
return Account.builder()
.userName(this.userName)
.email(this.email)
.password(this.password)
.address(this.address)
.phone(this.phone)
.accountRole(this.accountRole)
.createdDate(LocalDateTime.now())
.build();
}
}
10 changes: 10 additions & 0 deletions src/main/java/me/jjeda/mall/common/TestDescription.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.jjeda.mall.common;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface TestDescription { String value();}
Original file line number Diff line number Diff line change
@@ -1,7 +1,98 @@
package me.jjeda.mall.accounts.controller;

import static org.junit.Assert.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import me.jjeda.mall.accounts.domain.AccountRole;
import me.jjeda.mall.accounts.domain.Address;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.accounts.repository.AccountRepository;
import me.jjeda.mall.common.TestDescription;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class AccountControllerTest {

@Autowired
AccountRepository accountRepository;

@Autowired
private ObjectMapper objectMapper;

@Autowired
private MockMvc mockMvc;

@Test
@TestDescription("정상적으로 계정을 생성하는 테스트")
public void createAccount() throws Exception {
//given
AccountDto dto = AccountDto.builder()
.email("jjeda@naver.com")
.password("pass")
.userName("jjeda")
.phone("010-1234-1234")
.accountRole(AccountRole.USER)
.address(new Address("seoul", "anju", "1234"))
.build();
accountRepository.save(dto.toEntity());

// when & then
mockMvc.perform(post("/api/accounts")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(dto)))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("userName").value("jjeda"));
// .andExpect(jsonPath("password").doesNotExist());
}

@Test
@TestDescription("입력 값이 유효하지 않는 경우 에러가 발생하는 테스트")
public void createAccount_Bad_Request_Wrong_Input() {

}

@Test
@TestDescription("30명의 유저에서 10명씩 2번째 페이지 조회하기")
public void queryAccount() {

}

@Test
@TestDescription("정상적으로 유저 한명을 조회하는 테스트")
public void getAccount() {

}

@Test
@TestDescription("정상적으로 비밀번호를 변경하는 테스트")
public void modifyPassword() {

}

@Test
@TestDescription("정상적으로 개인정보를 변경하는 테스트")
public void modifyAccount() {

}

@Test
@TestDescription("정상적으로 계정정보를 삭제하는 테스트")
public void deleteAccount() {

}

}

0 comments on commit c243bef

Please sign in to comment.