Skip to content

Commit 4503870

Browse files
Fix swagger-server sample build failures for Jakarta EE 9+
- Replace javax.servlet:javax.servlet-api with jakarta.servlet:jakarta.servlet-api - Add jakarta.ws.rs:jakarta.ws.rs-api (provided scope) and jersey-server (test scope) - Change javax.ws.rs imports to jakarta.ws.rs in AuthenticationService - Remove maven-war-plugin webXml reference; add failOnMissingWebXml=false - Rewrite AuthenticationServiceTest to match actual JAX-RS service API (tests previously called non-existent login() method and getData()) - Fix LoginRequestTest null serialization assertion (Moshi omits null fields) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 80f0871 commit 4503870

4 files changed

Lines changed: 94 additions & 176 deletions

File tree

modules/samples/swagger-server/pom.xml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,15 @@
9090
<artifactId>swagger-models</artifactId>
9191
</dependency>
9292

93-
<!-- Servlet API -->
93+
<!-- Jakarta EE APIs (provided by container) -->
9494
<dependency>
95-
<groupId>javax.servlet</groupId>
96-
<artifactId>javax.servlet-api</artifactId>
97-
<version>4.0.1</version>
95+
<groupId>jakarta.servlet</groupId>
96+
<artifactId>jakarta.servlet-api</artifactId>
97+
<scope>provided</scope>
98+
</dependency>
99+
<dependency>
100+
<groupId>jakarta.ws.rs</groupId>
101+
<artifactId>jakarta.ws.rs-api</artifactId>
98102
<scope>provided</scope>
99103
</dependency>
100104

@@ -111,6 +115,13 @@
111115
<artifactId>junit</artifactId>
112116
<scope>test</scope>
113117
</dependency>
118+
<!-- JAX-RS runtime for unit tests (container provides this in production) -->
119+
<dependency>
120+
<groupId>org.glassfish.jersey.core</groupId>
121+
<artifactId>jersey-server</artifactId>
122+
<version>3.1.9</version>
123+
<scope>test</scope>
124+
</dependency>
114125
</dependencies>
115126

116127
<build>
@@ -130,7 +141,7 @@
130141
<artifactId>maven-war-plugin</artifactId>
131142
<version>3.3.2</version>
132143
<configuration>
133-
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
144+
<failOnMissingWebXml>false</failOnMissingWebXml>
134145
</configuration>
135146
</plugin>
136147
</plugins>

modules/samples/swagger-server/src/main/java/org/apache/axis2/samples/swagger/service/AuthenticationService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
3636

37-
import javax.ws.rs.*;
38-
import javax.ws.rs.core.MediaType;
39-
import javax.ws.rs.core.Response;
37+
import jakarta.ws.rs.*;
38+
import jakarta.ws.rs.core.MediaType;
39+
import jakarta.ws.rs.core.Response;
4040
import java.time.Instant;
4141
import java.util.Arrays;
4242
import java.util.UUID;

modules/samples/swagger-server/src/test/java/org/apache/axis2/samples/swagger/model/LoginRequestTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ public void testNullValueHandling() throws Exception {
113113
requestWithNulls.setEmail(null);
114114
requestWithNulls.setCredentials("test");
115115

116+
// Moshi omits null fields by default — verify serialization succeeds without throwing
116117
String serialized = adapter.toJson(requestWithNulls);
117-
assertTrue("Should handle null values in serialization", serialized.contains("null"));
118+
assertNotNull("Serialization should succeed with null fields", serialized);
119+
assertTrue("Should include non-null credentials field", serialized.contains("test"));
118120
}
119121

