Skip to content

Commit

Permalink
Merge pull request #328 from Team-Ampersand/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
KyungJunNoh authored Apr 14, 2022
2 parents 0784a2c + a4a374f commit f24757f
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 56 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ jacocoTestReport {
fileTree(dir: it, exclude: [
'**/exception/**' // 테스트 커버리지 측정 대상에서 제거
,'**/dto/**' // 테스트 커버리지 측정 대상에서 제거
,'**/config/**'
,'**/health_check/**'
,'**/Dotori/DotoriApplication*'
,'**/Dotori/**/Q*' // Qclass 테스트 커버리지 측정 대상에서 제거
])
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

@Repository
public interface BoardRepository extends JpaRepository<Board, Long>, BoardRepositoryCustom {
Board findTop1ByOrderByCreatedDateDesc(); // 제일 마지막에 등록된 공지사항을 불러온다.
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ public interface BoardService {
Board updateBoard(Long boardId, BoardDto boardUpdateDto);

void deleteBoard(Long boardId);

Long countBoard();
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,4 @@ public void deleteBoard(Long boardId) {
boardRepository.deleteById(board.getId());
}
}

/**
* 공지사항 카운트 서비스 로직 (로그인 되지 않은 유저 사용가능)
* @return board count
* @author 배태현
*/
@Override
public Long countBoard() {
return boardRepository.count();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.server.Dotori.domain.main_page.controller;

import com.server.Dotori.domain.main_page.service.BoardAlarmService;
import com.server.Dotori.global.response.ResponseService;
import com.server.Dotori.global.response.result.SingleResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/board")
public class BoardAlarmController {

private final BoardAlarmService boardService;
private final ResponseService responseService;

@GetMapping(value = "/alarm")
@ResponseStatus( HttpStatus.OK )
@ApiImplicitParams({
@ApiImplicitParam(name = "Authorization", value = "로그인 성공 후 access_token", required = true, dataType = "String", paramType = "header"),
@ApiImplicitParam(name = "RefreshToken", value = "로그인 성공 후 refresh_token", required = false, dataType = "String", paramType = "header")
})
public SingleResult getBoardAlarmInfo() {
return responseService.getSingleResult(boardService.getBoardAlarmInfo());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.server.Dotori.domain.main_page.dto;

import com.server.Dotori.domain.member.enumType.Role;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

import static lombok.AccessLevel.PROTECTED;

@Getter @Builder
@NoArgsConstructor(access = PROTECTED) @AllArgsConstructor
public class BoardAlarmDto {

private Long id;
private String title;
private List<Role> writerRole;
private LocalDate lastBoardWriteDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.server.Dotori.domain.main_page.service;

import com.server.Dotori.domain.main_page.dto.BoardAlarmDto;

public interface BoardAlarmService {

BoardAlarmDto getBoardAlarmInfo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.server.Dotori.domain.main_page.service.Impl;

import com.server.Dotori.domain.board.Board;
import com.server.Dotori.domain.main_page.dto.BoardAlarmDto;
import com.server.Dotori.domain.board.repository.BoardRepository;
import com.server.Dotori.domain.main_page.service.BoardAlarmService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class BoardAlarmServiceImpl implements BoardAlarmService {

private final BoardRepository boardRepository;

/**
* 새로운 공지사항 등록 여부를 판단할 수 있는 서비스 로직 (로그인 된 유저 사용가능)
* @return board count
* @author 배태현
*/
@Override
public BoardAlarmDto getBoardAlarmInfo() {
Board lastWriteBoard = boardRepository.findTop1ByOrderByCreatedDateDesc();

return BoardAlarmDto.builder()
.id(lastWriteBoard.getId())
.title(lastWriteBoard.getTitle())
.writerRole(lastWriteBoard.getMember().getRoles())
.lastBoardWriteDate(lastWriteBoard.getCreatedDate().toLocalDate())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
Expand All @@ -20,7 +21,7 @@
import org.springframework.security.crypto.password.PasswordEncoder;

@RequiredArgsConstructor
@EnableWebSecurity(debug = true)
@EnableWebSecurity(debug = false)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

Expand Down Expand Up @@ -68,7 +69,6 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers("/v1/members/password/email/check").permitAll()
.antMatchers("/v1/members/gender").permitAll()
.antMatchers("/v1/refresh").permitAll()
.antMatchers("/v1/board/count").permitAll()

// 권한 별 url 접근
.antMatchers("/v1/admin/**").hasRole("ADMIN")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,4 @@ public void deleteBoardTest() {
List<Board> findAll = boardRepository.findAll();
assertTrue(findAll.size() == 0);
}

@Test
@DisplayName("공지사항 카운트 조회 테스트")
public void countBoardTest() {
// given // when
boardService.createBoard(
BoardDto.builder()
.title("도토리 공지사항")
.content("도토리 공지사항 생성 테스트")
.build(), null
);
Long boardCount = boardService.countBoard();

// then
assertTrue(boardCount.equals(1L));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.server.Dotori.domain.main_page.board_alarm.service;

import com.server.Dotori.domain.board.dto.BoardDto;
import com.server.Dotori.domain.board.service.BoardService;
import com.server.Dotori.domain.main_page.dto.BoardAlarmDto;
import com.server.Dotori.domain.main_page.service.BoardAlarmService;
import com.server.Dotori.domain.member.dto.MemberDto;
import com.server.Dotori.domain.member.enumType.Gender;
import com.server.Dotori.domain.member.repository.member.MemberRepository;
import com.server.Dotori.global.util.CurrentMemberUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

import static com.server.Dotori.domain.member.enumType.Role.ROLE_ADMIN;
import static com.server.Dotori.domain.member.enumType.Role.ROLE_MEMBER;
import static org.junit.jupiter.api.Assertions.assertEquals;

@Transactional
@SpringBootTest
class BoardAlarmServiceTest {

@Autowired private MemberRepository memberRepository;
@Autowired private PasswordEncoder passwordEncoder;
@Autowired private BoardService boardService;
@Autowired private BoardAlarmService boardAlarmService;

@BeforeEach
@DisplayName("로그인 되어있는 유저를 세팅하는 테스트")
void currentUser() {
//given
MemberDto memberDto = MemberDto.builder()
.memberName("배태현")
.stuNum("2409")
.password("0809")
.email("[email protected]")
.gender(Gender.MAN)
.build();
memberRepository.save(
memberDto.toEntity(
passwordEncoder.encode(memberDto.getPassword())
)
);
System.out.println("======== saved =========");

// when login session 발급
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
memberDto.getEmail(),
memberDto.getPassword(),
List.of(new SimpleGrantedAuthority(ROLE_ADMIN.name())));
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(token);
System.out.println("=================================");
System.out.println(context);

//then
String currentMemberEmail = CurrentMemberUtil.getCurrentEmail();
assertEquals("[email protected]", currentMemberEmail);
}

@Test
@DisplayName("가장 마지막에 등록된 공지사항이 잘 가져와지나요 ?")
public void boardAlarmTest() {
// given // when
boardService.createBoard(
BoardDto.builder()
.title("도토리 공지사항")
.content("도토리 공지사항 생성 테스트")
.build(), null
);

BoardAlarmDto boardAlarmInfo = boardAlarmService.getBoardAlarmInfo();

// then
assertEquals("도토리 공지사항", boardAlarmInfo.getTitle());
assertEquals(ROLE_MEMBER.name(), boardAlarmInfo.getWriterRole().get(0).name());
assertEquals(LocalDate.now(), boardAlarmInfo.getLastBoardWriteDate());
}
}

0 comments on commit f24757f

Please sign in to comment.