-
Notifications
You must be signed in to change notification settings - Fork 1.3k
api,server: apis return their http request type #11382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c719b7f
api,server: apis return their http request type
shwstppr c7eceda
fix and unit test
shwstppr 8efc627
more test
shwstppr be1cc2d
address copilot
shwstppr 859cfdb
Merge remote-tracking branch 'apache/main' into api-request-type
shwstppr 385cfd9
Merge branch 'main' into api-request-type
DaanHoogland 44d4e91
Merge remote-tracking branch 'apache/main' into api-request-type
shwstppr f092c18
Update plugins/api/discovery/src/main/java/org/apache/cloudstack/api/…
harikrishna-patnala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,4 +50,6 @@ | |
| RoleType[] authorized() default {}; | ||
|
|
||
| Class<?>[] entityType() default {}; | ||
|
|
||
| String httpMethod() default ""; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
.../discovery/src/test/java/org/apache/cloudstack/discovery/ApiDiscoveryServiceImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package org.apache.cloudstack.discovery; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.cloudstack.api.APICommand; | ||
| import org.apache.cloudstack.api.BaseAsyncCmd; | ||
| import org.apache.cloudstack.api.BaseCmd; | ||
| import org.apache.cloudstack.api.Parameter; | ||
| import org.apache.cloudstack.api.command.admin.account.CreateAccountCmd; | ||
| import org.apache.cloudstack.api.command.admin.user.GetUserCmd; | ||
| import org.apache.cloudstack.api.command.user.discovery.ListApisCmd; | ||
| import org.apache.cloudstack.api.response.ApiDiscoveryResponse; | ||
| import org.apache.cloudstack.api.response.ApiParameterResponse; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.Spy; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
|
|
||
| import com.cloud.utils.ReflectUtil; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class ApiDiscoveryServiceImplTest { | ||
|
|
||
| @Mock | ||
| APICommand apiCommandMock; | ||
|
|
||
| @Spy | ||
| @InjectMocks | ||
| ApiDiscoveryServiceImpl discoveryServiceSpy; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| Mockito.when(apiCommandMock.name()).thenReturn("listApis"); | ||
| Mockito.when(apiCommandMock.since()).thenReturn(""); | ||
| } | ||
|
|
||
| @Test | ||
| public void getCmdRequestMapReturnsResponseWithCorrectApiNameAndDescription() { | ||
| Mockito.when(apiCommandMock.description()).thenReturn("Lists all APIs"); | ||
| ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(ListApisCmd.class, apiCommandMock); | ||
| Assert.assertEquals("listApis", response.getName()); | ||
| Assert.assertEquals("Lists all APIs", response.getDescription()); | ||
| } | ||
|
|
||
| @Test | ||
| public void getCmdRequestMapSetsHttpRequestTypeToGetWhenApiNameMatchesGetPattern() { | ||
| Mockito.when(apiCommandMock.name()).thenReturn("getUser"); | ||
| Mockito.when(apiCommandMock.httpMethod()).thenReturn(""); | ||
| ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(GetUserCmd.class, apiCommandMock); | ||
| Assert.assertEquals("GET", response.getHttpRequestType()); | ||
| } | ||
|
|
||
| @Test | ||
| public void getCmdRequestMapSetsHttpRequestTypeToPostWhenApiNameDoesNotMatchGetPattern() { | ||
| Mockito.when(apiCommandMock.name()).thenReturn("createAccount"); | ||
| Mockito.when(apiCommandMock.httpMethod()).thenReturn(""); | ||
| ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(CreateAccountCmd.class, apiCommandMock); | ||
| Assert.assertEquals("POST", response.getHttpRequestType()); | ||
| } | ||
|
|
||
| @Test | ||
| public void getCmdRequestMapSetsAsyncToTrueForAsyncCommand() { | ||
| Mockito.when(apiCommandMock.name()).thenReturn("asyncApi"); | ||
| ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(BaseAsyncCmd.class, apiCommandMock); | ||
| Assert.assertTrue(response.getAsync()); | ||
| } | ||
|
|
||
| @Test | ||
| public void getCmdRequestMapDoesNotAddParamsWithoutParameterAnnotation() { | ||
| ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(BaseCmd.class, apiCommandMock); | ||
| Assert.assertFalse(response.getParams().isEmpty()); | ||
| Assert.assertEquals(1, response.getParams().size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void getCmdRequestMapAddsParamsWithExposedAndIncludedInApiDocAnnotations() { | ||
| Field fieldMock = Mockito.mock(Field.class); | ||
| Parameter parameterMock = Mockito.mock(Parameter.class); | ||
| Mockito.when(parameterMock.expose()).thenReturn(true); | ||
| Mockito.when(parameterMock.includeInApiDoc()).thenReturn(true); | ||
| Mockito.when(parameterMock.name()).thenReturn("paramName"); | ||
| Mockito.when(parameterMock.since()).thenReturn(""); | ||
| Mockito.when(parameterMock.entityType()).thenReturn(new Class[]{Object.class}); | ||
| Mockito.when(parameterMock.description()).thenReturn("paramDescription"); | ||
| Mockito.when(parameterMock.type()).thenReturn(BaseCmd.CommandType.STRING); | ||
| Mockito.when(fieldMock.getAnnotation(Parameter.class)).thenReturn(parameterMock); | ||
| try (MockedStatic<ReflectUtil> reflectUtilMockedStatic = Mockito.mockStatic(ReflectUtil.class)) { | ||
| reflectUtilMockedStatic.when(() -> ReflectUtil.getAllFieldsForClass(any(Class.class), any(Class[].class))) | ||
| .thenReturn(Set.of(fieldMock)); | ||
| ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(ListApisCmd.class, apiCommandMock); | ||
| Set<ApiParameterResponse> params = response.getParams(); | ||
| Assert.assertEquals(1, params.size()); | ||
| ApiParameterResponse paramResponse = params.iterator().next(); | ||
| Assert.assertEquals("paramName", ReflectionTestUtils.getField(paramResponse, "name")); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.