Skip to content

Commit 7b602ba

Browse files
authored
Utility endpoints refactored and moved (#239)
* Utility endpoints refactored and moved * Switched datatypes and created few unit tests to api version * Refactored to use TestApiClient * UtilityEndpoint logic fixed. Now correctly tests version * Added extra insertion before version test
1 parent 917fba5 commit 7b602ba

7 files changed

Lines changed: 236 additions & 147 deletions

File tree

src/main/java/com/teragrep/cfe18/ApiSessionMapper.java renamed to src/main/java/com/teragrep/cfe18/ApiUtilityMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import org.apache.ibatis.annotations.Mapper;
4949

5050
@Mapper
51-
public interface ApiSessionMapper {
51+
public interface ApiUtilityMapper {
5252

53-
int getSession();
53+
Integer getSession();
5454
}

src/main/java/com/teragrep/cfe18/handlers/ApiSessionController.java renamed to src/main/java/com/teragrep/cfe18/handlers/ApiUtilityController.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,22 @@
4545
*/
4646
package com.teragrep.cfe18.handlers;
4747

48-
import com.teragrep.cfe18.ApiSessionMapper;
48+
import com.teragrep.cfe18.ApiUtilityMapper;
4949
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
5050
import org.mybatis.spring.SqlSessionTemplate;
5151
import org.springframework.beans.factory.annotation.Autowired;
52+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
53+
import org.springframework.security.oauth2.jwt.Jwt;
5254
import org.springframework.web.bind.annotation.RequestMapping;
5355
import org.springframework.web.bind.annotation.RequestMethod;
5456
import org.springframework.web.bind.annotation.RestController;
5557

5658
import javax.sql.DataSource;
5759

5860
@RestController
59-
@RequestMapping(path = "/version")
61+
@RequestMapping(path = "/v2/meta")
6062
@SecurityRequirement(name = "api")
61-
public class ApiSessionController {
63+
public class ApiUtilityController {
6264

6365
@Autowired
6466
DataSource dataSource;
@@ -67,14 +69,23 @@ public class ApiSessionController {
6769
SqlSessionTemplate sqlSessionTemplate;
6870

6971
@Autowired
70-
ApiSessionMapper apiSessionMapper;
72+
ApiUtilityMapper apiUtilityMapper;
7173

7274
@RequestMapping(
73-
path = "",
75+
path = "/data-version",
7476
method = RequestMethod.GET
7577
)
76-
public int version() {
77-
return apiSessionMapper.getSession();
78+
public Integer version() {
79+
return apiUtilityMapper.getSession();
80+
}
81+
82+
@RequestMapping(
83+
path = "/jwt",
84+
method = RequestMethod.GET,
85+
produces = "application/json"
86+
)
87+
public String index(@AuthenticationPrincipal Jwt jwt) throws Exception {
88+
return String.format("Hello, %s!", jwt.getSubject());
7889
}
7990

8091
}

src/main/java/com/teragrep/cfe18/handlers/CaptureFileController.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@
6262
import org.springframework.http.HttpStatus;
6363
import org.springframework.http.MediaType;
6464
import org.springframework.http.ResponseEntity;
65-
import org.springframework.security.core.annotation.AuthenticationPrincipal;
66-
import org.springframework.security.oauth2.jwt.Jwt;
6765
import org.springframework.web.bind.annotation.*;
6866

6967
import javax.sql.DataSource;
@@ -86,15 +84,6 @@ public class CaptureFileController {
8684
@Autowired
8785
CaptureFileMapper captureFileMapper;
8886

89-
@RequestMapping(
90-
path = "/jwt",
91-
method = RequestMethod.GET,
92-
produces = "application/json"
93-
)
94-
public String index(@AuthenticationPrincipal Jwt jwt) throws Exception {
95-
return String.format("Hello, %s!", jwt.getSubject());
96-
}
97-
9887
@RequestMapping(
9988
path = "",
10089
method = RequestMethod.PUT,

src/main/resources/com/teragrep/cfe18/ApiSessionMapper.xml renamed to src/main/resources/com/teragrep/cfe18/ApiUtilityMapper.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
<!DOCTYPE mapper
33
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
44
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
5-
<mapper namespace="com.teragrep.cfe18.ApiSessionMapper">
5+
<mapper namespace="com.teragrep.cfe18.ApiUtilityMapper">
66

77
<!-- GET ALL -->
88
<select id="getSession" resultMap="versionNumber" statementType="CALLABLE">
99
{CALL cfe_18.current_txn_id()}
1010
</select>
11-
<resultMap id="versionNumber" type="Integer">
11+
12+
<resultMap id="versionNumber" type="java.lang.Integer">
1213
<result property="version" column="tx_id"/>
1314
</resultMap>
1415

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* Integration main data management for Teragrep
3+
* Copyright (C) 2021 Suomen Kanuuna Oy
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
17+
*
18+
*
19+
* Additional permission under GNU Affero General Public License version 3
20+
* section 7
21+
*
22+
* If you modify this Program, or any covered work, by linking or combining it
23+
* with other code, such other code is not for that reason alone subject to any
24+
* of the requirements of the GNU Affero GPL version 3 as long as this Program
25+
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
26+
* modifications.
27+
*
28+
* Supplemented terms under GNU Affero General Public License version 3
29+
* section 7
30+
*
31+
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
32+
* versions must be marked as "Modified version of" The Program.
33+
*
34+
* Names of the licensors and authors may not be used for publicity purposes.
35+
*
36+
* No rights are granted for use of trade names, trademarks, or service marks
37+
* which are in The Program if any.
38+
*
39+
* Licensee must indemnify licensors and authors for any liability that these
40+
* contractual assumptions impose on licensors and authors.
41+
*
42+
* To the extent this program is licensed as part of the Commercial versions of
43+
* Teragrep, the applicable Commercial License may apply to this file if you as
44+
* a licensee so wish it.
45+
*/
46+
package com.teragrep.cfe18.controllerTests;
47+
48+
import com.google.gson.Gson;
49+
import com.teragrep.cfe18.handlers.entities.Flow;
50+
import org.apache.http.HttpEntity;
51+
import org.apache.http.HttpResponse;
52+
import org.apache.http.HttpStatus;
53+
import org.apache.http.client.methods.HttpGet;
54+
import org.apache.http.client.methods.HttpUriRequest;
55+
import org.apache.http.impl.client.HttpClientBuilder;
56+
import org.apache.http.util.EntityUtils;
57+
58+
import org.junit.jupiter.api.*;
59+
import org.junit.jupiter.api.extension.ExtendWith;
60+
import org.springframework.boot.test.context.SpringBootTest;
61+
62+
import org.springframework.boot.test.web.server.LocalServerPort;
63+
64+
import java.util.ArrayList;
65+
66+
import static org.junit.jupiter.api.Assertions.*;
67+
68+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
69+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
70+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
71+
@ExtendWith(MigrateDatabaseExtension.class)
72+
public class ApiUtilityControllerTest extends TestSpringBootInformation {
73+
74+
Gson gson = new Gson();
75+
76+
@LocalServerPort
77+
private int port;
78+
79+
// Testing if token works
80+
@Test
81+
@Order(1)
82+
public void testToken() {
83+
84+
// Given
85+
HttpUriRequest request = new HttpGet("http://localhost:" + port + "/v2/meta/jwt");
86+
request.setHeader("Authorization", "Bearer " + token);
87+
// When
88+
89+
HttpResponse httpResponse = Assertions
90+
.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(request));
91+
92+
HttpEntity entity = httpResponse.getEntity();
93+
94+
String responseString = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entity, "UTF-8"));
95+
96+
// Then
97+
assertEquals(HttpStatus.SC_OK, httpResponse.getStatusLine().getStatusCode());
98+
assertEquals("Hello, subject!", responseString);
99+
}
100+
101+
// Testing that without token there is no access
102+
@Test
103+
@Order(2)
104+
public void testInvalidToken() {
105+
106+
// Given
107+
HttpUriRequest request2 = new HttpGet("http://localhost:" + port + "/v2/meta/jwt");
108+
request2.setHeader("Authorization", "Bearer noToken");
109+
110+
// When
111+
112+
HttpResponse httpResponse2 = Assertions
113+
.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(request2));
114+
115+
// Then
116+
assertEquals(HttpStatus.SC_UNAUTHORIZED, httpResponse2.getStatusLine().getStatusCode());
117+
}
118+
119+
// Tests that giving no token returns 500
120+
@Test
121+
@Order(3)
122+
public void testNoToken() {
123+
124+
// Given
125+
HttpUriRequest request2 = new HttpGet("http://localhost:" + port + "/v2/meta/jwt");
126+
127+
// When
128+
129+
HttpResponse httpResponse2 = Assertions
130+
.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(request2));
131+
132+
// Then
133+
assertEquals(HttpStatus.SC_UNAUTHORIZED, httpResponse2.getStatusLine().getStatusCode());
134+
}
135+
136+
@Test
137+
@Order(4)
138+
public void testVersionIsNumber() {
139+
140+
TestApiClient testApiClient = new TestApiClient(port, token);
141+
142+
Integer flowId = testApiClient.insertFlow("Testflow");
143+
144+
HttpUriRequest request = new HttpGet("http://localhost:" + port + "/v2/meta/data-version");
145+
request.setHeader("Authorization", "Bearer " + token);
146+
147+
HttpResponse httpResponse = Assertions
148+
.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(request));
149+
150+
HttpEntity entity = httpResponse.getEntity();
151+
152+
String responseString = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entity, "UTF-8"));
153+
154+
Integer result = Integer.valueOf(responseString);
155+
156+
assertNotNull(result);
157+
}
158+
159+
@Test
160+
@Order(5)
161+
public void testVersionCanBeFetched() {
162+
TestApiClient testApiClient = new TestApiClient(port, token);
163+
164+
Integer flowId = testApiClient.insertFlow("Testflow");
165+
166+
HttpGet requestGet = new HttpGet("http://localhost:" + port + "/v2/meta/data-version");
167+
168+
requestGet.setHeader("Authorization", "Bearer " + token);
169+
170+
HttpResponse responseGet = Assertions
171+
.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(requestGet));
172+
173+
HttpEntity entityGet = responseGet.getEntity();
174+
175+
String responseStringGet = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entityGet, "UTF-8"));
176+
177+
int version = Integer.parseInt(responseStringGet);
178+
179+
HttpResponse response = testApiClient.deleteFlow(flowId);
180+
181+
HttpGet requestFlow = new HttpGet("http://localhost:" + port + "/flow?version=" + version);
182+
183+
requestFlow.setHeader("Authorization", "Bearer " + token);
184+
185+
HttpResponse responseFlow = Assertions
186+
.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(requestFlow));
187+
188+
HttpEntity entityFlow = responseFlow.getEntity();
189+
190+
String flowAsResponseString = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entityFlow, "UTF-8"));
191+
192+
ArrayList<Flow> expected = new ArrayList<>();
193+
Flow flow = new Flow();
194+
flow.setName("Testflow");
195+
flow.setId(1);
196+
expected.add(flow);
197+
String expectedJson = new Gson().toJson(expected);
198+
199+
assertEquals(expectedJson, flowAsResponseString);
200+
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
201+
assertEquals(HttpStatus.SC_OK, responseFlow.getStatusLine().getStatusCode());
202+
203+
}
204+
205+
}

src/test/java/com/teragrep/cfe18/controllerTests/TestApiClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.teragrep.cfe18.handlers.entities.*;
5050
import org.apache.http.HttpEntity;
5151
import org.apache.http.HttpResponse;
52+
import org.apache.http.client.methods.HttpDelete;
5253
import org.apache.http.client.methods.HttpPut;
5354
import org.apache.http.entity.ContentType;
5455
import org.apache.http.entity.StringEntity;
@@ -91,6 +92,13 @@ public Integer insertFlow(final String name) {
9192
return Assertions.assertDoesNotThrow(() -> flowAsJson.getInt("id"));
9293
}
9394

95+
public HttpResponse deleteFlow(final Integer flowId) {
96+
HttpDelete flowRequest = new HttpDelete("http://localhost:" + port + "/flow/" + flowId);
97+
flowRequest.setHeader("Authorization", "Bearer " + token);
98+
99+
return Assertions.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(flowRequest));
100+
}
101+
94102
public Integer insertSink(final Integer flowId, final String sinkPort, final String ip, final String protocol) {
95103
Sink sink = new Sink();
96104
sink.setFlowId(flowId);

0 commit comments

Comments
 (0)