Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part1 #4

Open
wants to merge 76 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
03e8abb
first commit
Feb 18, 2024
4c9492e
start commit
Feb 19, 2024
6c5ef08
set up db and security mode
Feb 19, 2024
ea99f8f
start test spring security
Feb 19, 2024
9937918
failed test shouldNotReturnStatusOk in UserControllerTest
Feb 19, 2024
b2b4a24
pass test shouldNotReturnStatusOk in UserControllerTest
Feb 19, 2024
2a92c74
failed test shouldNotReturnHttpStatusOK in AdminControllerTest
Feb 19, 2024
bb43902
pass test shouldNotReturnHttpStatusOK in AdminControllerTest
Feb 19, 2024
448f96f
failed shouldNotReturnHttpStatusOK in TestLotteryControllerTest class
Feb 19, 2024
8481798
pass shouldNotReturnHttpStatusOK in TestLotteryControllerTest class
Feb 19, 2024
b570a80
failed shouldNotReturnEntity TestLotteryControllerTest.class
Feb 19, 2024
194a12d
pass shouldNotReturnEntity TestLotteryControllerTest.class
Feb 19, 2024
2c92a22
failed shouldReturnEntityWithCorrectTicketNumber TestLotteryControlle…
Feb 19, 2024
1bd1222
start section EXP01
Feb 19, 2024
b363894
failed shouldReturnEntity AdminControllerTest.class
Feb 19, 2024
717912a
pass shouldReturnEntity AdminControllerTest.class
Feb 19, 2024
7815885
failed shouldReturnListOfEntity AdminControllerTest.class
Feb 19, 2024
8c97b6d
pass shouldReturnListOfEntity AdminControllerTest.class
Feb 19, 2024
4aa5aa5
failed EXP01 task
Feb 19, 2024
22c3974
pass EXP01 task : checkDuplicate Lottery created
Feb 19, 2024
01f7549
failed EXPO1 taks:shouldReturnCreatedNewLotteryRequest
Feb 19, 2024
45a08d2
pass EXP01 : shouldReturnCreatedNewLotteryRequest
Feb 19, 2024
130b7ec
pause
Feb 19, 2024
791781c
push and pause
Feb 19, 2024
cb817a5
resume
Feb 20, 2024
f9fda4e
failed EXP01 task: checkDuplicate 2nd
Feb 20, 2024
05c8b47
pass EXP01 task : checkDupliate 2nd
Feb 20, 2024
c02dada
failed EXP01 task : check notExist lettery ticket
Feb 20, 2024
d35c12f
pass EXP01 task : check notExist lettery ticket
Feb 20, 2024
132b07f
failed EXP01 task :check return response body
Feb 20, 2024
5ba829f
pass EXP01 task :check return response body
Feb 20, 2024
3cfb66d
finished EXP01 i guess (phewwww)
Feb 20, 2024
85eadd9
failed EXP02 task: should return null
Feb 20, 2024
1308a07
pass EXP02 task: should return non null
Feb 20, 2024
7784d51
pass EXP02 task: should return UserResponse
Feb 20, 2024
75e07f7
start EXP03
Feb 20, 2024
86bf829
failed EXP03 : Connect with User_ticket shouldNotReturnANull
Feb 20, 2024
e1a27d5
pass EXP03 : Connect with User_ticket shouldNotReturnANull
Feb 20, 2024
e4c144a
pause at EXP03
Feb 20, 2024
1c527d0
EXP03 test 2nd
Feb 21, 2024
644d413
failed EXP03 test: shouldReturn HTTPStatusOK
Feb 21, 2024
3b107aa
pass EXP03 test: shouldReturn HTTPStatusOK
Feb 21, 2024
01b4b23
failed EXP03 test: shouldReturn HTTPStatusOK and Body
Feb 21, 2024
82a0968
pass EXP03 test: shouldReturn HTTPStatusOK and Body
Feb 21, 2024
36c7146
failed EXP03 test: shouldReturn Body with Profile
Feb 21, 2024
d8f0a6e
pass EXP03 test: shouldReturn Body with Profile
Feb 21, 2024
033c2e7
failed EXP03 test: shouldReturn Body with Id from user_ticket
Feb 21, 2024
fa80da3
pass EXP03 test: shouldReturn Body with Id from user_ticket
Feb 21, 2024
c845db1
Pass EXP03
Feb 21, 2024
b8230e4
failed EXP04 test:shouldReturn Status OK
Feb 21, 2024
e7ccf80
pass EXP04 test:shouldReturn Status OK
Feb 21, 2024
aa1b18a
failed EXP04 test:shouldReturn Status OK with correct path variable
Feb 21, 2024
d8ba931
pass EXP04 test:shouldReturn Status OK with correct path variable
Feb 21, 2024
f291d46
failed EXP04 test: get all lottery ticket by user
Feb 21, 2024
4f00f37
pass EXP04 test: get all lottery ticket by user
Feb 21, 2024
8e02363
restructure test
Feb 21, 2024
5e22497
pause
Feb 21, 2024
4fd6bbc
falied EXP04 : exist userId return true
Feb 21, 2024
92c6d37
pass EXP04 : exist userId return true
Feb 21, 2024
29d294e
failed EXP04 : return List of lottery that existedUserId have
Feb 21, 2024
a4f1477
pass EXP04 : return List of lottery that existedUserId have
Feb 21, 2024
4e65d11
finished EXP04
Feb 21, 2024
8c935cf
failed EXP05 : shouldReturnStatus OK
Feb 22, 2024
f53bd2c
pass EXP05 : shouldReturnStatus OK
Feb 22, 2024
48d1ddd
failed Should return NOT_FOUND when selling back a non-existent lotte…
Feb 22, 2024
137a041
pass Should return NOT_FOUND when selling back a non-existent lottery…
Feb 22, 2024
d8467eb
failed EXP05 : Should return the lottery associated with the given us…
Feb 22, 2024
4e84d09
pass EXP05 : Should return the lottery associated with the given user ID
Feb 22, 2024
3ad2043
failed EXP05 :Should return a list of lotteries associated with the g…
Feb 22, 2024
ba97225
pass EXP05 :Should return a list of lotteries associated with the giv…
Feb 22, 2024
e659c83
start config container
Feb 23, 2024
42c70ce
finish EXP05
Feb 24, 2024
d64a579
test case all pass
Feb 24, 2024
6ebb6a5
finist api design 00
Feb 24, 2024
78eeb80
a
Feb 24, 2024
21d86fd
add GlobalExceptionHandler
Feb 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions posttest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# stage-1 build artifact
FROM amazoncorretto:17.0.9-alpine3.18

