Skip to content

Commit 041126e

Browse files
committed
fix: Fix assertion for problem+json
1 parent 90004ad commit 041126e

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

hivemq-edge/src/test/java/com/hivemq/api/auth/BearerTokenAuthTests.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.hivemq.api.auth.handler.impl.BearerTokenAuthenticationHandler;
2525
import com.hivemq.api.auth.jwt.JwtAuthenticationProvider;
2626
import com.hivemq.api.config.ApiJwtConfiguration;
27+
import com.hivemq.api.error.ApiExceptionMapper;
2728
import com.hivemq.api.resources.impl.AuthenticationResourceImpl;
2829
import com.hivemq.bootstrap.ioc.Injector;
2930
import com.hivemq.edge.api.model.ApiBearerToken;
@@ -35,6 +36,7 @@
3536
import com.hivemq.http.core.HttpUrlConnectionClient;
3637
import com.hivemq.http.core.HttpUtils;
3738
import com.hivemq.http.error.ProblemDetails;
39+
import jakarta.ws.rs.core.MediaType;
3840
import org.glassfish.jersey.server.ResourceConfig;
3941
import org.junit.AfterClass;
4042
import org.junit.Assert;
@@ -44,7 +46,6 @@
4446
import org.slf4j.Logger;
4547
import org.slf4j.LoggerFactory;
4648

