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 #18

Open
wants to merge 8 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
14 changes: 14 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/study_api_call.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions hn/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

implementation platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.5")
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'

implementation 'org.apache.commons:commons-math3:3.6.1'

testImplementation "org.springframework.cloud:spring-cloud-contract-wiremock:3.1.5"
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.5'

}

tasks.named('test') {
Expand Down
2 changes: 2 additions & 0 deletions hn/src/main/java/com/example/hn/HnApplication.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 HnApplication {

public static void main(String[] args) {
Expand Down
13 changes: 13 additions & 0 deletions hn/src/main/java/com/example/hn/homework1/api/YkApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.hn.homework1.api;

import com.example.hn.homework1.response.YkServiceResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


@FeignClient(name = "homework1-yk", url = "${feign.ykapi-url}", primary = false)
public interface YkApi {
@GetMapping(value = "/api/cal/{x}")
YkServiceResponse getAnswer(@PathVariable("x") int x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.hn.homework1.controller;

import com.example.hn.homework1.response.ABResponse;
import com.example.hn.homework1.response.ApiResponse;
import com.example.hn.homework1.service.HomeworkService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class HomeworkController {

private final HomeworkService homeworkService;

@GetMapping("/hn/homework1")
public ApiResponse getAnswer() {

ABResponse response = homeworkService.getAnswer();
return ApiResponse.ok(response);
}
}
17 changes: 17 additions & 0 deletions hn/src/main/java/com/example/hn/homework1/response/ABResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.hn.homework1.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@AllArgsConstructor
public class ABResponse {
private int a;
private int b;

public static ABResponse of(int a, int b) {
return new ABResponse(a, b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.hn.homework1.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Getter
@AllArgsConstructor
@ToString
public class ApiResponse<T> {

private final String message;
private final int code;
private final T data;

public static <T> ApiResponse<T> ok(T data){
return new ApiResponse<>(
"성공",
200,
data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.hn.homework1.response;

import lombok.*;
import org.springframework.web.bind.annotation.ResponseBody;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class YkServiceResponse {
private int answer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.example.hn.homework1.service;

import com.example.hn.homework1.response.ABResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.print.attribute.IntegerSyntax;
import java.util.Arrays;
import java.util.stream.IntStream;

import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.MatrixUtils;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.linear.SingularMatrixException;


@Service
@RequiredArgsConstructor
@Slf4j
public class HomeworkService {

private final YkService ykService;

public ABResponse getAnswer() {
int x1 = 2;
int x2 = 3;
int answer1 = ykService.getAnswer(x1);
int answer2 = ykService.getAnswer(x2);

double[][] matrixData = {
{x1 * x1, x1},
{x2 * x2, x2}
};
log.debug("입력 행렬:{}", printMatrix(matrixData));

double[] resultData = {answer1, answer2};

RealMatrix matrix = new Array2DRowRealMatrix(matrixData);
RealMatrix resultMatrix = new Array2DRowRealMatrix(resultData);

RealMatrix inverseMatrix = MatrixUtils.inverse(matrix);
log.debug("역행렬:{}", printMatrix(inverseMatrix.getData()));

RealMatrix solutionMatrix = inverseMatrix.multiply(resultMatrix);
double[] solution = solutionMatrix.getColumn(0);
int a = (int) Math.round(solution[0]);
int b = (int) Math.round(solution[1]);
log.debug("a:{}, b:{}", a, b);

return ABResponse.of(a, b);
}

private String printMatrix(double[][] matrix) {
StringBuilder str = new StringBuilder();
IntStream.range(0, matrix.length)
.forEach(e -> str.append(Arrays.toString(matrix[e]) + "\n"));

return str.toString();
}
}
20 changes: 20 additions & 0 deletions hn/src/main/java/com/example/hn/homework1/service/YkService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.hn.homework1.service;

import com.example.hn.homework1.api.YkApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
@Slf4j
public class YkService {

private final YkApi ykApi;

public int getAnswer(int x) {
int answer = ykApi.getAnswer(x).getAnswer();
log.debug("answer:{}", answer);
return answer;
}
}
1 change: 0 additions & 1 deletion hn/src/main/resources/application.properties

This file was deleted.

9 changes: 9 additions & 0 deletions hn/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server:
port: 8082

feign:
ykapi-url: http://localhost:8081

#logging:
# level:
# root: debug
26 changes: 26 additions & 0 deletions hn/src/test/java/com/example/hn/MockYkApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.hn;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.ResourceUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MockYkApi {

public static void setupGetAnswerResponse(WireMockServer wireMockServer, String url) throws IOException {
Path file = ResourceUtils.getFile("classpath:test-response/ykapi-response.json").toPath();
wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(url))
.willReturn(
WireMock.aResponse()
.withStatus(HttpStatus.OK.value())
.withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.withBody(Files.readAllBytes(file))
)
);
}
}
15 changes: 15 additions & 0 deletions hn/src/test/java/com/example/hn/WireMockConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.hn;

import com.github.tomakehurst.wiremock.WireMockServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;

@TestConfiguration
public class WireMockConfig {

@Bean(initMethod = "start", destroyMethod = "stop")
public WireMockServer ykServiceMock() {
return new WireMockServer(8081);
}
}
7 changes: 7 additions & 0 deletions hn/src/test/java/com/example/hn/homework1/api/YkApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.hn.homework1.api;

import static org.junit.jupiter.api.Assertions.*;

class YkApiTest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.hn.homework1.service;

import static org.junit.jupiter.api.Assertions.*;

class HomeworkServiceTest {

}
Loading