Skip to content

Commit 7e7ccc7

Browse files
aietcnpivovarit
authored andcommitted
BAEL-1416 quick guide to kong (eugenp#3448)
* BAEL-1416 quick guide to kong * refactor kong samples
1 parent 3ecab4a commit 7e7ccc7

11 files changed

Lines changed: 471 additions & 1 deletion

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.kong;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
/**
9+
* @author aiet
10+
*/
11+
@RestController
12+
@RequestMapping("/stock")
13+
public class QueryController {
14+
15+
private static int REQUEST_COUNTER = 0;
16+
17+
@GetMapping("/reqcount")
18+
public int getReqCount(){
19+
return REQUEST_COUNTER;
20+
}
21+
22+
@GetMapping("/{code}")
23+
public String getStockPrice(@PathVariable String code){
24+
REQUEST_COUNTER++;
25+
if("BTC".equalsIgnoreCase(code))
26+
return "10000";
27+
else return "N/A";
28+
}
29+
30+
31+
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.kong;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class StockApp {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(StockApp.class, args);
11+
}
12+
13+
}

‎spring-boot/src/main/resources/application.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
server.port=8080
1+
server.port=9090
22
server.contextPath=/springbootapp
33
management.port=8081
44
management.address=127.0.0.1
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.baeldung.kong;
2+
3+
import com.baeldung.kong.domain.APIObject;
4+
import com.baeldung.kong.domain.ConsumerObject;
5+
import com.baeldung.kong.domain.KeyAuthObject;
6+
import com.baeldung.kong.domain.PluginObject;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.boot.test.web.client.TestRestTemplate;
13+
import org.springframework.http.*;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
16+
import java.net.URI;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertTrue;
20+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
21+
22+
/**
23+
* @author aiet
24+
*/
25+
@RunWith(SpringRunner.class)
26+
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class)
27+
public class KongAdminAPILiveTest {
28+
29+
private String getStockPrice(String code) {
30+
try {
31+
return restTemplate.getForObject(new URI("http://localhost:8080/stock/" + code), String.class);
32+
} catch (Exception ignored) {
33+
}
34+
return null;
35+
}
36+
37+
@Before
38+
public void init() {
39+
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
40+
}
41+
42+
@Autowired TestRestTemplate restTemplate;
43+
44+
@Test
45+
public void givenEndpoint_whenQueryStockPrice_thenPriceCorrect() {
46+
String response = getStockPrice("btc");
47+
assertEquals("10000", response);
48+
49+
response = getStockPrice("eth");
50+
assertEquals("N/A", response);
51+
}
52+
53+
@Test
54+
public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception {
55+
restTemplate.delete("http://localhost:8001/apis/stock-api");
56+
57+
APIObject stockAPI = new APIObject("stock-api", "stock.api", "http://localhost:8080", "/");
58+
HttpEntity<APIObject> apiEntity = new HttpEntity<>(stockAPI);
59+
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
60+
61+
assertEquals(HttpStatus.CREATED, addAPIResp.getStatusCode());
62+
63+
addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
64+
assertEquals(HttpStatus.CONFLICT, addAPIResp.getStatusCode());
65+
String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class);
66+
67+
assertTrue(apiListResp.contains("stock-api"));
68+
69+
HttpHeaders headers = new HttpHeaders();
70+
headers.set("Host", "stock.api");
71+
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
72+
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
73+
74+
assertEquals("10000", stockPriceResp.getBody());
75+
}
76+
77+
@Test
78+
public void givenKongAdminAPI_whenAddAPIConsumer_thenAdded() {
79+
restTemplate.delete("http://localhost:8001/consumers/eugenp");
80+
81+
ConsumerObject consumer = new ConsumerObject("eugenp");
82+
HttpEntity<ConsumerObject> addConsumerEntity = new HttpEntity<>(consumer);
83+
ResponseEntity<String> addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers/", addConsumerEntity, String.class);
84+
85+
assertEquals(HttpStatus.CREATED, addConsumerResp.getStatusCode());
86+
87+
addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers", addConsumerEntity, String.class);
88+
assertEquals(HttpStatus.CONFLICT, addConsumerResp.getStatusCode());
89+
90+
String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class);
91+
assertTrue(consumerListResp.contains("eugenp"));
92+
}
93+
94+
@Test
95+
public void givenAPI_whenEnableAuth_thenAnonymousDenied() throws Exception {
96+
String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class);
97+
if (!apiListResp.contains("stock-api")) {
98+
givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong();
99+
}
100+
101+
PluginObject authPlugin = new PluginObject("key-auth");
102+
ResponseEntity<String> enableAuthResp = restTemplate.postForEntity("http://localhost:8001/apis/stock-api/plugins", new HttpEntity<>(authPlugin), String.class);
103+
104+
assertTrue(HttpStatus.CREATED == enableAuthResp.getStatusCode() || HttpStatus.CONFLICT == enableAuthResp.getStatusCode());
105+
106+
String pluginsResp = restTemplate.getForObject("http://localhost:8001/apis/stock-api/plugins", String.class);
107+
assertTrue(pluginsResp.contains("key-auth"));
108+
109+
HttpHeaders headers = new HttpHeaders();
110+
headers.set("Host", "stock.api");
111+
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
112+
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
113+
assertEquals(HttpStatus.UNAUTHORIZED, stockPriceResp.getStatusCode());
114+
}
115+
116+
@Test
117+
public void givenAPIAuthEnabled_whenAddKey_thenAccessAllowed() throws Exception {
118+
String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class);
119+
if (!apiListResp.contains("stock-api")) {
120+
givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong();
121+
}
122+
123+
String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class);
124+
if (!consumerListResp.contains("eugenp")) {
125+
givenKongAdminAPI_whenAddAPIConsumer_thenAdded();
126+
}
127+
128+
final String consumerKey = "eugenp.pass";
129+
KeyAuthObject keyAuth = new KeyAuthObject(consumerKey);
130+
ResponseEntity<String> keyAuthResp = restTemplate.postForEntity("http://localhost:8001/consumers/eugenp/key-auth", new HttpEntity<>(keyAuth), String.class);
131+
132+
assertTrue(HttpStatus.CREATED == keyAuthResp.getStatusCode() || HttpStatus.CONFLICT == keyAuthResp.getStatusCode());
133+
134+
HttpHeaders headers = new HttpHeaders();
135+
headers.set("Host", "stock.api");
136+
headers.set("apikey", consumerKey);
137+
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
138+
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
139+
140+
assertEquals("10000", stockPriceResp.getBody());
141+
142+
headers.set("apikey", "wrongpass");
143+
requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
144+
stockPriceResp = restTemplate.exchange(requestEntity, String.class);
145+
assertEquals(HttpStatus.FORBIDDEN, stockPriceResp.getStatusCode());
146+
}
147+
148+
@Test
149+
public void givenAdminAPIProxy_whenAddAPIViaProxy_thenAPIAdded() throws Exception {
150+
APIObject adminAPI = new APIObject("admin-api", "admin.api", "http://localhost:8001", "/admin-api");
151+
HttpEntity<APIObject> apiEntity = new HttpEntity<>(adminAPI);
152+
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
153+
154+
assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode());
155+
156+
HttpHeaders headers = new HttpHeaders();
157+
headers.set("Host", "admin.api");
158+
APIObject baeldungAPI = new APIObject("baeldung-api", "baeldung.com", "http://ww.baeldung.com", "/");
159+
RequestEntity<APIObject> requestEntity = new RequestEntity<>(baeldungAPI, headers, HttpMethod.POST, new URI("http://localhost:8000/admin-api/apis"));
160+
addAPIResp = restTemplate.exchange(requestEntity, String.class);
161+
162+
assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode());
163+
}
164+
165+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.kong;
2+
3+
import com.baeldung.kong.domain.APIObject;
4+
import com.baeldung.kong.domain.TargetObject;
5+
import com.baeldung.kong.domain.UpstreamObject;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.boot.test.web.client.TestRestTemplate;
12+
import org.springframework.http.*;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
15+
import java.net.URI;
16+
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertTrue;
19+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
20+
21+
/**
22+
* @author aiet
23+
*/
24+
@RunWith(SpringRunner.class)
25+
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class)
26+
public class KongLoadBalanceLiveTest {
27+
28+
@Before
29+
public void init() {
30+
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
31+
}
32+
33+
@Autowired TestRestTemplate restTemplate;
34+
35+
@Test
36+
public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception {
37+
UpstreamObject upstream = new UpstreamObject("stock.api.service");
38+
ResponseEntity<String> addUpstreamResp = restTemplate.postForEntity("http://localhost:8001/upstreams", new HttpEntity<>(upstream), String.class);
39+
assertTrue(HttpStatus.CREATED == addUpstreamResp.getStatusCode() || HttpStatus.CONFLICT == addUpstreamResp.getStatusCode());
40+
41+
TargetObject testTarget = new TargetObject("localhost:8080", 10);
42+
ResponseEntity<String> addTargetResp = restTemplate.postForEntity("http://localhost:8001/upstreams/stock.api.service/targets", new HttpEntity<>(testTarget), String.class);
43+
assertTrue(HttpStatus.CREATED == addTargetResp.getStatusCode() || HttpStatus.CONFLICT == addTargetResp.getStatusCode());
44+
45+
TargetObject releaseTarget = new TargetObject("localhost:9090", 40);
46+
addTargetResp = restTemplate.postForEntity("http://localhost:8001/upstreams/stock.api.service/targets", new HttpEntity<>(releaseTarget), String.class);
47+
assertTrue(HttpStatus.CREATED == addTargetResp.getStatusCode() || HttpStatus.CONFLICT == addTargetResp.getStatusCode());
48+
49+
APIObject stockAPI = new APIObject("balanced-stock-api", "balanced.stock.api", "http://stock.api.service", "/");
50+
HttpEntity<APIObject> apiEntity = new HttpEntity<>(stockAPI);
51+
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
52+
assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode());
53+
54+
HttpHeaders headers = new HttpHeaders();
55+
headers.set("Host", "balanced.stock.api");
56+
for (int i = 0; i < 1000; i++) {
57+
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
58+
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
59+
assertEquals("10000", stockPriceResp.getBody());
60+
}
61+
62+
int releaseCount = restTemplate.getForObject("http://localhost:9090/stock/reqcount", Integer.class);
63+
int testCount = restTemplate.getForObject("http://localhost:8080/stock/reqcount", Integer.class);
64+
65+
assertTrue(Math.round(releaseCount * 1.0 / testCount) == 4);
66+
}
67+
68+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.kong.domain;
2+
3+
/**
4+
* @author aiet
5+
*/
6+
public class APIObject {
7+
8+
public APIObject() {
9+
}
10+
11+
public APIObject(String name, String hosts, String upstream_url, String uris) {
12+
this.name = name;
13+
this.hosts = hosts;
14+
this.upstream_url = upstream_url;
15+
this.uris = uris;
16+
}
17+
18+
private String name;
19+
private String hosts;
20+
private String upstream_url;
21+
private String uris;
22+
23+
public String getUris() {
24+
return uris;
25+
}
26+
27+
public void setUris(String uris) {
28+
this.uris = uris;
29+
}
30+
31+
public String getUpstream_url() {
32+
return upstream_url;
33+
}
34+
35+
public void setUpstream_url(String upstream_url) {
36+
this.upstream_url = upstream_url;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public String getHosts() {
48+
return hosts;
49+
}
50+
51+
public void setHosts(String hosts) {
52+
this.hosts = hosts;
53+
}
54+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.kong.domain;
2+
3+
/**
4+
* @author aiet
5+
*/
6+
public class ConsumerObject {
7+
8+
private String username;
9+
private String custom_id;
10+
11+
public ConsumerObject(String username) {
12+
this.username = username;
13+
}
14+
15+
public ConsumerObject(String username, String custom_id) {
16+
this.username = username;
17+
this.custom_id = custom_id;
18+
}
19+
20+
public String getUsername() {
21+
return username;
22+
}
23+
24+
public void setUsername(String username) {
25+
this.username = username;
26+
}
27+
28+
public String getCustom_id() {
29+
return custom_id;
30+
}
31+
32+
public void setCustom_id(String custom_id) {
33+
this.custom_id = custom_id;
34+
}
35+
}

0 commit comments

Comments
 (0)