Skip to content

Commit

Permalink
#27 Feat: QnetCertificateStatusTest μˆ˜μ •
Browse files Browse the repository at this point in the history
  • Loading branch information
pjhcsols committed Oct 6, 2024
1 parent 0c043ea commit 7198f72
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 53 deletions.
1 change: 1 addition & 0 deletions .idea/modules.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/web3-credential-server.main.iml

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

Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
//import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

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

@SpringBootTest
@SpringBootApplication
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class QnetCertificateStatusTest {

@Autowired
Expand All @@ -28,63 +35,133 @@ public class QnetCertificateStatusTest {
private static final String ACCESS_TOKEN_URL = "https://oauth.codef.io/oauth/token";
private static final String API_URL = "https://development.codef.io/v1/kr/etc/hr/qnet-certificate/status"; // μˆ˜μ •λœ 도메인

private static final String CLIENT_ID = "9f515c3f-8df3-41b7-9da1-e08192131b3d";
private static final String CLIENT_SECRET = "d0c5a8b8-2858-4059-acff-289f42892f47";
private static final String CLIENT_ID = "86640213-3b83-461a-97ab-2491d68a2052";
private static final String CLIENT_SECRET = "8721d0b3-37ea-4484-8d65-6418a61fd1a1";

@Test
void shouldCheckQnetCertificateStatusSuccessfully() throws Exception {

@Test //성곡은 ν•˜λŠ”λ° κ²°κ³Όκ°€ μ•”ν˜Έν™”λ˜μ„œ λ‚˜μ˜΄
void shouldCheckQnetCertificateStatusSuccessfully() throws UnsupportedEncodingException {

//Get Access Token
// Get Access Token
HashMap<String, String> tokenResponse = publishToken(CLIENT_ID, CLIENT_SECRET);
assertThat(tokenResponse).isNotNull();
String accessToken = tokenResponse.get("access_token");
assertThat(accessToken).isNotNull();

// JSON Request Payload
String requestBody = "{\n" +
" \"organization\": \"0001\",\n" +
" \"userName\": \"김건아\",\n" +
//" \"docNo\": \"2024031002110534064\"\n" + //이건 λ™μž‘ν•¨(근데 "resIssueYN":"0"라고 뜸)
" \"docNo\": \"2024082120452245739\"\n" + //이건 였λ₯˜λ‚¨- μ‹€μ œ 였늘 λ°œκΈ‰λ°›μ€ λ¬Έμ„œ 번호인데,, μ™œ μ•ˆλ˜λŠ”μ§€ λͺ¨λ₯΄κ² μŒ
" \"docNo\": \"2024082120452245739\"\n" +
"}";

// Set Headers
// HTTP μš”μ²­ 헀더 μ„€μ •
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBearerAuth(accessToken);

// Create HttpEntity
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);

// Send POST request
ResponseEntity<String> response = restTemplate.exchange(
"https://development.codef.io/v1/kr/etc/hr/qnet-certificate/status",
HttpMethod.POST,
request,
String.class
);
// HttpEntity 객체 생성 (μš”μ²­ 바디와 헀더 포함)
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);

// λ‘œκ·Έμ— 응닡 λ³Έλ¬Έ 좜λ ₯
System.out.println("Response Body: " + response.getBody());

// Validate Response
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).contains("resIssueYN");
try {
// API μ—”λ“œν¬μΈνŠΈλ‘œ POST μš”μ²­ 전솑
ResponseEntity<String> response = restTemplate.exchange(
API_URL,
HttpMethod.POST,
requestEntity,
String.class
);

// μΈμ½”λ”©λœ 응닡 λ³Έλ¬Έ 좜λ ₯
String encodedResponseBody = response.getBody();
System.out.println("Encoded Response Body: " + encodedResponseBody);

// Content-Type 확인
MediaType contentType = response.getHeaders().getContentType();
System.out.println("Content-Type: " + contentType);

// 응닡이 URL μΈμ½”λ”©λœ λ¬Έμžμ—΄λ‘œ μ „μ†‘λœ 경우, 이λ₯Ό λ””μ½”λ”©ν•˜μ—¬ JSON으둜 λ³€ν™˜
if (encodedResponseBody != null && contentType != null && contentType.toString().contains("text/plain")) {
// 1. ISO-8859-1둜 μΈμ½”λ”©λœ 데이터λ₯Ό λ¨Όμ € UTF-8둜 λ³€ν™˜
String isoDecodedResponseBody = new String(encodedResponseBody.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
System.out.println("ISO Decoded Response Body: " + isoDecodedResponseBody);

// 2. URL λ””μ½”λ”©
String urlDecodedResponseBody = URLDecoder.decode(isoDecodedResponseBody, StandardCharsets.UTF_8.name());
System.out.println("URL Decoded Response Body: " + urlDecodedResponseBody);

// 3. JSON νŒŒμ‹±
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> parsedResponseBody = objectMapper.readValue(urlDecodedResponseBody, new TypeReference<Map<String, Object>>() {});

// λ””μ½”λ”© ν›„ JSON 응닡 검증
assertThat(parsedResponseBody).containsKey("data");
Map<String, Object> data = (Map<String, Object>) parsedResponseBody.get("data");

// μ›ν•˜λŠ” ν•„λ“œ 검증
assertThat(data).containsKey("resIssueYN");
assertThat(data.get("resIssueYN")).isInstanceOf(String.class);
String resIssueYN = (String) data.get("resIssueYN");
assertThat(resIssueYN).isNotEmpty();

// μ˜ˆμ‹œ 좜λ ₯
System.out.println("Parsed JSON Data: " + data);

// URL λ””μ½”λ”©λœ λ©”μ‹œμ§€ 좜λ ₯
String decodedResponse = java.net.URLDecoder.decode(response.getBody(), "UTF-8");
System.out.println("Decoded Response Body: " + decodedResponse);
} else {
System.err.println("Unexpected content type or empty response: " + contentType);
}

// You can also add more specific assertions based on the expected response structure
// For example, checking if certain fields exist in the response
assertThat(decodedResponse).contains("resIssueYN");
assertThat(decodedResponse).contains("resDocType");
} catch (Exception e) {
// 였λ₯˜ 둜그 좜λ ₯
System.err.println("Exception: " + e.getMessage());
e.printStackTrace();
throw e;
}

// try {
// // API μ—”λ“œν¬μΈνŠΈλ‘œ POST μš”μ²­ 전솑
// ResponseEntity<String> response = restTemplate.exchange(
// API_URL,
// HttpMethod.POST,
// requestEntity,
// String.class
// );

// // λ¬Έμžμ—΄ 응닡 λ³Έλ¬Έ 좜λ ₯
// String responseBody = response.getBody();
// System.out.println("Response Body: " + responseBody);

// // Content-Type 확인
// MediaType contentType = response.getHeaders().getContentType();
// System.out.println("Content-Type: " + contentType);

// // 응닡 검증
// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
// assertThat(responseBody).isNotNull();

// // JSON νŒŒμ‹± μ‹œλ„ (Content-Type이 JSON일 경우만)
// if (contentType != null && contentType.equals(MediaType.APPLICATION_JSON)) {
// ObjectMapper objectMapper = new ObjectMapper();
// Map<String, Object> parsedResponseBody = objectMapper.readValue(responseBody, new TypeReference<Map<String, Object>>() {});

// // ν•„λ“œ 검증
// assertThat(parsedResponseBody).containsKey("resIssueYN");
// assertThat(parsedResponseBody.get("resIssueYN")).isInstanceOf(String.class);
// String resIssueYN = (String) parsedResponseBody.get("resIssueYN");
// assertThat(resIssueYN).isNotEmpty();
// } else {
// System.err.println("Unexpected content type: " + contentType);
// }

// } catch (Exception e) {
// // 였λ₯˜ 둜그 좜λ ₯
// System.err.println("Exception: " + e.getMessage());
// e.printStackTrace();
// throw e;
// }
}

protected static HashMap<String, String> publishToken(String clientId, String clientSecret) {
BufferedReader br = null;
try {
try (BufferedReader br = null) {
URL url = new URL(ACCESS_TOKEN_URL);
String params = "grant_type=client_credentials&scope=read";

Expand All @@ -107,31 +184,33 @@ protected static HashMap<String, String> publishToken(String clientId, String cl

int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
try (BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
StringBuilder responseStr = new StringBuilder();
String inputLine;
while ((inputLine = reader.readLine()) != null) {
responseStr.append(inputLine);
}

ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(responseStr.toString(), new TypeReference<HashMap<String, String>>() {});
}
} else {
System.out.println("Failed to get access token: HTTP error code : " + responseCode);
return null;
}

StringBuilder responseStr = new StringBuilder();
String inputLine;
while ((inputLine = br.readLine()) != null) {
responseStr.append(inputLine);
}

ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(responseStr.toString(), new TypeReference<HashMap<String, String>>() {});
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

@org.springframework.context.annotation.Configuration
static class TestConfig {
@Bean
@Primary
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
}

0 comments on commit 7198f72

Please sign in to comment.