Qaf: QAF๋ฅผ ์‚ฌ์šฉํ•œ API ์ž๋™ํ™”

์— ๋งŒ๋“  2017๋…„ 03์›” 29์ผ  ยท  7์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: qmetry/qaf

์•ˆ๋…•ํ•˜์„ธ์š” ํŒ€,

QAF๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ API ์ž๋™ํ™”๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋ˆ„๊ตฌ๋“ ์ง€ ์•„๋ž˜ ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด ์ œ์•ˆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

  1. QAF์—์„œ GET ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๊ตฌ๋ฌธ ๋˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? GET ์˜ต์…˜ ๋ชฉ๋ก์—์„œ ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•ด์•ผ ํ• ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฐ์ƒท์„ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค.

--> new RestTestBase().getWebResource(getBundle().getString("ws.endurl", url), "/application.json")์—์„œ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. * * --> ์–ด๋–ค GET ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ๊ฐ€?

  1. ์‘๋‹ต์„ ๋ฐ›์€ ํ›„ JSON ์‘๋‹ต์—์„œ ๋‚ด์šฉ์„ ์ถ”์ถœํ•˜๊ณ  QAF๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

๋ฏธ๋ฆฌ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค!!

๋ฌธ์•ˆ ์ธ์‚ฌ,
ํ”„๋ ˜
capture_webserviceget

help wanted webservice

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์•ˆ๋…•ํ•˜์„ธ์š” @APrem

HTTP ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ธ์ฆ์„œ๋ฅผ ์‹ ๋ขฐํ•˜์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด - ์—ฌ๊ธฐ์—์„œ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์„ ์‚ฌ์šฉํ•˜์—ฌ RestClientFactory๋ฅผ ํ™•์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
https://gist.github.com/outbounder/1069465

๋”ฐ๋ผ์„œ ํ™•์žฅ ํด๋ž˜์Šค๋Š” ์•„๋ž˜์™€ ๊ฐ™์„ ๊ฒƒ์ด๊ณ  ์ด๊ฒƒ์„ ์ž๋ฐ” ํŒจํ‚ค์ง€ ์•ˆ์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
`ํŒจํ‚ค์ง€;

com.qmetry.qaf.automation.ws.rest.RestClientFactory ๊ฐ€์ ธ์˜ค๊ธฐ;
com.sun.jersey.api.client.Client ๊ฐ€์ ธ์˜ค๊ธฐ;
com.sun.jersey.api.client.config.ClientConfig ๊ฐ€์ ธ์˜ค๊ธฐ;
com.sun.jersey.api.client.config.DefaultClientConfig ๊ฐ€์ ธ์˜ค๊ธฐ;
com.sun.jersey.client.urlconnection.HTTPSProperties ๊ฐ€์ ธ์˜ค๊ธฐ;

๊ฐ€์ ธ์˜ค๊ธฐ javax.net.ssl.*;
๊ฐ€์ ธ์˜ค๊ธฐ java.security.SecureRandom;
import java.security.cert.CertificateException;
๊ฐ€์ ธ์˜ค๊ธฐ java.security.cert.X509Certificate;

๊ณต๊ฐœ ํด๋ž˜์Šค ClientHelper๋Š” RestClientFactory {๋ฅผ ํ™•์žฅํ•ฉ๋‹ˆ๋‹ค.

public static ClientConfig configureClient() {
    TrustManager[ ] certs = new TrustManager[ ] {
            new X509TrustManager() {
                <strong i="24">@Override</strong>
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                <strong i="25">@Override</strong>
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
                <strong i="26">@Override</strong>
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
            }
    };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (java.security.GeneralSecurityException ex) {
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

    ClientConfig config = new DefaultClientConfig();
    try {
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(
                new HostnameVerifier() {
                    <strong i="27">@Override</strong>
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                },
                ctx
        ));
    } catch(Exception e) {
    }
    return config;
}

<strong i="28">@Override</strong>
protected  Client createClient() {
    return Client.create(ClientHelper.configureClient());
}

}`

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์†์„ฑ ๋‚ด์— ์ด ํด๋ž˜์Šค๋ฅผ ๋“ฑ๋กํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
rest.client.impl=<Your Package Name>.ClientHelper

๋ชจ๋“  7 ๋Œ“๊ธ€

POJO๊ฐ€ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ํด๋ž˜์Šค ๋˜๋Š” String.class ๋˜๋Š” ์ผ๋ฐ˜ ์‘๋‹ต ClientResponse.class

