Skip to content

Commit

Permalink
Merge pull request #82 from T2-Papillon/dashboard_service_test/choi
Browse files Browse the repository at this point in the history
[ADD] DashBoard logic Junit Test
  • Loading branch information
choi3179 authored Apr 15, 2024
2 parents 2f795e7 + c0e3bc9 commit 6da5c80
Show file tree
Hide file tree
Showing 17 changed files with 313 additions and 7 deletions.
9 changes: 9 additions & 0 deletions papplan/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

<!-- H2 Database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>test</scope>
</dependency>

</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,24 @@ public ResponseEntity<Object> empProjectTaskAll(@PathVariable("empno") Integer e
}
}

// 사용자 프로젝트 상세 조회
// 24.03.31 : 사용자 전체 프로젝트 조회
@GetMapping("/emp/{empno}/projects")
public ResponseEntity<List<ProjectDTO>> empProjectsAll(@PathVariable("empno") Integer empNo){
List<ProjectDTO> projectDTOS = projectService.findProjectsByEmpNo(empNo);
return ResponseEntity.ok().body(projectDTOS);
}

// 프로젝트의 상세 정보와 하위 업무 조회
@GetMapping("/projects/{projNo}/tasks")
public ResponseEntity<HashMap<String,Object>> getTaskById(@PathVariable Integer projNo) {
HashMap<String,Object> prjDashBoard = new HashMap<>();
ProjectDTO project = projectService.getProjectByProjNo(projNo);
List<TaskDTO> tasks = taskService.getTasksByProjectId(projNo);
prjDashBoard = objectMapper.convertValue(project,HashMap.class);
prjDashBoard.put("tasks", tasks);
return ResponseEntity.ok().body(prjDashBoard);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@


@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Contributor{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Department {
@Column(length = 20, nullable = false)
private String dept_name;

@OneToMany(mappedBy = "department", cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "department", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private List<Employees> employees = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.boogle.papplan.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Employees {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Position {
@Column(length = 20, nullable = false)
private String position_name;

@OneToMany(mappedBy = "position", cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "position", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private List<Employees> employees = new ArrayList<>();
}

4 changes: 4 additions & 0 deletions papplan/src/main/java/com/boogle/papplan/entity/Project.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.boogle.papplan.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Project {
@Id
Expand Down
7 changes: 3 additions & 4 deletions papplan/src/main/java/com/boogle/papplan/entity/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import java.util.Objects;

@Entity
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
35 changes: 35 additions & 0 deletions papplan/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
spring:
sql:
init:
platform: mariadb
encoding: UTF-8
mode: always
schema-locations: classpath:db/schema-script/create.sql
data-locations: classpath:/db/data-script/data-department.sql
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:test;MODE=MariaDB
username: sa
password:
# data: classpath:/db/data-script/

jpa:
hibernate:
ddl-auto: none
generate-ddl: false
show-sql: false
h2:
console: # H2 DB를 웹에서 관리할 수 있는 기능
enabled: true # H2 Console 사용 여부
path: /h2-console # H2 Console 접속 주소

# Hibernate SQL ?? ?? ??
logging:
level:
org:
hibernate:
SQL: debug

devtools:
LiveReload:
enable: true
2 changes: 1 addition & 1 deletion papplan/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ spring:
spring:
config:
activate:
on-profile: local, dev, prd
on-profile: local, dev, prd, test
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.boogle.papplan.service.dashboard;

import com.boogle.papplan.dto.project.ProjectDTO;
import com.boogle.papplan.entity.*;
import com.boogle.papplan.repository.*;
import com.boogle.papplan.service.project.ProjectService;
import com.boogle.papplan.service.task.TaskService;
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.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.*;

import static org.assertj.core.api.Assertions.*;

@SpringBootTest
@ActiveProfiles("test")
public class DashBoardServiceTest {

@Autowired
private ProjectService projectService;

@Autowired
private TaskService taskService;
@Autowired
private EmployeeRepository employeeRepository;
@Autowired
private ProjectRepository projectRepository;
@Autowired
private ContributorRepository contributorRepository;
@Autowired
private TaskRepository taskRepository;


Employees employees;
Project project;
Contributor contributor;

List<Task> tasks = new ArrayList<>();

@Autowired
private DataSource dataSource;

@BeforeEach
void beforeEmployee() throws SQLException {

// given
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH);

Department department = new Department(); department.setDept_no("PL");
Position position = new Position(); position.setPosition_id("TEAM_LEADER");
ProjectPriority projectPriority = new ProjectPriority(); projectPriority.setProjectPriorityId("LV1");
ProjectStatus projectStatus = new ProjectStatus(); projectStatus.setProjectStatusId("DOING");
TaskPriority taskPriority = new TaskPriority(); taskPriority.setTaskPriorityId("LV1");
TaskStatus doing = new TaskStatus(); doing.setTaskStatusId("DOING");
TaskStatus done = new TaskStatus(); done.setTaskStatusId("DONE");

// 회원 추가
employees = new Employees(1, "test@boogle.com", "1234", "홍길동", department, position);

// 프로젝트 추가
project = new Project(1, "테스트 프로젝트", employees,
new GregorianCalendar(2024, month, 1).getTime(),
new GregorianCalendar(2024, month, 25).getTime(),
0, new Date(), "",
projectPriority, projectStatus, new ArrayList<Task>(), new ArrayList<Contributor>());
contributor = new Contributor(1L,project, employees);

// 하위업무 추가
Task task1 = new Task(1, project, "테스크1", employees,
new GregorianCalendar(2024, month, 1).getTime(),
new GregorianCalendar(2024,month, 13).getTime(),
new GregorianCalendar(2024,month,10).getTime(),
100, new GregorianCalendar(2024,month,1).getTime(),
false, "", null, "", done, taskPriority);
Task task2 = new Task(2, project, "테스크2", employees,
new GregorianCalendar(2024, month, 1).getTime(),
new GregorianCalendar(2024,month, 12).getTime(),
new GregorianCalendar(2024,month,10).getTime(),
100, new GregorianCalendar(2024,month,1).getTime(),
false, "", null, "", done, taskPriority);
Task task3 = new Task(3, project, "테스크3", employees,
new GregorianCalendar(2024, month, 1).getTime(),
new GregorianCalendar(2024,month, 12).getTime(),
null,
50, new GregorianCalendar(2024,month,1).getTime(),
false, "", null, "", doing, taskPriority);
tasks.add(task1); tasks.add(task2); tasks.add(task3);
}

/**
* T006-1
* 대시보드 집계, 필터링 로직 테스트
* 테스트를 진행하는 달을 기준으로 값을 비교
*/
@Test
@Transactional
@DisplayName("TEST1 - 대시보드 데이터 집계 및 필터링")
public void 대시보드_집계_필터링() {

// given
int empno = 1;
HashMap<String, Object> serviceResult = new HashMap<>();
LocalDate today = LocalDate.now();
int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

employeeRepository.save(employees);
projectRepository.save(project);
contributorRepository.save(contributor);
taskRepository.saveAll(tasks);

// when
Optional<HashMap<String,Object>> projectData = projectService.findProjectsByEmpNoDashBoard(empno);
Optional<HashMap<String,Object>> taskData = taskService.getTasksByEmpNoDashBoard(empno);
projectData.ifPresent(serviceResult::putAll);
taskData.ifPresent(serviceResult::putAll);

// then
assertThat(serviceResult).isNotNull();
assertThat((List)serviceResult.get("tasks")).hasSize(1); // DOING 상태인 프로젝트 개수 확인
assertThat((List)serviceResult.get("projects")).hasSize(1); // DOING 상태인 하위업무 개수 확인
if(day == 12 || day == 13)
assertThat(serviceResult.get("task_today")).isEqualTo(1);
if((day - 12) == 1 || (day - 13) == 1)
assertThat(serviceResult.get("task_yesterday")).isEqualTo(1);
assertThat(serviceResult.get("task_week")).isIn(0,1,2);

}

}
94 changes: 94 additions & 0 deletions papplan/src/test/resources/db/schema-script/create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
CREATE TABLE department (
dept_no VARCHAR(20) NOT NULL,
dept_name VARCHAR(20) NOT NULL,
PRIMARY KEY (dept_no)
);

CREATE TABLE job_title (
position_id VARCHAR(20) NOT NULL,
position_name VARCHAR(20) NOT NULL,
PRIMARY KEY (position_id)
);

CREATE TABLE employees (
eno INTEGER AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(65) NOT NULL,
name VARCHAR(20) NOT NULL,
dept_no VARCHAR(20) NOT NULL,
position_id VARCHAR(20) NOT NULL,
CONSTRAINT fk_dept_no FOREIGN KEY (dept_no) REFERENCES department(dept_no),
CONSTRAINT fk_position_id FOREIGN KEY (position_id) REFERENCES job_title(position_id)
);

CREATE TABLE project_priority (
project_priority_id VARCHAR(20) NOT NULL,
project_priority_name VARCHAR(20) NOT NULL,
PRIMARY KEY (project_priority_id)
);

CREATE TABLE project_status (
project_status_id VARCHAR(20) NOT NULL,
project_status_name VARCHAR(20) NOT NULL,
PRIMARY KEY (project_status_id)
);

CREATE TABLE project (
proj_no INTEGER AUTO_INCREMENT PRIMARY KEY,
proj_title VARCHAR(50) NOT NULL,
proj_pm INTEGER NOT NULL,
proj_start_date DATE NOT NULL,
proj_end_date DATE NOT NULL,
proj_percent INTEGER,
proj_create_date DATE NOT NULL,
proj_desc VARCHAR(300) NOT NULL,
projp_no VARCHAR(20) NOT NULL,
projs_no VARCHAR(20) NOT NULL,
FOREIGN KEY (proj_pm) REFERENCES employees (eno),
FOREIGN KEY (projp_no) REFERENCES project_priority (project_priority_id),
FOREIGN KEY (projs_no) REFERENCES project_status (project_status_id)
);

CREATE TABLE contributor (
id BIGINT NOT NULL AUTO_INCREMENT,
proj_no INTEGER NOT NULL,
eno INTEGER NOT NULL,
PRIMARY KEY (id),
CONSTRAINT FK_proj_no FOREIGN KEY (proj_no) REFERENCES project (proj_no),
CONSTRAINT FK_eno FOREIGN KEY (eno) REFERENCES employees (eno)
);

CREATE TABLE task_priority (
task_priority_id VARCHAR(20) NOT NULL,
task_priority_name VARCHAR(20) NOT NULL,
PRIMARY KEY (task_priority_id)
);

CREATE TABLE task_status (
task_status_id VARCHAR(20) NOT NULL,
task_status_name VARCHAR(20) NOT NULL,
PRIMARY KEY (task_status_id)
);

CREATE TABLE task (
task_no INTEGER NOT NULL AUTO_INCREMENT,
proj_no INTEGER NOT NULL,
task_title VARCHAR(50) NOT NULL,
assignee_eno INTEGER NOT NULL,
task_start_date DATE NOT NULL,
task_end_date DATE NOT NULL,
task_finish_date DATE,
task_percent INTEGER NOT NULL,
task_create_date DATE NOT NULL,
task_test BOOLEAN NOT NULL,
task_test_url VARCHAR(255),
task_update_date DATE,
task_desc VARCHAR(300) NOT NULL,
task_status_id VARCHAR(20) NOT NULL,
task_priority_id VARCHAR(20) NOT NULL,
PRIMARY KEY (task_no),
CONSTRAINT FK_proj_no_task FOREIGN KEY (proj_no) REFERENCES project (proj_no),
CONSTRAINT FK_assignee_eno FOREIGN KEY (assignee_eno) REFERENCES employees (eno),
CONSTRAINT FK_task_status_id_task FOREIGN KEY (task_status_id) REFERENCES task_status (task_status_id),
CONSTRAINT FK_task_priority_id_task FOREIGN KEY (task_priority_id) REFERENCES task_priority (task_priority_id)
);
Binary file modified papplan/target/classes/com/boogle/papplan/entity/Contributor.class
Binary file not shown.
Binary file modified papplan/target/classes/com/boogle/papplan/entity/Department.class
Binary file not shown.
Binary file modified papplan/target/classes/com/boogle/papplan/entity/Employees.class
Binary file not shown.
Binary file modified papplan/target/classes/com/boogle/papplan/entity/Position.class
Binary file not shown.
Binary file modified papplan/target/classes/com/boogle/papplan/entity/Project.class
Binary file not shown.

0 comments on commit 6da5c80

Please sign in to comment.