WORKDIR /app
ADD . .
RUN ["./gradlew","bootJar"]


# stage-2 running image
FROM gcr.io/distroless/java17-debian12:latest
WORKDIR /app
COPY --from=builder ""/app/build/libs/lottery-0.0.1-SNAPSHOT.jar lottery-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "lottery-0.0.1-SNAPSHOT.jar"]
51 changes: 48 additions & 3 deletions posttest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,59 @@ repositories {
}

dependencies {
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.postgresql:postgresql'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-validation'

implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.security:spring-security-test'
}

sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/test/java')
}
}
}

tasks.register('integrationTest', Test) {
// to prevent Mockito's inline mock maker fail on container
jvmArgs('-Djdk.attach.allowAttachSelf=true', '-XX:+StartAttachListener')

description = 'Runs integration tests.'
group = 'verification'
useJUnitPlatform()
filter {
include '**/*IntegrationTest.*'
}
}

tasks.named('test') {
filter {
exclude '**/*IntegrationTest.*'
}
// to prevent Mockito's inline mock maker fail on container
jvmArgs('-Djdk.attach.allowAttachSelf=true', '-XX:+StartAttachListener')
useJUnitPlatform()
}

test {
testLogging {
events "passed", "skipped", "failed" //, "standardOut", "standardError"

showExceptions true
exceptionFormat "full"
showCauses true
showStackTraces true

// Change to `true` for more verbose test output
showStandardStreams = false
}
}
17 changes: 17 additions & 0 deletions posttest/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3'

services:
postgres:
image: postgres:latest
environment:
POSTGRES_DB: lottery
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
# - pgdata:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "5432:5432"

volumes:
pgdata:
27 changes: 27 additions & 0 deletions posttest/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Drop tables if they exist
DROP TABLE IF EXISTS lottery CASCADE;
DROP TABLE IF EXISTS user_ticket CASCADE;

CREATE TABLE user_ticket (
id SERIAL PRIMARY KEY,
userId VARCHAR(10) UNIQUE NOT NULL,
phone VARCHAR(20) NOT NULL,
name VARCHAR(20) NOT NULL
);


CREATE TABLE lottery (
id SERIAL PRIMARY KEY,
ticket VARCHAR(6) UNIQUE NOT NULL,
price DOUBLE PRECISION NOT NULL,
amount BIGINT NOT NULL,
profile_id VARCHAR(255) REFERENCES user_ticket(userId) ON DELETE CASCADE
);