์‚ฌ์šฉ ์ค€๋น„ ๋‹จ๊ณ„์— ws-support ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‘๋‹ตํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

์ฝ”๋“œ์˜ ์•„๋ž˜ ๋ถ€๋ถ„์ด ๋„์›€์ด ๋˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค.
๋งค๊ฐœ๋ณ€์ˆ˜

What's ----> ๋ฌธ์ž์—ด ๋ฆฌ์†Œ์Šค
๋ฌด์—‡ ---->์ง€๋„๋งค๊ฐœ๋ณ€์ˆ˜

private static void requestFor(๋ฌธ์ž์—ด ๋ฆฌ์†Œ์Šค, ๋งต๋งค๊ฐœ๋ณ€์ˆ˜) {

  • WebResource webResource = ์ƒˆ๋กœ์šด RestTestBase().getWebResource(
  • getBundle().getString("ws.endurl", ApplicationProperties.SELENIUM_BASE_URL.getStringVal()), ๋ฆฌ์†Œ์Šค);
  • if (null != params && !params.isEmpty()) {
  • ๋‹ค์ค‘๊ฐ’ ๋งตmparams = ์ƒˆ๋กœ์šด MultivaluedMapImpl();


    • (๋ฌธ์ž์—ด ํ‚ค : params.keySet()) {

  • mparams.add(ํ‚ค, params.get(ํ‚ค));
  • }
  • webResource = webResource.queryParams(mparams);
  • }
  • webResource.get(ClientResponse.class);
  • }

์šฐ๋ฆฌ๋Š” ์ฝ”๋“œ์˜ ์•„๋ž˜ ๋ถ€๋ถ„์œผ๋กœ ์‹œ๋„ํ–ˆ๊ณ  ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

์‹œ๋„ํ•œ ์Šคํฌ๋ฆฝํŠธ:

    WebResource webResource = new RestTestBase().getWebResource(getBundle().getString("ws.endurl", url));
    webResource.get(ClientResponse.class);

์˜ค๋ฅ˜:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX ๊ฒฝ๋กœ ๊ตฌ์ถ• ์‹คํŒจ:
sun.security.provider.certpath.SunCertPathBuilderException: ์š”์ฒญ๋œ ๋Œ€์ƒ์— ๋Œ€ํ•œ ์œ ํšจํ•œ ์ธ์ฆ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์•ˆ๋…•ํ•˜์„ธ์š” @APrem

HTTP ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ธ์ฆ์„œ๋ฅผ ์‹ ๋ขฐํ•˜์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด - ์—ฌ๊ธฐ์—์„œ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์„ ์‚ฌ์šฉํ•˜์—ฌ RestClientFactory๋ฅผ ํ™•์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
https://gist.github.com/outbounder/1069465

๋”ฐ๋ผ์„œ ํ™•์žฅ ํด๋ž˜์Šค๋Š” ์•„๋ž˜์™€ ๊ฐ™์„ ๊ฒƒ์ด๊ณ  ์ด๊ฒƒ์„ ์ž๋ฐ” ํŒจํ‚ค์ง€ ์•ˆ์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
`ํŒจํ‚ค์ง€;

com.qmetry.qaf.automation.ws.rest.RestClientFactory ๊ฐ€์ ธ์˜ค๊ธฐ;
com.sun.jersey.api.client.Client ๊ฐ€์ ธ์˜ค๊ธฐ;
com.sun.jersey.api.client.config.ClientConfig ๊ฐ€์ ธ์˜ค๊ธฐ;
com.sun.jersey.api.client.config.DefaultClientConfig ๊ฐ€์ ธ์˜ค๊ธฐ;
com.sun.jersey.client.urlconnection.HTTPSProperties ๊ฐ€์ ธ์˜ค๊ธฐ;

๊ฐ€์ ธ์˜ค๊ธฐ javax.net.ssl.*;
๊ฐ€์ ธ์˜ค๊ธฐ java.security.SecureRandom;
import java.security.cert.CertificateException;
๊ฐ€์ ธ์˜ค๊ธฐ java.security.cert.X509Certificate;

๊ณต๊ฐœ ํด๋ž˜์Šค ClientHelper๋Š” RestClientFactory {๋ฅผ ํ™•์žฅํ•ฉ๋‹ˆ๋‹ค.

public static ClientConfig configureClient() {
    TrustManager[ ] certs = new TrustManager[ ] {
            new X509TrustManager() {
                <strong i="24">@Override</strong>
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                <strong i="25">@Override</strong>
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
                <strong i="26">@Override</strong>
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
            }
    };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (java.security.GeneralSecurityException ex) {
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

    ClientConfig config = new DefaultClientConfig();
    try {
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(
                new HostnameVerifier() {
                    <strong i="27">@Override</strong>
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                },
                ctx
        ));
    } catch(Exception e) {
    }
    return config;
}

<strong i="28">@Override</strong>
protected  Client createClient() {
    return Client.create(ClientHelper.configureClient());
}

}`

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์†์„ฑ ๋‚ด์— ์ด ํด๋ž˜์Šค๋ฅผ ๋“ฑ๋กํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
rest.client.impl=<Your Package Name>.ClientHelper

