-
Notifications
You must be signed in to change notification settings - Fork 3
/
RemoteRestAPITest.java
111 lines (85 loc) · 2.59 KB
/
RemoteRestAPITest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
*
*/
package com.example.restapi;
import java.nio.charset.Charset;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author 정명성
* create date : 2016. 2. 4.
* com.example.restapi.RemoteRestAPITest.java
*/
//@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RemoteRestAPITest {
Logger logger = LoggerFactory.getLogger(getClass());
RestTemplate restTemplate = new TestRestTemplate();
static String access_token;
HttpHeaders headers;
@Before
public void start() {
// header 셋팅
headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
}
/**
* RestTemplate를 이용한 API 점검
* @author 정명성
* create date : 2016. 2. 4.
* 설명
* @throws Exception
*/
//@Test
public void test1RestTemplate() throws Exception {
// 클라이언트 계정 셋팅
JSONObject param = new JSONObject();
param.put("clientId", "client_id");
param.put("secret", "client_secret");
param.put("serialNo", "number");
param.put("userId", "uid");
param.put("password", "password");
HttpEntity entity = new HttpEntity(param.toString(), headers);
String result = restTemplate.postForEntity("http://url:port",
entity,
String.class).getBody();
JSONParser parser = new JSONParser();
JSONObject parsing = (JSONObject)parser.parse(result);
// 토큰 저장
access_token = parsing.get("accessToken").toString();
assertNotNull(access_token);
logger.info(parsing.toJSONString());
}
/**
* 장비 리스트 가져오기
* @author 정명성
* create date : 2016. 2. 4.
* 설명
* @throws Exception
*/
//@Test
public void test2GetMachines() throws Exception {
assertNotNull(access_token);
// 인증 토큰 셋팅
headers.set("Authorization", access_token);
// 결과
String result = restTemplate.exchange("http://url:port",
HttpMethod.GET ,
new HttpEntity(headers),
String.class).getBody();
logger.info(result);
}
}