Skip to content

Commit 8efc627

Browse files
committed
more test
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent c7eceda commit 8efc627

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

plugins/api/discovery/src/main/java/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private ApiResponseResponse getFieldResponseMap(Field responseField) {
190190
return responseResponse;
191191
}
192192

193-
private ApiDiscoveryResponse getCmdRequestMap(Class<?> cmdClass, APICommand apiCmdAnnotation) {
193+
protected ApiDiscoveryResponse getCmdRequestMap(Class<?> cmdClass, APICommand apiCmdAnnotation) {
194194
String apiName = apiCmdAnnotation.name();
195195
ApiDiscoveryResponse response = new ApiDiscoveryResponse();
196196
response.setName(apiName);
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. 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,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.discovery;
18+
19+
import static org.mockito.ArgumentMatchers.any;
20+
21+
import java.lang.reflect.Field;
22+
import java.util.Set;
23+
24+
import org.apache.cloudstack.api.APICommand;
25+
import org.apache.cloudstack.api.BaseAsyncCmd;
26+
import org.apache.cloudstack.api.BaseCmd;
27+
import org.apache.cloudstack.api.Parameter;
28+
import org.apache.cloudstack.api.command.admin.account.CreateAccountCmd;
29+
import org.apache.cloudstack.api.command.admin.user.GetUserCmd;
30+
import org.apache.cloudstack.api.command.user.discovery.ListApisCmd;
31+
import org.apache.cloudstack.api.response.ApiDiscoveryResponse;
32+
import org.apache.cloudstack.api.response.ApiParameterResponse;
33+
import org.junit.Assert;
34+
import org.junit.Before;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.mockito.InjectMocks;
38+
import org.mockito.Mock;
39+
import org.mockito.MockedStatic;
40+
import org.mockito.Mockito;
41+
import org.mockito.Spy;
42+
import org.mockito.junit.MockitoJUnitRunner;
43+
import org.springframework.test.util.ReflectionTestUtils;
44+
45+
import com.cloud.utils.ReflectUtil;
46+
47+
@RunWith(MockitoJUnitRunner.class)
48+
public class ApiDiscoveryServiceImplTest {
49+
50+
@Mock
51+
APICommand apiCommandMock;
52+
53+
@Spy
54+
@InjectMocks
55+
ApiDiscoveryServiceImpl discoveryServiceSpy;
56+
57+
@Before
58+
public void setUp() {
59+
Mockito.when(apiCommandMock.name()).thenReturn("listApis");
60+
Mockito.when(apiCommandMock.since()).thenReturn("");
61+
}
62+
63+
@Test
64+
public void getCmdRequestMapReturnsResponseWithCorrectApiNameAndDescription() {
65+
Mockito.when(apiCommandMock.description()).thenReturn("Lists all APIs");
66+
ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(ListApisCmd.class, apiCommandMock);
67+
Assert.assertEquals("listApis", response.getName());
68+
Assert.assertEquals("Lists all APIs", response.getDescription());
69+
}
70+
71+
@Test
72+
public void getCmdRequestMapSetsHttpRequestTypeToGetWhenApiNameMatchesGetPattern() {
73+
Mockito.when(apiCommandMock.name()).thenReturn("getUser");
74+
Mockito.when(apiCommandMock.httpMethod()).thenReturn("");
75+
ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(GetUserCmd.class, apiCommandMock);
76+
Assert.assertEquals("GET", response.getHttpRequestType());
77+
}
78+
79+
@Test
80+
public void getCmdRequestMapSetsHttpRequestTypeToPostWhenApiNameDoesNotMatchGetPattern() {
81+
Mockito.when(apiCommandMock.name()).thenReturn("createAccount");
82+
Mockito.when(apiCommandMock.httpMethod()).thenReturn("");
83+
ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(CreateAccountCmd.class, apiCommandMock);
84+
Assert.assertEquals("POST", response.getHttpRequestType());
85+
}
86+
87+
@Test
88+
public void getCmdRequestMapSetsAsyncToTrueForAsyncCommand() {
89+
Mockito.when(apiCommandMock.name()).thenReturn("asyncApi");
90+
ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(BaseAsyncCmd.class, apiCommandMock);
91+
Assert.assertTrue(response.getAsync());
92+
}
93+
94+
@Test
95+
public void getCmdRequestMapDoesNotAddParamsWithoutParameterAnnotation() {
96+
ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(BaseCmd.class, apiCommandMock);
97+
Assert.assertFalse(response.getParams().isEmpty());
98+
Assert.assertEquals(1, response.getParams().size());
99+
}
100+
101+
@Test
102+
public void getCmdRequestMapAddsParamsWithExposedAndIncludedInApiDocAnnotations() {
103+
Field fieldMock = Mockito.mock(Field.class);
104+
Parameter parameterMock = Mockito.mock(Parameter.class);
105+
Mockito.when(parameterMock.expose()).thenReturn(true);
106+
Mockito.when(parameterMock.includeInApiDoc()).thenReturn(true);
107+
Mockito.when(parameterMock.name()).thenReturn("paramName");
108+
Mockito.when(parameterMock.since()).thenReturn("");
109+
Mockito.when(parameterMock.entityType()).thenReturn(new Class[]{Object.class});
110+
Mockito.when(parameterMock.description()).thenReturn("paramDescription");
111+
Mockito.when(parameterMock.type()).thenReturn(BaseCmd.CommandType.STRING);
112+
Mockito.when(fieldMock.getAnnotation(Parameter.class)).thenReturn(parameterMock);
113+
try (MockedStatic<ReflectUtil> reflectUtilMockedStatic = Mockito.mockStatic(ReflectUtil.class)) {
114+
reflectUtilMockedStatic.when(() -> ReflectUtil.getAllFieldsForClass(any(Class.class), any(Class[].class)))
115+
.thenReturn(Set.of(fieldMock));
116+
ApiDiscoveryResponse response = discoveryServiceSpy.getCmdRequestMap(ListApisCmd.class, apiCommandMock);
117+
Set<ApiParameterResponse> params = response.getParams();
118+
Assert.assertEquals(1, params.size());
119+
ApiParameterResponse paramResponse = params.iterator().next();
120+
Assert.assertEquals("paramName", ReflectionTestUtils.getField(paramResponse, "name"));
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)