์•ˆ๋…•ํ•˜์„ธ์š” ํ”„๋ ˜์ž…๋‹ˆ๋‹ค.
์œ„์˜ ์˜๊ฒฌ์ด ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๋„์›€์ด ๋˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค.

QAF ํƒœ๊ทธ๊ฐ€ ์žˆ๋Š” stackoverflow ๋˜๋Š” ๊ทธ๋ฃน์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๊ฒƒ๊ณผ ๊ฐ™์€ ์ปค๋ฎค๋‹ˆํ‹ฐ ์ง€์› ํฌ๋Ÿผ์œผ๋กœ ์งˆ๋ฌธ์„ ์ด๋™ํ•ด ๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. stackoverflow์— ์š”์ฒญํ•  ๋•Œ qaf ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋น ๋ฅธ ์‘๋‹ต์„ ์–ป์œผ์‹ญ์‹œ์˜ค.

๊ฐ์‚ฌ ํ•ด์š”,
์น˜๋ผ๊ทธ

์‘๋‹ตํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

ClientHelper ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ–ˆ์œผ๋ฉฐ ์ด์ œ ์‘๋‹ต์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ๋‚ด๋ถ€์— ์ œ๊ณต๋œ URL์€ .getWebResource(getBundle().getString("ws.endurl", url)); api ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌผ์Œํ‘œ ๊ธฐํ˜ธ "?" URL์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ ์กฐ๊ฐ์€ "?" "%3F"๋กœ ์ง€์ •ํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ˜์†”์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ƒ: https://sample.url.com/xxxxxxxxxxxxx?apikey=l7xxfc8df89f313944a89e280481e09bb411
์‹ค์ œ: https://sample.url.com/xxxxxxxxxxxxxx%3Fapikey=l7xxfc8df89f313944a89e280481e09bb411

์ด ๋ณ€ํ™˜์œผ๋กœ ์ธํ•ด ์ถœ๋ ฅ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ฝ˜์†” ์ถœ๋ ฅ:
1 * ํด๋ผ์ด์–ธํŠธ ์•„์›ƒ๋ฐ”์šด๋“œ ์š”์ฒญ
1 >> https://sample.url.com/xxxxxxxxxxxxx%3Fapikey=l7xxfc8df89f313944a89e280481e09bb411 ๊ฐ€์ ธ ์˜ค๊ธฐ
1 * ์ˆ˜์‹  ์‹œ์ž‘: 87
1 * ์ˆ˜์‹ : ๋ธํƒ€: 87๋ฐ”์ดํŠธ: 87
์™„๋ฃŒ1 * ์™„๋ฃŒ
1 * ํด๋ผ์ด์–ธํŠธ ์ธ๋ฐ”์šด๋“œ ์‘๋‹ต
1 << 401
1 << ์„œ๋ฒ„: Apache-Coyote/1.1
1 << ์ฝ˜ํ…์ธ  ๊ธธ์ด: 87
1 << ๋‚ ์งœ: 2017๋…„ 3์›” 31์ผ ๊ธˆ์š”์ผ 14:39:19 GMT
1 << ์ฝ˜ํ…์ธ  ์œ ํ˜•: text/plain;charset=UTF-8
1 <<
{
"์˜ค๋ฅ˜": [
{
"message": "์ž˜๋ชป๋œ apikey",
"์ฝ”๋“œ": 4010
}
]
}

์•„๋ž˜์™€ ๊ฐ™์ด Header ๊ฐ์ฒด๊ฐ€ ์žˆ๋Š” apikey๋ฅผ ์ „๋‹ฌํ–ˆ๊ณ  ์‘๋‹ต์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๋นŒ๋” ๋นŒ๋” = webResource.getRequestBuilder();
builder.header(apikey, apiValue);
builder.get(ClientResponse.class);

๊ฐ์‚ฌ ํ•ด์š”!

@cjayswal @thetechnocrat ์ง€์› ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