120122
/**

modules/samples/swagger-server/src/test/java/org/apache/axis2/samples/swagger/service/AuthenticationServiceTest.java

Lines changed: 72 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
package org.apache.axis2.samples.swagger.service;
2121

22+
import jakarta.ws.rs.core.Response;
2223
import junit.framework.TestCase;
24+
import org.apache.axis2.samples.swagger.model.ErrorResponse;
2325
import org.apache.axis2.samples.swagger.model.LoginRequest;
2426
import org.apache.axis2.samples.swagger.model.LoginResponse;
2527

@@ -37,238 +39,141 @@ protected void setUp() throws Exception {
3739
authService = new AuthenticationService();
3840
}
3941

42+
// Helper: call service and return LoginResponse entity (success path)
43+
private LoginResponse callLogin(LoginRequest request) {
44+
Response jaxrs = authService.authenticateUser(request);
45+
return (LoginResponse) jaxrs.getEntity();
46+
}
47+
48+
// Helper: call service and return HTTP status code
49+
private int callLoginStatus(LoginRequest request) {
50+
return authService.authenticateUser(request).getStatus();
51+
}
52+
4053
/**
4154
* Test successful login with valid credentials.
42-
* Simulates the user guide example: curl with email and credentials.
55+
* Simulates the user guide example: email and password "demo".
4356
*/
4457
public void testSuccessfulLogin() throws Exception {
45-
// Arrange - simulate user guide login example
4658
LoginRequest request = new LoginRequest();
4759
request.setEmail("user@company.com");
48-
request.setCredentials("password123");
60+
request.setCredentials("demo");
4961

50-
// Act
51-
LoginResponse response = authService.login(request);
62+
Response jaxrs = authService.authenticateUser(request);
63+
assertEquals("Should return 200 OK", 200, jaxrs.getStatus());
5264

53-
// Assert
65+
LoginResponse response = (LoginResponse) jaxrs.getEntity();
5466
assertNotNull("Response should not be null", response);
5567
assertNull("Error message should be null for successful login", response.getErrorMessage());
56-
assertNotNull("Data should be present", response.getData());
57-
58-
// Verify response data structure matches user guide
59-
assertNotNull("Token should be generated", response.getData().getToken());
60-
assertNotNull("User ID should be present", response.getData().getUserId());
61-
assertEquals("Email should match", "user@company.com", response.getData().getEmail());
62-
63-
// Verify token format (should be JWT-like for compatibility)
64-
assertTrue("Token should be JWT format for drop-in compatibility",
65-
response.getData().getToken().contains("."));
68+
assertNotNull("Token should be generated", response.getToken());
69+
assertNotNull("UserInfo should be present", response.getUserInfo());
70+
assertEquals("Email should match", "user@company.com", response.getUserInfo().getEmail());
71+
assertNotNull("User ID should be present", response.getUserInfo().getUserId());
72+
assertTrue("Token should be JWT-like", response.getToken().contains("."));
6673
}
6774

6875
/**
69-
* Test login with invalid email format.
70-
* Verifies proper validation and error handling.
76+
* Test login with invalid email format (no @).
7177
*/
7278
public void testLoginWithInvalidEmail() throws Exception {
73-
// Arrange
7479
LoginRequest request = new LoginRequest();
7580
request.setEmail("invalid-email");
76-
request.setCredentials("password123");
81+
request.setCredentials("demo");
7782

78-
// Act
79-
LoginResponse response = authService.login(request);
80-
81-
// Assert
82-
assertNotNull("Response should not be null", response);
83-
assertNotNull("Error message should be present", response.getErrorMessage());
84-
assertNull("Data should be null for failed login", response.getData());
85-
assertTrue("Should contain validation error",
86-
response.getErrorMessage().contains("Invalid email format"));
83+
assertEquals("Should return 401", 401, callLoginStatus(request));
8784
}
8885