47-
import jakarta.ws.rs.core.MediaType;
4849
import java.io.ByteArrayInputStream;
4950
import java.io.IOException;
5051
import java.util.HashSet;
@@ -60,17 +61,14 @@
6061
*/
6162
public class BearerTokenAuthTests {
6263

63-
protected final Logger logger = LoggerFactory.getLogger(BearerTokenAuthTests.class);
64-
6564
static final int TEST_HTTP_PORT = 8088;
6665
static final int CONNECT_TIMEOUT = 1000;
6766
static final int READ_TIMEOUT = 1000;
6867
static final String HTTP = "http";
69-
7068
protected static JaxrsHttpServer server;
71-
7269
@Mock
7370
private static Injector injector;
71+
protected final Logger logger = LoggerFactory.getLogger(BearerTokenAuthTests.class);
7472

7573
@BeforeClass
7674
public static void setUp() throws Exception {
@@ -86,7 +84,8 @@ public static void setUp() throws Exception {
8684
final ResourceConfig conf = new ResourceConfig() {
8785
{
8886
register(new ApiAuthenticationFeature(authenticationHandlers));
89-
}};
87+
}
88+
};
9089
conf.register(TestApiResource.class);
9190
conf.register(TestPermitAllApiResource.class);
9291
conf.register(TestResourceLevelRolesApiResource.class);
@@ -132,17 +131,16 @@ public void testAuthenticateValidUser() throws IOException {
132131
@Test
133132
public void testAuthenticateInvalidUser() throws IOException {
134133
final ObjectMapper mapper = new ObjectMapper();
135-
final UsernamePasswordCredentials creds =
134+
final UsernamePasswordCredentials credentials =
136135
new UsernamePasswordCredentials().userName("testuser").password("invalidpassword");
137136
final HttpResponse response = HttpUrlConnectionClient.post(HttpUrlConnectionClient.JSON_HEADERS,
138137
getTestServerAddress(HTTP, TEST_HTTP_PORT, "api/v1/auth/authenticate"),
139-
new ByteArrayInputStream(mapper.writeValueAsBytes(creds)),
138+
new ByteArrayInputStream(mapper.writeValueAsBytes(credentials)),
140139
CONNECT_TIMEOUT,
141140
READ_TIMEOUT);
142-
143141
assertThat(response.getStatusCode()).as("Resource should NOT be accepted").isEqualTo(401);
144142
assertThat(response.getContentType()).as("API authenticate response should be json")
145-
.startsWith(MediaType.APPLICATION_JSON);
143+
.isEqualTo(ApiExceptionMapper.APPLICATION_PROBLEM_JSON_CHARSET_UTF_8);
146144
assertThat(mapper.readValue(response.getResponseBody(), ProblemDetails.class)
147145
.getErrors()
148146
.get(0)
@@ -154,7 +152,8 @@ public void testAuthenticateInvalidUser() throws IOException {
154152
public void testAuthenticatedTokenAllowsApiAccess() throws IOException {
155153

156154
final ObjectMapper mapper = new ObjectMapper();
157-
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials().userName("testuser").password("test");
155+
final UsernamePasswordCredentials creds =
156+
new UsernamePasswordCredentials().userName("testuser").password("test");
158157
HttpResponse response = HttpUrlConnectionClient.post(HttpUrlConnectionClient.JSON_HEADERS,
159158
getTestServerAddress(HTTP, TEST_HTTP_PORT, "api/v1/auth/authenticate"),
160159
new ByteArrayInputStream(mapper.writeValueAsBytes(creds)),

hivemq-edge/src/test/java/com/hivemq/api/auth/ChainedAuthTests.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.hivemq.api.auth.jwt.JwtAuthenticationProvider;
2727
import com.hivemq.api.auth.provider.IUsernamePasswordProvider;
2828
import com.hivemq.api.config.ApiJwtConfiguration;
29+
import com.hivemq.api.error.ApiExceptionMapper;
2930
import com.hivemq.api.resources.impl.AuthenticationResourceImpl;
3031
import com.hivemq.edge.api.model.ApiBearerToken;
3132
import com.hivemq.edge.api.model.UsernamePasswordCredentials;
@@ -36,6 +37,7 @@
3637
import com.hivemq.http.core.HttpUrlConnectionClient;
3738
import com.hivemq.http.core.HttpUtils;
3839
import com.hivemq.http.error.ProblemDetails;
40+
import jakarta.ws.rs.core.MediaType;
3941
import org.glassfish.jersey.server.ResourceConfig;
4042
import org.jetbrains.annotations.NotNull;
4143
import org.junit.AfterClass;
@@ -45,7 +47,6 @@
4547
import org.slf4j.Logger;
4648
import org.slf4j.LoggerFactory;
4749

48-
import jakarta.ws.rs.core.MediaType;
4950
import java.io.ByteArrayInputStream;
5051
import java.io.IOException;
5152
import java.util.HashSet;
@@ -61,14 +62,12 @@
6162
*/
6263
public class ChainedAuthTests {
6364

64-
protected final Logger logger = LoggerFactory.getLogger(ChainedAuthTests.class);
65-
6665
static final int TEST_HTTP_PORT = 8088;
6766
static final int CONNECT_TIMEOUT = 1000;
6867
static final int READ_TIMEOUT = 1000;
6968
static final String HTTP = "http";
70-
7169
protected static @NotNull JaxrsHttpServer server;
70+
protected final Logger logger = LoggerFactory.getLogger(ChainedAuthTests.class);
7271

7372
@BeforeClass
7473
public static void setUp() throws Exception {
@@ -115,7 +114,8 @@ protected static String getTestServerAddress(final @NotNull String protocol, fin
115114
public void testAuthenticateValidUser() throws IOException {
116115

117116
final ObjectMapper mapper = new ObjectMapper();
118-
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials().userName("testuser").password("test");
117+
final UsernamePasswordCredentials creds =
118+
new UsernamePasswordCredentials().userName("testuser").password("test");
119119
final HttpResponse response = HttpUrlConnectionClient.post(HttpUrlConnectionClient.JSON_HEADERS,
120120
getTestServerAddress(HTTP, TEST_HTTP_PORT, "api/v1/auth/authenticate"),
121121
new ByteArrayInputStream(mapper.writeValueAsBytes(creds)),
@@ -131,19 +131,17 @@ public void testAuthenticateValidUser() throws IOException {
131131

132132
@Test
133133
public void testAuthenticateInvalidUser() throws IOException {
134-
135134
final ObjectMapper mapper = new ObjectMapper();
136-
final UsernamePasswordCredentials creds =
135+
final UsernamePasswordCredentials credentials =
137136
new UsernamePasswordCredentials().userName("testuser").password("invalidpassword");
138137
final HttpResponse response = HttpUrlConnectionClient.post(HttpUrlConnectionClient.JSON_HEADERS,
139138
getTestServerAddress(HTTP, TEST_HTTP_PORT, "api/v1/auth/authenticate"),
140-
new ByteArrayInputStream(mapper.writeValueAsBytes(creds)),
139+
new ByteArrayInputStream(mapper.writeValueAsBytes(credentials)),
141140
CONNECT_TIMEOUT,
142141
READ_TIMEOUT);
143-
144142
assertThat(response.getStatusCode()).as("Resource should NOT be accepted").isEqualTo(401);
145143
assertThat(response.getContentType()).as("API authenticate response should be json")
146-
.startsWith(MediaType.APPLICATION_JSON);
144+
.isEqualTo(ApiExceptionMapper.APPLICATION_PROBLEM_JSON_CHARSET_UTF_8);
147145
assertThat(mapper.readValue(response.getResponseBody(), ProblemDetails.class)
148146
.getErrors()
149147
.get(0)
@@ -155,7 +153,8 @@ public void testAuthenticateInvalidUser() throws IOException {
155153
public void testAuthenticatedTokenAllowsApiAccess() throws IOException {
156154

157155
final ObjectMapper mapper = new ObjectMapper();
158-
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials().userName("testuser").password("test");
156+
final UsernamePasswordCredentials creds =
157+
new UsernamePasswordCredentials().userName("testuser").password("test");
159158
HttpResponse response = HttpUrlConnectionClient.post(HttpUrlConnectionClient.JSON_HEADERS,
160159
getTestServerAddress(HTTP, TEST_HTTP_PORT, "api/v1/auth/authenticate"),
161160
new ByteArrayInputStream(mapper.writeValueAsBytes(creds)),

0 commit comments

Comments
 (0)