1616// under the License.
1717package org .apache .cloudstack .discovery ;
1818
19- import static org .junit .Assert .assertEquals ;
20- import static org .junit .Assert .assertFalse ;
21- import static org .junit .Assert .assertTrue ;
22- import static org .mockito .Matchers .any ;
23- import static org .mockito .Matchers .anyString ;
24- import static org .mockito .Mockito .mock ;
25- import static org .mockito .Mockito .when ;
19+ import com .cloud .user .User ;
20+ import com .cloud .user .UserVO ;
21+ import com .cloud .utils .component .PluggableService ;
22+ import org .apache .cloudstack .acl .APIChecker ;
23+ import org .apache .cloudstack .api .APICommand ;
24+ import org .apache .cloudstack .api .command .user .discovery .ListApisCmd ;
25+ import org .apache .cloudstack .api .command .user .vm .ListVMsCmd ;
26+ import org .apache .cloudstack .api .response .ApiDiscoveryResponse ;
27+ import org .apache .cloudstack .api .response .ApiResponseResponse ;
28+ import org .apache .cloudstack .api .response .ListResponse ;
29+ import org .apache .commons .lang .StringUtils ;
30+ import org .junit .BeforeClass ;
31+ import org .junit .Test ;
2632
33+ import javax .naming .ConfigurationException ;
2734import java .util .ArrayList ;
2835import java .util .Arrays ;
2936import java .util .HashSet ;
3037import java .util .List ;
3138import java .util .Set ;
39+ import java .util .stream .Collectors ;
3240
33- import javax .naming .ConfigurationException ;
34-
35- import org .junit .BeforeClass ;
36- import org .junit .Test ;
37-
38- import org .apache .cloudstack .acl .APIChecker ;
39- import org .apache .cloudstack .api .APICommand ;
40- import org .apache .cloudstack .api .command .user .discovery .ListApisCmd ;
41- import org .apache .cloudstack .api .response .ApiDiscoveryResponse ;
42- import org .apache .cloudstack .api .response .ListResponse ;
43-
44- import com .cloud .user .User ;
45- import com .cloud .user .UserVO ;
46- import com .cloud .utils .component .PluggableService ;
41+ import static org .junit .Assert .assertEquals ;
42+ import static org .junit .Assert .assertFalse ;
43+ import static org .junit .Assert .assertNotNull ;
44+ import static org .junit .Assert .assertTrue ;
45+ import static org .mockito .Matchers .any ;
46+ import static org .mockito .Matchers .anyString ;
47+ import static org .mockito .Mockito .mock ;
48+ import static org .mockito .Mockito .when ;
4749
4850public class ApiDiscoveryTest {
4951 private static APIChecker s_apiChecker = mock (APIChecker .class );
5052 private static PluggableService s_pluggableService = mock (PluggableService .class );
5153 private static ApiDiscoveryServiceImpl s_discoveryService = new ApiDiscoveryServiceImpl ();
5254
5355 private static Class <?> testCmdClass = ListApisCmd .class ;
56+ private static Class <?> listVMsCmdClass = ListVMsCmd .class ;
5457 private static User testUser ;
5558 private static String testApiName ;
59+ private static String listVmsCmdName ;
5660 private static String testApiDescription ;
5761 private static String testApiSince ;
5862 private static boolean testApiAsync ;
5963
6064 @ BeforeClass
6165 public static void setUp () throws ConfigurationException {
66+
67+ listVmsCmdName = listVMsCmdClass .getAnnotation (APICommand .class ).name ();
68+
6269 testApiName = testCmdClass .getAnnotation (APICommand .class ).name ();
6370 testApiDescription = testCmdClass .getAnnotation (APICommand .class ).description ();
6471 testApiSince = testCmdClass .getAnnotation (APICommand .class ).since ();
@@ -75,13 +82,15 @@ public static void setUp() throws ConfigurationException {
7582
7683 Set <Class <?>> cmdClasses = new HashSet <Class <?>>();
7784 cmdClasses .add (ListApisCmd .class );
85+ cmdClasses .add (ListVMsCmd .class );
7886 s_discoveryService .start ();
7987 s_discoveryService .cacheResponseMap (cmdClasses );
8088 }
8189
8290 @ Test
8391 public void verifyListSingleApi () throws Exception {
8492 ListResponse <ApiDiscoveryResponse > responses = (ListResponse <ApiDiscoveryResponse >)s_discoveryService .listApis (testUser , testApiName );
93+ assertNotNull ("Responses should not be null" , responses );
8594 if (responses != null ) {
8695 ApiDiscoveryResponse response = responses .getResponses ().get (0 );
8796 assertTrue ("No. of response items should be one" , responses .getCount () == 1 );
@@ -95,12 +104,25 @@ public void verifyListSingleApi() throws Exception {
95104 @ Test
96105 public void verifyListApis () throws Exception {
97106 ListResponse <ApiDiscoveryResponse > responses = (ListResponse <ApiDiscoveryResponse >)s_discoveryService .listApis (testUser , null );
107+ assertNotNull ("Responses should not be null" , responses );
98108 if (responses != null ) {
99- assertTrue ("No. of response items > 1 " , responses .getCount ().intValue () == 1 );
109+ assertTrue ("No. of response items > 2 " , responses .getCount ().intValue () == 2 );
100110 for (ApiDiscoveryResponse response : responses .getResponses ()) {
101111 assertFalse ("API name is empty" , response .getName ().isEmpty ());
102112 assertFalse ("API description is empty" , response .getDescription ().isEmpty ());
103113 }
104114 }
105115 }
116+
117+ @ Test
118+ public void verifyListVirtualMachinesTagsField () throws Exception {
119+ ListResponse <ApiDiscoveryResponse > responses = (ListResponse <ApiDiscoveryResponse >)s_discoveryService .listApis (testUser , listVmsCmdName );
120+ assertNotNull ("Response should not be null" , responses );
121+ if (responses != null ) {
122+ assertEquals ("No. of response items should be one" , 1 , (int ) responses .getCount ());
123+ ApiDiscoveryResponse response = responses .getResponses ().get (0 );
124+ List <ApiResponseResponse > tagsResponse = response .getApiResponse ().stream ().filter (resp -> StringUtils .equals (resp .getName (), "tags" )).collect (Collectors .toList ());
125+ assertEquals ("Tags field should be present in listVirtualMachines response fields" , tagsResponse .size (), 1 );
126+ }
127+ }
106128}
0 commit comments