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+ import static org .testng .Assert .*;
22+
23+ /**
24+ * Tests for error entity deserialization in jersey3 client
25+ *
26+ * These tests verify that the templates generate the correct code
27+ * for errorEntity feature (issue #4777)
28+ */
29+ public class JavaJersey3ErrorEntityTest {
30+
31+ private static final String JERSEY3_TEMPLATE_DIR =
32+ "src/main/resources/Java/libraries/jersey3/" ;
33+
34+ /**
35+ * Test that apiException.mustache contains errorEntity field
36+ */
37+ @ Test
38+ public void testApiExceptionHasErrorEntityField () throws Exception {
39+ String template = readTemplate ("apiException.mustache" );
40+ assertNotNull (template , "apiException.mustache should exist" );
41+ assertTrue (template .contains ("errorEntity" ),
42+ "apiException.mustache should contain 'errorEntity' field" );
43+ }
44+
45+ /**
46+ * Test that apiException.mustache contains getErrorEntity method
47+ */
48+ @ Test
49+ public void testApiExceptionHasGetErrorEntityMethod () throws Exception {
50+ String template = readTemplate ("apiException.mustache" );
51+ assertNotNull (template );
52+ assertTrue (template .contains ("getErrorEntity" ),
53+ "apiException.mustache should contain 'getErrorEntity()' method" );
54+ }
55+
56+ /**
57+ * Test that ApiClient.mustache contains deserializeErrorEntity method
58+ */
59+ @ Test
60+ public void testApiClientHasDeserializeErrorEntityMethod () throws Exception {
61+ String template = readTemplate ("ApiClient.mustache" );
62+ assertNotNull (template );
63+ assertTrue (template .contains ("deserializeErrorEntity" ),
64+ "ApiClient.mustache should contain 'deserializeErrorEntity' method" );
65+ }
66+
67+ /**
68+ * Test that api.mustache contains localVarErrorTypes
69+ */
70+ @ Test
71+ public void testApiGeneratesErrorTypesMap () throws Exception {
72+ String template = readTemplate ("api.mustache" );
73+ assertNotNull (template );
74+ assertTrue (template .contains ("localVarErrorTypes" ),
75+ "api.mustache should contain 'localVarErrorTypes'" );
76+ }
77+
78+ /**
79+ * Test that invokeAPI accepts errorTypes parameter
80+ */
81+ @ Test
82+ public void testInvokeAPIHasErrorTypesParameter () throws Exception {
83+ String template = readTemplate ("ApiClient.mustache" );
84+ assertNotNull (template );
85+ assertTrue (template .contains ("errorTypes" ),
86+ "ApiClient.mustache should contain 'errorTypes' parameter" );
87+ }
88+
89+ /**
90+ * Test that template handles "default" response (uses "0" as key)
91+ */
92+ @ Test
93+ public void testDefaultResponseHandling () throws Exception {
94+ String template = readTemplate ("ApiClient.mustache" );
95+ assertNotNull (template );
96+ assertTrue (template .contains ("\" 0\" " ),
97+ "ApiClient.mustache should handle 'default' response with '0' key" );
98+ }
99+
100+ /**
101+ * Test error types map building pattern
102+ */
103+ @ Test
104+ public void testErrorTypesMapBuildingPattern () throws Exception {
105+ String template = readTemplate ("api.mustache" );
106+ assertNotNull (template );
107+ // Check pattern: localVarErrorTypes.put("code", new GenericType<...>)
108+ assertTrue (template .contains ("localVarErrorTypes.put" ),
109+ "api.mustache should build error types map using put" );
110+ }
111+
112+ /**
113+ * Test backward compatibility - null is passed for errorTypes
114+ */
115+ @ Test
116+ public void testBackwardCompatibility () throws Exception {
117+ String template = readTemplate ("ApiClient.mustache" );
118+ assertNotNull (template );
119+ // Check that null is passed for backward compatibility
120+ assertTrue (template .contains (", null" ) ||
121+ template .contains ("null," ) ||
122+ template .contains ("null/*" ),
123+ "Backwards compatibility: null should be passed for errorTypes" );
124+ }
125+
126+ /**
127+ * Helper method to read template files
128+ */
129+ private String readTemplate (String templateName ) throws Exception {
130+ java .nio .file .Path templatePath = java .nio .file .Paths .get (
131+ JERSEY3_TEMPLATE_DIR + templateName );
132+ if (!java .nio .file .Files .exists (templatePath )) {
133+ // Try alternate path
134+ templatePath = java .nio .file .Paths .get (
135+ "modules/openapi-generator/" + JERSEY3_TEMPLATE_DIR + templateName );
136+ }
137+ if (java .nio .file .Files .exists (templatePath )) {
138+ return java .nio .file .Files .readString (templatePath );
139+ }
140+ // Try classpath
141+ try {
142+ java .io .InputStream is = getClass ().getClassLoader ()
143+ .getResourceAsStream (JERSEY3_TEMPLATE_DIR + templateName );
144+ if (is != null ) {
145+ return new String (is .readAllBytes ());
146+ }
147+ } catch (Exception ignored ) {}
148+
149+ return null ;
150+ }
151+ }
0 commit comments