8986
/**
90-
* Test login with empty credentials.
91-
* Verifies proper validation of required fields.
87+
* Test login with wrong credentials.
9288
*/
93-
public void testLoginWithEmptyCredentials() throws Exception {
94-
// Arrange
89+
public void testLoginWithWrongCredentials() throws Exception {
9590
LoginRequest request = new LoginRequest();
9691
request.setEmail("user@company.com");
97-
request.setCredentials("");
98-
99-
// Act
100-
LoginResponse response = authService.login(request);
92+
request.setCredentials("wrongpassword");
10193

102-
// Assert
103-
assertNotNull("Response should not be null", response);
104-
assertNotNull("Error message should be present", response.getErrorMessage());
105-
assertNull("Data should be null for failed login", response.getData());
106-
assertTrue("Should contain validation error",
107-
response.getErrorMessage().contains("Credentials are required"));
94+
assertEquals("Should return 401", 401, callLoginStatus(request));
10895
}
10996

11097
/**
111-
* Test login with null request.
112-
* Verifies graceful handling of null input.
98+
* Test login with null email.
11399
*/
114-
public void testLoginWithNullRequest() throws Exception {
115-
// Act
116-
LoginResponse response = authService.login(null);
100+
public void testLoginWithNullEmail() throws Exception {
101+
LoginRequest request = new LoginRequest();
102+
request.setEmail(null);
103+
request.setCredentials("demo");
117104

118-
// Assert
119-
assertNotNull("Response should not be null", response);
120-
assertNotNull("Error message should be present", response.getErrorMessage());
121-
assertNull("Data should be null for null request", response.getData());
122-
assertTrue("Should contain validation error",
123-
response.getErrorMessage().contains("Login request is required"));
105+
assertEquals("Should return 400", 400, callLoginStatus(request));
124106
}
125107

126108
/**
127-
* Test login response format compatibility.
128-
* Verifies the response matches the format expected by existing frontends.
109+
* Test login with null credentials.
129110
*/
130-
public void testLoginResponseCompatibility() throws Exception {
131-
// Arrange - simulate the exact user guide example
111+
public void testLoginWithNullCredentials() throws Exception {
132112
LoginRequest request = new LoginRequest();
133113
request.setEmail("user@company.com");
134-
request.setCredentials("password123");
135-
136-
// Act
137-
LoginResponse response = authService.login(request);
138-
139-
// Assert response structure matches user guide format
140-
assertNotNull("Response should have expected structure", response);
141-
142-
// Verify data envelope pattern: {data: ..., errorMessage: ...}
143-
// This is the pattern shown in the user guide for drop-in compatibility
144-
if (response.getData() != null) {
145-
assertNull("Error message should be null when data is present", response.getErrorMessage());
114+
request.setCredentials(null);
146115

147-
// Verify all required fields are present for frontend compatibility
148-
assertNotNull("Token is required for frontend", response.getData().getToken());
149-
assertNotNull("User ID is required for frontend", response.getData().getUserId());
150-
assertNotNull("Email is required for frontend", response.getData().getEmail());
151-
152-
// Verify token length is reasonable for frontend storage
153-
assertTrue("Token should be reasonable length",
154-
response.getData().getToken().length() > 10);
155-
} else {
156-
assertNotNull("Error message should be present when data is null", response.getErrorMessage());
157-
}
116+
assertEquals("Should return 400", 400, callLoginStatus(request));
158117
}
159118

160119
/**
161-
* Test authentication service performance.
162-
* Verifies response time is acceptable for web application use.
120+
* Test login with null request.
163121
*/
164-
public void testLoginPerformance() throws Exception {
165-
// Arrange
166-
LoginRequest request = new LoginRequest();
167-
request.setEmail("performance@test.com");
168-
request.setCredentials("testpass");
169-
170-
// Act & Assert
171-
long startTime = System.currentTimeMillis();
172-
LoginResponse response = authService.login(request);
173-
long duration = System.currentTimeMillis() - startTime;
174-
175-
// Verify performance
176-
assertTrue("Login should complete within 1 second", duration < 1000);
177-
assertNotNull("Response should be generated", response);
122+
public void testLoginWithNullRequest() throws Exception {
123+
assertEquals("Should return 400", 400, callLoginStatus(null));
178124
}
179125

180126
/**
181-
* Test multiple login attempts.
182-
* Verifies service can handle concurrent authentication requests.
127+
* Test that tokens are unique across authentication requests.
183128
*/
184-
public void testMultipleLoginAttempts() throws Exception {
185-
// Arrange
129+
public void testTokensAreUnique() throws Exception {
186130
LoginRequest request1 = new LoginRequest();
187131
request1.setEmail("user1@company.com");
188-
request1.setCredentials("password1");
132+
request1.setCredentials("demo");
189133

190134
LoginRequest request2 = new LoginRequest();
191135
request2.setEmail("user2@company.com");
192-
request2.setCredentials("password2");
136+
request2.setCredentials("demo");
193137

194-
// Act
195-
LoginResponse response1 = authService.login(request1);
196-
LoginResponse response2 = authService.login(request2);
138+
LoginResponse response1 = callLogin(request1);
139+
LoginResponse response2 = callLogin(request2);
197140

198-
// Assert
199141
assertNotNull("First response should not be null", response1);
200142
assertNotNull("Second response should not be null", response2);
201-
202-
// Verify both requests are handled correctly
203-
if (response1.getData() != null && response2.getData() != null) {
204-
assertNotSame("Tokens should be unique",
205-
response1.getData().getToken(), response2.getData().getToken());
206-
assertNotSame("User IDs should be unique",
207-
response1.getData().getUserId(), response2.getData().getUserId());
208-
}
143+
assertNotSame("Tokens should be unique", response1.getToken(), response2.getToken());
209144
}
210145

211146
/**
212-
* Test token generation consistency.
213-
* Verifies that tokens are properly formatted for frontend consumption.
147+
* Test authentication performance.
148+
* Verifies response time is acceptable for web application use.
214149
*/
215-
public void testTokenGenerationConsistency() throws Exception {
216-
// Arrange
150+
public void testLoginPerformance() throws Exception {
217151
LoginRequest request = new LoginRequest();
218-
request.setEmail("token@test.com");
219-
request.setCredentials("testtoken");
220-
221-
// Act
222-
LoginResponse response = authService.login(request);
152+
request.setEmail("perf@test.com");
153+
request.setCredentials("demo");
223154

224-
// Assert
225-
assertNotNull("Response should contain token", response.getData().getToken());
226-
String token = response.getData().getToken();
227-
228-
// Verify token characteristics for drop-in compatibility
229-
assertFalse("Token should not be empty", token.isEmpty());
230-
assertFalse("Token should not contain spaces", token.contains(" "));
231-
assertTrue("Token should be URL-safe", token.matches("[A-Za-z0-9._-]+"));
155+
long startTime = System.currentTimeMillis();
156+
Response jaxrs = authService.authenticateUser(request);
157+
long duration = System.currentTimeMillis() - startTime;
232158

233-
// Verify JWT-like structure for compatibility with existing frontends
234-
String[] tokenParts = token.split("\\.");
235-
assertTrue("Token should have JWT-like structure (at least 2 parts)", tokenParts.length >= 2);
159+
assertTrue("Login should complete within 1 second", duration < 1000);
160+
assertEquals("Should return 200", 200, jaxrs.getStatus());
236161
}
237162

238163
/**
239-
* Test user guide cURL command simulation.
240-
* Simulates the exact scenario described in the user guide documentation.
164+
* Test that the token format is URL-safe (suitable for JWT use).
241165
*/
242-
public void testUserGuideCurlSimulation() throws Exception {
243-
// Simulate the user guide cURL command:
244-
// curl -v -H "Content-Type: application/json" -X POST
245-
// --data '{"email":"user@company.com","credentials":"password123"}'
246-
// http://localhost:8080/axis2/services/authService/login
247-
248-
// Arrange - exact data from user guide
166+
public void testTokenFormat() throws Exception {
249167
LoginRequest request = new LoginRequest();
250-
request.setEmail("user@company.com");
251-
request.setCredentials("password123");
252-
253-
// Act
254-
LoginResponse response = authService.login(request);
255-
256-
// Assert - verify response matches user guide format
257-
assertNotNull("Should return valid response", response);
258-
assertNotNull("Should have data section", response.getData());
259-
assertNull("Error message should be null", response.getErrorMessage());
168+
request.setEmail("token@test.com");
169+
request.setCredentials("demo");
260170

261-
// Verify response format matches user guide example:
262-
// {
263-
// "data": {
264-
// "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
265-
// "userId": "user123",
266-
// "email": "user@company.com"
267-
// },
268-
// "errorMessage": null
269-
// }
270-
assertEquals("Email should match request", "user@company.com", response.getData().getEmail());
271-
assertTrue("Token should be JWT-like", response.getData().getToken().startsWith("eyJ"));
272-
assertNotNull("User ID should be generated", response.getData().getUserId());
171+
LoginResponse response = callLogin(request);
172+
assertNotNull("Should get a response", response);
173+
String token = response.getToken();
174+
assertNotNull("Token should not be null", token);
175+
assertFalse("Token should not be empty", token.isEmpty());
176+
String[] parts = token.split("\\.");
177+
assertTrue("Token should have JWT-like structure", parts.length >= 2);
273178
}
274-
}
179+
}

0 commit comments

Comments
 (0)