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

Commit

Permalink
[#1] read & delete Account
Browse files Browse the repository at this point in the history
- Account 전체 목록 불러오는 테스트 코드 작성 -> 로직 작성
- Account 하나 불러오는 테스트 코드 작성 -> 로직
- Account 하나 삭제하는 테스트 코드 작성 -> 로직
  • Loading branch information
jjeda committed Sep 8, 2019
1 parent 4d2eef7 commit 4e87215
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 10 deletions.
19 changes: 18 additions & 1 deletion src/main/java/me/jjeda/mall/accounts/Service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
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.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.PagedResources;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@AllArgsConstructor
public class AccountService {
Expand All @@ -17,4 +21,17 @@ public Account saveAccount(AccountDto dto) {
//TODO : passwordEncoder로 비밀번호 hashing처리
return accountRepository.save(dto.toEntity());
}

public PagedResources findAllAccount(Pageable pageable, PagedResourcesAssembler<Account> pagedResourcesAssembler) {

return pagedResourcesAssembler.toResource(accountRepository.findAll(pageable));
}

public Optional<Account> getAccount(Long id) {
return accountRepository.findById(id);
}

public void deleteAccount(Long id) {
accountRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
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.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
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;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;


import java.net.URI;
import java.util.Optional;

@RestController
@AllArgsConstructor
Expand All @@ -28,4 +35,25 @@ public ResponseEntity createAccount(@RequestBody AccountDto requestAccount) {

return ResponseEntity.created(uri).body(account);
}

@GetMapping
public ResponseEntity queryAccount(Pageable pageable, PagedResourcesAssembler<Account> pagedResourcesAssembler) {
PagedResources pagedResources = accountService.findAllAccount(pageable, pagedResourcesAssembler);

return ResponseEntity.ok(pagedResources);
}

@GetMapping("/{id}")
public ResponseEntity getAccount(@PathVariable Long id) {
Optional<Account> account = accountService.getAccount(id);

return ResponseEntity.ok(account);
}

@DeleteMapping("/{id}")
public ResponseEntity deleteAccount(@PathVariable Long id) {
accountService.deleteAccount(id);

return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.jjeda.mall.accounts.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.domain.AccountRole;
import me.jjeda.mall.accounts.domain.Address;
import me.jjeda.mall.accounts.dto.AccountDto;
Expand All @@ -15,7 +16,9 @@
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 java.util.stream.IntStream;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
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;
Expand Down Expand Up @@ -50,13 +53,13 @@ public void createAccount() throws Exception {

// when & then
mockMvc.perform(post("/api/accounts")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(dto)))
.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
Expand All @@ -66,15 +69,51 @@ public void createAccount_Bad_Request_Wrong_Input() {
}

@Test
@TestDescription("30명의 유저에서 10명씩 2번째 페이지 조회하기")
public void queryAccount() {
@TestDescription("100명의 유저에서 20명씩 3번째 페이지 조회하기")
public void queryAccount() throws Exception {
// given
IntStream.range(0, 100).forEach(this::generateAccount);

// when & then
this.mockMvc.perform(get("/api/accounts")
.param("page","2")
.param("size","20")
.param("sort","id,DESC"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("page").exists())
.andExpect(jsonPath("_embedded.accountList[0]").exists());

}

private Account generateAccount(int index) {
AccountDto accountDto = AccountDto.builder()
.accountRole(AccountRole.USER)
.address(new Address("", "", ""))
.email("jjeda" + index + "@naver.com")
.userName("jjeda" + index)
.phone("01012341234")
.password("pass")
.build();
return accountRepository.save(accountDto.toEntity());

}

@Test
@TestDescription("정상적으로 유저 한명을 조회하는 테스트")
public void getAccount() {
public void getAccount() throws Exception {
//given
Account account = generateAccount(1);

// when & then
this.mockMvc.perform(get("/api/accounts/{id}",account.getId()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("email").exists())
.andExpect(jsonPath("userName").exists())
.andExpect(jsonPath("phone").exists())
.andExpect(jsonPath("address").exists());
}

@Test
Expand All @@ -91,8 +130,19 @@ public void modifyAccount() {

@Test
@TestDescription("정상적으로 계정정보를 삭제하는 테스트")
public void deleteAccount() {
public void deleteAccount() throws Exception {
//given
Account account = generateAccount(1);

// when & then
this.mockMvc.perform(delete("/api/accounts/{id}",account.getId()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("id").doesNotExist())
.andExpect(jsonPath("email").doesNotExist())
.andExpect(jsonPath("userName").doesNotExist())
.andExpect(jsonPath("phone").doesNotExist())
.andExpect(jsonPath("address").doesNotExist());
}

}

0 comments on commit 4e87215

Please sign in to comment.