1
1
/**
2
- *
3
- * Copyright 2025 Google LLC
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
- http://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
- */
2
+ * Copyright 2025 Google LLC
3
+ *
4
+ * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5
+ * except in compliance with the License. You may obtain a copy of the License at
6
+ *
7
+ * <p>http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * <p>Unless required by applicable law or agreed to in writing, software distributed under the
10
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11
+ * express or implied. See the License for the specific language governing permissions and
12
+ * limitations under the License.
13
+ */
18
14
package com .google .cloud .pso ;
19
15
20
16
import static org .junit .jupiter .api .Assertions .assertEquals ;
29
25
import com .google .api .client .http .HttpRequestFactory ;
30
26
import com .google .api .client .http .HttpResponse ;
31
27
import com .google .api .client .http .HttpTransport ;
32
- import com .google .api .client .json .JsonFactory ;
33
28
import com .google .cloud .discoveryengine .v1alpha .SearchRequest ;
34
29
import com .google .cloud .discoveryengine .v1alpha .SearchResponse ;
35
30
import com .google .cloud .discoveryengine .v1alpha .SearchServiceClient ;
@@ -72,7 +67,8 @@ void testSearchSuccess() throws Exception {
72
67
IamCredentialsClient mockIamCredentialsClient = mock (IamCredentialsClient .class );
73
68
SignJwtResponse mockSignJwtResponse = mock (SignJwtResponse .class );
74
69
when (mockSignJwtResponse .getSignedJwt ()).thenReturn ("mockedSignedJwt" );
75
- when (mockIamCredentialsClient .signJwt (any (SignJwtRequest .class ))).thenReturn (mockSignJwtResponse );
70
+ when (mockIamCredentialsClient .signJwt (any (SignJwtRequest .class )))
71
+ .thenReturn (mockSignJwtResponse );
76
72
77
73
// Mocking getAccessToken
78
74
GoogleTokenResponse mockGoogleTokenResponse = mock (GoogleTokenResponse .class );
@@ -82,7 +78,8 @@ void testSearchSuccess() throws Exception {
82
78
HttpRequest mockHttpRequest = mock (HttpRequest .class );
83
79
when (mockHttpRequest .execute ()).thenReturn (mockHttpResponse );
84
80
HttpRequestFactory mockHttpRequestFactory = mock (HttpRequestFactory .class );
85
- when (mockHttpRequestFactory .buildPostRequest (any (GenericUrl .class ), any ())).thenReturn (mockHttpRequest );
81
+ when (mockHttpRequestFactory .buildPostRequest (any (GenericUrl .class ), any ()))
82
+ .thenReturn (mockHttpRequest );
86
83
HttpTransport mockHttpTransport = mock (HttpTransport .class );
87
84
when (mockHttpTransport .createRequestFactory ()).thenReturn (mockHttpRequestFactory );
88
85
@@ -91,34 +88,58 @@ void testSearchSuccess() throws Exception {
91
88
SearchResponse mockSearchResponse = mock (SearchResponse .class );
92
89
SearchResponse .SearchResult mockSearchResult = mock (SearchResponse .SearchResult .class );
93
90
when (mockSearchResult .toString ()).thenReturn ("Mocked Search Result" );
94
- when (mockSearchResponse .getResultsList ()).thenReturn (java .util .Collections .singletonList (mockSearchResult ));
91
+ when (mockSearchResponse .getResultsList ())
92
+ .thenReturn (java .util .Collections .singletonList (mockSearchResult ));
95
93
when (mockSearchResponse .getResultsCount ()).thenReturn (1 );
96
- SearchServiceClient .SearchPagedResponse mockPagedResponse = mock (SearchServiceClient .SearchPagedResponse .class );
97
- when (mockPagedResponse .getPage ()).thenReturn (new SearchResponse .SearchResult .Page () {
98
- @ Override
99
- public SearchResponse getResponse () {
100
- return mockSearchResponse ;
101
- }
102
- });
94
+ SearchServiceClient .SearchPagedResponse mockPagedResponse =
95
+ mock (SearchServiceClient .SearchPagedResponse .class );
96
+ when (mockPagedResponse .getPage ())
97
+ .thenReturn (
98
+ new SearchResponse .SearchResult .Page () {
99
+ @ Override
100
+ public SearchResponse getResponse () {
101
+ return mockSearchResponse ;
102
+ }
103
+ });
103
104
when (mockSearchServiceClient .search (any (SearchRequest .class ))).thenReturn (mockPagedResponse );
104
105
105
- try (MockedStatic <IamCredentialsClient > mockedIamCredentialsClient = Mockito .mockStatic (IamCredentialsClient .class );
106
- MockedStatic <SearchServiceClient > mockedSearchServiceClient = Mockito .mockStatic (SearchServiceClient .class );
107
- MockedStatic <OauthApplication > mockedOauthApplication = Mockito .mockStatic (OauthApplication .class );
108
- MockedStatic <HttpTransport > mockedHttpTransport = Mockito .mockStatic (HttpTransport .class )) {
109
-
110
- mockedIamCredentialsClient .when (IamCredentialsClient ::create ).thenReturn (mockIamCredentialsClient );
111
- mockedSearchServiceClient .when (() -> SearchServiceClient .create (any (SearchServiceSettings .class ))).thenReturn (mockSearchServiceClient );
106
+ try (MockedStatic <IamCredentialsClient > mockedIamCredentialsClient =
107
+ Mockito .mockStatic (IamCredentialsClient .class );
108
+ MockedStatic <SearchServiceClient > mockedSearchServiceClient =
109
+ Mockito .mockStatic (SearchServiceClient .class );
110
+ MockedStatic <OauthApplication > mockedOauthApplication =
111
+ Mockito .mockStatic (OauthApplication .class );
112
+ MockedStatic <HttpTransport > mockedHttpTransport = Mockito .mockStatic (HttpTransport .class )) {
113
+
114
+ mockedIamCredentialsClient
115
+ .when (IamCredentialsClient ::create )
116
+ .thenReturn (mockIamCredentialsClient );
117
+ mockedSearchServiceClient
118
+ .when (() -> SearchServiceClient .create (any (SearchServiceSettings .class )))
119
+ .thenReturn (mockSearchServiceClient );
112
120
mockedHttpTransport .when (HttpTransport ::new ).thenReturn (mockHttpTransport );
113
121
when (mockHttpTransport .createRequestFactory ()).thenReturn (mockHttpRequestFactory );
114
- when (mockHttpRequestFactory .buildPostRequest (any (GenericUrl .class ), any ())).thenReturn (mockHttpRequest );
122
+ when (mockHttpRequestFactory .buildPostRequest (any (GenericUrl .class ), any ()))
123
+ .thenReturn (mockHttpRequest );
115
124
when (mockHttpRequest .execute ()).thenReturn (mockHttpResponse );
116
125
when (mockHttpResponse .parseAs (GoogleTokenResponse .class )).thenReturn (mockGoogleTokenResponse );
117
126
118
- mockedOauthApplication .when (() -> OauthApplication .generateSingedJwt (searchUserEmail , serviceAccountId )).thenReturn ("mockedSignedJwt" );
119
- mockedOauthApplication .when (() -> OauthApplication .getAccessToken ("mockedSignedJwt" )).thenReturn ("mockedAccessToken" );
120
-
121
- OauthApplication .search (projectId , location , collectionId , engineId , servingConfigId , searchQuery , searchUserEmail , serviceAccountId );
127
+ mockedOauthApplication
128
+ .when (() -> OauthApplication .generateSingedJwt (searchUserEmail , serviceAccountId ))
129
+ .thenReturn ("mockedSignedJwt" );
130
+ mockedOauthApplication
131
+ .when (() -> OauthApplication .getAccessToken ("mockedSignedJwt" ))
132
+ .thenReturn ("mockedAccessToken" );
133
+
134
+ OauthApplication .search (
135
+ projectId ,
136
+ location ,
137
+ collectionId ,
138
+ engineId ,
139
+ servingConfigId ,
140
+ searchQuery ,
141
+ searchUserEmail ,
142
+ serviceAccountId );
122
143
123
144
// Verify that the search method executed without throwing an exception. More specific
124
145
// verification would be ideal, but this at least confirms the happy path.
@@ -133,10 +154,14 @@ void testGenerateSingedJwtSuccess() throws IOException {
133
154
IamCredentialsClient mockIamCredentialsClient = mock (IamCredentialsClient .class );
134
155
SignJwtResponse mockSignJwtResponse = mock (SignJwtResponse .class );
135
156
when (mockSignJwtResponse .getSignedJwt ()).thenReturn ("mockedSignedJwt" );
136
- when (mockIamCredentialsClient .signJwt (any (SignJwtRequest .class ))).thenReturn (mockSignJwtResponse );
157
+ when (mockIamCredentialsClient .signJwt (any (SignJwtRequest .class )))
158
+ .thenReturn (mockSignJwtResponse );
137
159
138
- try (MockedStatic <IamCredentialsClient > mockedIamCredentialsClient = Mockito .mockStatic (IamCredentialsClient .class )) {
139
- mockedIamCredentialsClient .when (IamCredentialsClient ::create ).thenReturn (mockIamCredentialsClient );
160
+ try (MockedStatic <IamCredentialsClient > mockedIamCredentialsClient =
161
+ Mockito .mockStatic (IamCredentialsClient .class )) {
162
+ mockedIamCredentialsClient
163
+ .when (IamCredentialsClient ::create )
164
+ .thenReturn (mockIamCredentialsClient );
140
165
141
166
String signedJwt = OauthApplication .generateSingedJwt (searchUserEmail , serviceAccountId );
142
167
@@ -155,14 +180,17 @@ void testGetAccessTokenSuccess() throws IOException {
155
180
HttpRequest mockHttpRequest = mock (HttpRequest .class );
156
181
when (mockHttpRequest .execute ()).thenReturn (mockHttpResponse );
157
182
HttpRequestFactory mockHttpRequestFactory = mock (HttpRequestFactory .class );
158
- when (mockHttpRequestFactory .buildPostRequest (any (GenericUrl .class ), any ())).thenReturn (mockHttpRequest );
183
+ when (mockHttpRequestFactory .buildPostRequest (any (GenericUrl .class ), any ()))
184
+ .thenReturn (mockHttpRequest );
159
185
HttpTransport mockHttpTransport = mock (HttpTransport .class );
160
186
when (mockHttpTransport .createRequestFactory ()).thenReturn (mockHttpRequestFactory );
161
187
162
- try (MockedStatic <HttpTransport > mockedHttpTransport = Mockito .mockStatic (HttpTransport .class )) {
188
+ try (MockedStatic <HttpTransport > mockedHttpTransport =
189
+ Mockito .mockStatic (HttpTransport .class )) {
163
190
mockedHttpTransport .when (HttpTransport ::new ).thenReturn (mockHttpTransport );
164
191
when (mockHttpTransport .createRequestFactory ()).thenReturn (mockHttpRequestFactory );
165
- when (mockHttpRequestFactory .buildPostRequest (any (GenericUrl .class ), any ())).thenReturn (mockHttpRequest );
192
+ when (mockHttpRequestFactory .buildPostRequest (any (GenericUrl .class ), any ()))
193
+ .thenReturn (mockHttpRequest );
166
194
when (mockHttpRequest .execute ()).thenReturn (mockHttpResponse );
167
195
when (mockHttpResponse .parseAs (GoogleTokenResponse .class )).thenReturn (mockGoogleTokenResponse );
168
196
0 commit comments