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

최재영 과제 1차 제출 #20

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions cjy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

//openFeign
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '3.1.3'

}

tasks.named('test') {
Expand Down
2 changes: 2 additions & 0 deletions cjy/src/main/java/com/study/cjy/CjyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class CjyApplication {

public static void main(String[] args) {
Expand Down
20 changes: 20 additions & 0 deletions cjy/src/main/java/com/study/cjy/CjyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.study.cjy;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class CjyController {
private final CjyService cjyService;

@GetMapping("/getNum")
public ResponseEntity<ResponseDto> getNum() {
ResponseDto result = cjyService.algorithm();
return ResponseEntity.ok(result);
}
}
19 changes: 19 additions & 0 deletions cjy/src/main/java/com/study/cjy/CjyService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.study.cjy;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CjyService {
private final YkOpenFeign openFeign;
public ResponseDto algorithm() {
int xIsOne = openFeign.getResult(1);
int xIsTwo = openFeign.getResult(2);

int a = (xIsTwo - 2 * xIsOne) / 2;
int b = xIsOne - a;

return ResponseDto.of(a, b);
}
}
17 changes: 17 additions & 0 deletions cjy/src/main/java/com/study/cjy/ResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.study.cjy;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class ResponseDto {
private int a;
private int b;

public static ResponseDto of(int a, int b) {
ResponseDto responseDto = new ResponseDto(a, b);
return responseDto;
}
}
11 changes: 11 additions & 0 deletions cjy/src/main/java/com/study/cjy/YkOpenFeign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.study.cjy;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "YkOpenFeign", url = "http://localhost:8081")
public interface YkOpenFeign {
@GetMapping("/api/cal/{x}")
int getResult(@PathVariable("x") int x);
}
33 changes: 33 additions & 0 deletions cjy/src/test/java/com/study/cjy/CjyServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.study.cjy;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CjyServiceTest {

@InjectMocks
CjyService cjyService;

@Mock
YkOpenFeign ykOpenFeign;

@Test
@DisplayName("과제1 서비스 코드 테스트")
void test1() {
when(ykOpenFeign.getResult(1)).thenReturn(2); // a + b = 2
when(ykOpenFeign.getResult(2)).thenReturn(6); // 4a + 2b = 6

ResponseDto responseDto = cjyService.algorithm();

assertThat(responseDto.getA()).isEqualTo(1);
assertThat(responseDto.getB()).isEqualTo(1);
}
}