-- Initial data
INSERT INTO user_ticket(userId, phone, name ) VALUES('1234567890', '012345678', 'Foo');
INSERT INTO user_ticket(userId, phone, name ) VALUES('0987654321', '12345678', 'Sally');
INSERT INTO lottery(ticket, price, amount, profile_id) VALUES('111111', 120, 1,'1234567890');
INSERT INTO lottery(ticket, price, amount, profile_id) VALUES('222222', 100, 2,'1234567890');
INSERT INTO lottery(ticket, price, amount, profile_id) VALUES('333333', 100, 2,'0987654321');
INSERT INTO lottery(ticket, price, amount, profile_id) VALUES('444444', 100, 2,'0987654321');
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.kbtg.bootcamp.posttest.admin;

import com.kbtg.bootcamp.posttest.lottery.Lottery;
import com.kbtg.bootcamp.posttest.lottery.LotteryResponse;
import com.kbtg.bootcamp.posttest.lottery.LotteryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;
@RestController
@RequestMapping("/admin")
public class AdminController {

@Autowired
private final LotteryService lotteryService;

public AdminController(LotteryService lotteryService) {
this.lotteryService = lotteryService;
}

@GetMapping("")
public List<Lottery> showAllLotteryPage() {
return
lotteryService.getAllLotteries();
}

@PostMapping("/lotteries")
public ResponseEntity<?> addNewLotteryTicketToDb(
@RequestBody AdminRequest request
) {
return
lotteryService.createLottery(request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kbtg.bootcamp.posttest.admin;

import jakarta.validation.constraints.Size;
import org.springframework.lang.NonNull;

public record AdminRequest(@NonNull @Size(min = 6, max = 6)String ticket, Double price, Long amount) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kbtg.bootcamp.posttest.exception;

public class DuplicateLotteryException extends RuntimeException {
public DuplicateLotteryException(String string) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.kbtg.bootcamp.posttest.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(DuplicateLotteryException.class)
public ResponseEntity<String> handleDuplicateLotteryException(DuplicateLotteryException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}

@ExceptionHandler(NotExistUserIdException.class)
public ResponseEntity<String> handleNotExistUserIdException(NotExistUserIdException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}

@ExceptionHandler(NotExistLotteryException.class)
public ResponseEntity<String> handleNotExistLotteryNumberException(NotExistLotteryException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}

@ExceptionHandler(LotteryNotBelongToUserException.class)
public ResponseEntity<String> handleLotteryNotBelongToUserException(LotteryNotBelongToUserException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kbtg.bootcamp.posttest.exception;

public class LotteryNotBelongToUserException extends RuntimeException {
public LotteryNotBelongToUserException(String s) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kbtg.bootcamp.posttest.exception;

public class NotExistLotteryException extends RuntimeException {
public NotExistLotteryException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kbtg.bootcamp.posttest.exception;

public class NotExistUserIdException extends RuntimeException {
public NotExistUserIdException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.kbtg.bootcamp.posttest.lottery;

import com.kbtg.bootcamp.posttest.profile.Profile;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;


@Entity
@Table(name = "lottery")
public class Lottery {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@NotNull
@Size(min = 6, max = 6)
@Pattern(regexp = "\\d{6}", message = "Ticket must be exactly 6 digits")
private String ticket;

private Double price;
private Long amount;

@Override
public String toString() {
return "Lottery{" +
"ticket='" + ticket + '\'' +
'}';
}

@ManyToOne
@JoinColumn(name = "profile_id", referencedColumnName = "userId")
private Profile profile;

public Lottery() {}

public Lottery(String ticket, Double price, Long amount) {
this.ticket = ticket;
this.price = price;
this.amount = amount;
}

public Lottery(String ticket, Double price, Long amount, Profile profile) {
this.ticket = ticket;
this.price = price;
this.amount = amount;
this.profile =profile;
}

public String getTicket() {
return ticket;
}

public void setTicket(String ticket) {
this.ticket = ticket;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

public Long getAmount() {
return amount;
}

public Integer getId() {
return id;
}

public void setAmount(Long amount) {
this.amount = amount;
}

public Profile getProfile() {
return profile;
}

public void setProfile(Profile profile) {
this.profile = profile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.kbtg.bootcamp.posttest.lottery;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LotteryRepository extends JpaRepository<Lottery, Long> {
Lottery findByTicket(String ticket);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kbtg.bootcamp.posttest.lottery;

public class LotteryResponse {
private String ticket;

public LotteryResponse(String ticket) {
this.ticket = ticket;
}

public String getTicket() {
return ticket;
}

public void setTicket(String ticket) {
this.ticket = ticket;
}
}
Loading