1+ /*
2+ * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+ * Copyright 2018 SmartBear Software
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * https://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+
18+ package org .openapitools .codegen .java .jersey3 ;
19+
20+ import org .testng .annotations .Test ;
21+
22+ import java .util .HashMap ;
23+ import java .util .Map ;
24+
25+ import static org .testng .Assert .*;
26+
27+ /**
28+ * Functional test for errorEntity deserialization feature.
29+ *
30+ * This test verifies that the generated code includes the errorEntity
31+ * field and getErrorEntity() method by examining the generated templates.
32+ *
33+ * Full integration tests would require:
34+ * 1. A running mock HTTP server
35+ * 2. Generated client code compiled and executed
36+ * 3. Actual API calls to verify runtime behavior
37+ *
38+ * The client's original project (BudgetApiTest) provides this type of
39+ * functional testing. This test verifies the template structure is correct.
40+ */
41+ public class JavaJersey3ErrorEntityFunctionalTest {
42+
43+ private static final String JERSEY3_TEMPLATE_DIR =
44+ "modules/openapi-generator/src/main/resources/Java/libraries/jersey3/" ;
45+
46+ /**
47+ * Verify generated code includes errorEntity field in ApiException
48+ */
49+ @ Test
50+ public void testGeneratedApiExceptionHasErrorEntity () throws Exception {
51+ String template = readTemplate ("apiException.mustache" );
52+ assertNotNull (template );
53+
54+ // Verify errorEntity field exists
55+ assertTrue (template .contains ("private Object errorEntity = null" ),
56+ "Generated ApiException should have errorEntity field" );
57+
58+ // Verify getErrorEntity() method exists
59+ assertTrue (template .contains ("public Object getErrorEntity()" ),
60+ "Generated ApiException should have getErrorEntity() method" );
61+
62+ // Verify constructor with errorEntity parameter
63+ assertTrue (template .contains ("Object errorEntity" ),
64+ "Generated ApiException should accept errorEntity in constructor" );
65+ }
66+
67+ /**
68+ * Verify generated code includes deserializeErrorEntity method
69+ */
70+ @ Test
71+ public void testGeneratedApiClientHasDeserializeErrorEntity () throws Exception {
72+ String template = readTemplate ("ApiClient.mustache" );
73+ assertNotNull (template );
74+
75+ // Verify deserializeErrorEntity method exists
76+ assertTrue (template .contains ("deserializeErrorEntity" ),
77+ "Generated ApiClient should have deserializeErrorEntity method" );
78+
79+ // Verify errorTypes parameter handling
80+ assertTrue (template .contains ("Map<String, GenericType> errorTypes" ),
81+ "Generated ApiClient should handle errorTypes parameter" );
82+ }
83+
84+ /**
85+ * Verify generated API methods build error types map
86+ */
87+ @ Test
88+ public void testGeneratedApiMethodsBuildErrorTypesMap () throws Exception {
89+ String template = readTemplate ("api.mustache" );
90+ assertNotNull (template );
91+
92+ // Verify localVarErrorTypes is built
93+ assertTrue (template .contains ("localVarErrorTypes" ),
94+ "Generated API methods should build localVarErrorTypes" );
95+
96+ // Verify error types are put into the map
97+ assertTrue (template .contains ("localVarErrorTypes.put" ),
98+ "Generated API methods should put error types into map" );
99+ }
100+
101+ /**
102+ * Verify null is returned when deserialization fails
103+ */
104+ @ Test
105+ public void testDeserializationReturnsNullOnFailure () throws Exception {
106+ String template = readTemplate ("ApiClient.mustache" );
107+ assertNotNull (template );
108+
109+ // Verify that on exception, null is returned (not error message)
110+ // This is the fix we applied for the P2 issue
111+ assertFalse (template .contains ("String.format(\" Failed deserializing" ),
112+ "deserializeErrorEntity should return null, not error message string" );
113+ }
114+
115+ /**
116+ * Helper method to read template files
117+ */
118+ private String readTemplate (String templateName ) throws Exception {
119+ java .nio .file .Path templatePath = java .nio .file .Paths .get (JERSEY3_TEMPLATE_DIR + templateName );
120+ if (java .nio .file .Files .exists (templatePath )) {
121+ return java .nio .file .Files .readString (templatePath );
122+ }
123+ return null ;
124+ }
125+ }
0 commit comments