1
+ /*
2
+ *
3
+ * *
4
+ * * * Copyright 2019-2021 the original author or authors.
5
+ * * *
6
+ * * * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * * * you may not use this file except in compliance with the License.
8
+ * * * You may obtain a copy of the License at
9
+ * * *
10
+ * * * https://www.apache.org/licenses/LICENSE-2.0
11
+ * * *
12
+ * * * Unless required by applicable law or agreed to in writing, software
13
+ * * * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * * * See the License for the specific language governing permissions and
16
+ * * * limitations under the License.
17
+ * *
18
+ *
19
+ */
20
+
21
+ package org .springdoc .api ;
22
+
23
+ import io .swagger .v3 .oas .annotations .enums .ParameterIn ;
24
+ import io .swagger .v3 .oas .models .OpenAPI ;
25
+ import io .swagger .v3 .oas .models .Operation ;
26
+ import io .swagger .v3 .oas .models .PathItem ;
27
+ import io .swagger .v3 .oas .models .Paths ;
28
+ import io .swagger .v3 .oas .models .media .NumberSchema ;
29
+ import io .swagger .v3 .oas .models .media .StringSchema ;
30
+ import io .swagger .v3 .oas .models .parameters .Parameter ;
31
+ import org .junit .jupiter .api .BeforeEach ;
32
+ import org .junit .jupiter .api .Test ;
33
+ import org .junit .jupiter .api .extension .ExtendWith ;
34
+ import org .mockito .Mock ;
35
+ import org .mockito .junit .jupiter .MockitoExtension ;
36
+ import org .springdoc .core .AbstractRequestService ;
37
+ import org .springdoc .core .GenericResponseService ;
38
+ import org .springdoc .core .OpenAPIService ;
39
+ import org .springdoc .core .OperationService ;
40
+ import org .springdoc .core .SpringDocConfigProperties ;
41
+ import org .springdoc .core .fn .RouterOperation ;
42
+ import org .springframework .beans .factory .ObjectFactory ;
43
+ import org .springframework .context .ApplicationContext ;
44
+ import org .springframework .web .bind .annotation .RequestMethod ;
45
+
46
+ import java .util .List ;
47
+ import java .util .Map ;
48
+ import java .util .Optional ;
49
+
50
+ import static java .util .Arrays .asList ;
51
+ import static org .hamcrest .MatcherAssert .assertThat ;
52
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
53
+ import static org .hamcrest .Matchers .is ;
54
+ import static org .hamcrest .Matchers .nullValue ;
55
+ import static org .mockito .Mockito .when ;
56
+ import static org .springframework .web .bind .annotation .RequestMethod .GET ;
57
+
58
+ @ ExtendWith (MockitoExtension .class )
59
+ class AbstractOpenApiResourceTest {
60
+
61
+ private static final String GROUP_NAME = "groupName" ;
62
+ private static final String PATH = "/some/path" ;
63
+ public static final String PARAMETER_REFERENCE = "#/components/parameters/MyParameter" ;
64
+ public static final String PARAMETER_WITH_NUMBER_SCHEMA_NAME = "parameterWithNumberSchema" ;
65
+ public static final String PARAMETER_WITHOUT_SCHEMA_NAME = "parameterWithoutSchema" ;
66
+
67
+ @ Mock
68
+ private ObjectFactory <OpenAPIService > openAPIBuilderObjectFactory ;
69
+
70
+ @ Mock
71
+ private OpenAPIService openAPIService ;
72
+
73
+ @ Mock
74
+ private AbstractRequestService requestBuilder ;
75
+
76
+ @ Mock
77
+ private GenericResponseService responseBuilder ;
78
+
79
+ @ Mock
80
+ private OperationService operationParser ;
81
+
82
+ @ Mock
83
+ private ApplicationContext context ;
84
+
85
+ private OpenAPI openAPI ;
86
+
87
+ private AbstractOpenApiResource resource ;
88
+
89
+ @ BeforeEach
90
+ public void setUp () {
91
+ openAPI = new OpenAPI ();
92
+ openAPI .setPaths (new Paths ().addPathItem (PATH , new PathItem ()));
93
+
94
+ when (openAPIService .getCalculatedOpenAPI ()).thenReturn (openAPI );
95
+ when (openAPIService .getContext ()).thenReturn (context );
96
+
97
+ when (openAPIBuilderObjectFactory .getObject ()).thenReturn (openAPIService );
98
+
99
+ resource = new AbstractOpenApiResource (
100
+ GROUP_NAME ,
101
+ openAPIBuilderObjectFactory ,
102
+ requestBuilder ,
103
+ responseBuilder ,
104
+ operationParser ,
105
+ Optional .empty (),
106
+ Optional .empty (),
107
+ new SpringDocConfigProperties (),
108
+ Optional .empty ()
109
+ ) {
110
+
111
+ @ Override
112
+ protected void getPaths (final Map <String , Object > findRestControllers ) { }
113
+ };
114
+ }
115
+
116
+ @ Test
117
+ void calculatePathFromRouterOperation () {
118
+ final Parameter refParameter = new Parameter ().$ref (PARAMETER_REFERENCE );
119
+
120
+ final Parameter numberParameterInPath = new Parameter ()
121
+ .name (PARAMETER_WITH_NUMBER_SCHEMA_NAME )
122
+ .in (ParameterIn .PATH .toString ())
123
+ .schema (new NumberSchema ());
124
+
125
+ final Parameter parameterWithoutSchema = new Parameter ()
126
+ .name (PARAMETER_WITHOUT_SCHEMA_NAME );
127
+
128
+ final Operation operation = new Operation ();
129
+ operation .setParameters (asList (
130
+ refParameter ,
131
+ numberParameterInPath ,
132
+ parameterWithoutSchema
133
+ ));
134
+
135
+ final RouterOperation routerOperation = new RouterOperation ();
136
+ routerOperation .setMethods (new RequestMethod []{ GET });
137
+ routerOperation .setOperationModel (operation );
138
+ routerOperation .setPath (PATH );
139
+
140
+ resource .calculatePath (routerOperation );
141
+
142
+ final List <Parameter > parameters = resource .getOpenApi ().getPaths ().get (PATH ).getGet ().getParameters ();
143
+ assertThat (parameters .size (), is (3 ));
144
+ assertThat (parameters , containsInAnyOrder (refParameter , numberParameterInPath , parameterWithoutSchema ));
145
+
146
+ assertThat (refParameter .getName (), nullValue ());
147
+ assertThat (refParameter .get$ref (), is (PARAMETER_REFERENCE ));
148
+ assertThat (refParameter .getSchema (), nullValue ());
149
+ assertThat (refParameter .getIn (), nullValue ());
150
+
151
+ assertThat (numberParameterInPath .getName (), is (PARAMETER_WITH_NUMBER_SCHEMA_NAME ));
152
+ assertThat (numberParameterInPath .get$ref (), nullValue ());
153
+ assertThat (numberParameterInPath .getSchema (), is (new NumberSchema ()));
154
+ assertThat (numberParameterInPath .getIn (), is (ParameterIn .PATH .toString ()));
155
+
156
+ assertThat (parameterWithoutSchema .getName (), is (PARAMETER_WITHOUT_SCHEMA_NAME ));
157
+ assertThat (parameterWithoutSchema .get$ref (), nullValue ());
158
+ assertThat (parameterWithoutSchema .getSchema (), is (new StringSchema ()));
159
+ assertThat (parameterWithoutSchema .getIn (), is (ParameterIn .QUERY .toString ()));
160
+ }
161
+ }
0 commit comments