Skip to content

Commit 1ae738b

Browse files
Merge pull request #25 from NetApp/feature/CSTACKEX-77
Feature/cstackex 77: Added the junit for initialize method
2 parents 2f02d8a + ef0354a commit 1ae738b

File tree

2 files changed

+348
-2
lines changed

2 files changed

+348
-2
lines changed

plugins/storage/volume/ontap/pom.xml

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
under the License.
1818
-->
1919
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2121
<modelVersion>4.0.0</modelVersion>
2222
<artifactId>cloud-plugin-storage-volume-ontap</artifactId>
2323
<name>Apache CloudStack Plugin - Storage Volume ONTAP Provider</name>
@@ -36,6 +36,7 @@
3636
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
3737
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
3838
<jackson-databind.version>2.13.4</jackson-databind.version>
39+
<assertj.version>3.24.2</assertj.version>
3940
</properties>
4041
<dependencyManagement>
4142
<dependencies>
@@ -94,7 +95,49 @@
9495
<artifactId>swagger-annotations</artifactId>
9596
<version>${swagger-annotations.version}</version>
9697
</dependency>
98+
<!-- JUnit 5 -->
99+
<dependency>
100+
<groupId>org.junit.jupiter</groupId>
101+
<artifactId>junit-jupiter-engine</artifactId>
102+
<version>5.8.1</version>
103+
<scope>test</scope>
104+
</dependency>
105+
106+
<!-- Mockito -->
107+
<dependency>
108+
<groupId>org.mockito</groupId>
109+
<artifactId>mockito-core</artifactId>
110+
<version>3.12.4</version>
111+
<scope>test</scope>
112+
</dependency>
113+
<dependency>
114+
<groupId>org.mockito</groupId>
115+
<artifactId>mockito-junit-jupiter</artifactId>
116+
<version>5.2.0</version>
117+
<scope>test</scope>
118+
</dependency>
119+
120+
<!-- Mockito Inline (for static method mocking) -->
121+
<dependency>
122+
<groupId>org.mockito</groupId>
123+
<artifactId>mockito-inline</artifactId>
124+
<version>3.12.4</version>
125+
<scope>test</scope>
126+
</dependency>
127+
<dependency>
128+
<groupId>org.assertj</groupId>
129+
<artifactId>assertj-core</artifactId>
130+
<version>${assertj.version}</version>
131+
<scope>test</scope>
132+
</dependency>
97133
</dependencies>
134+
<repositories>
135+
<repository>
136+
<id>central</id>
137+
<name>Maven Central</name>
138+
<url>https://repo.maven.apache.org/maven2</url>
139+
</repository>
140+
</repositories>
98141
<build>
99142
<plugins>
100143
<plugin>
@@ -110,7 +153,10 @@
110153
<artifactId>maven-surefire-plugin</artifactId>
111154
<version>${maven-surefire-plugin.version}</version>
112155
<configuration>
113-
<skipTests>true</skipTests>
156+
<skipTests>false</skipTests>
157+
<includes>
158+
<include>**/*Test.java</include>
159+
</includes>
114160
</configuration>
115161
<executions>
116162
<execution>
Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.cloudstack.storage.lifecycle;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.mockito.InjectMocks;
25+
import org.mockito.Mock;
26+
import org.mockito.MockedStatic;
27+
import org.mockito.Mockito;
28+
import org.mockito.junit.jupiter.MockitoExtension;
29+
import org.mockito.junit.jupiter.MockitoSettings;
30+
import org.mockito.quality.Strictness;
31+
import org.apache.cloudstack.storage.feign.model.Volume;
32+
import com.cloud.dc.dao.ClusterDao;
33+
import com.cloud.utils.exception.CloudRuntimeException;
34+
import com.cloud.dc.ClusterVO;
35+
import java.util.Map;
36+
import static org.mockito.ArgumentMatchers.any;
37+
import static org.mockito.Mockito.when;
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
40+
import java.util.HashMap;
41+
import org.apache.cloudstack.storage.provider.StorageProviderFactory;
42+
import org.apache.cloudstack.storage.service.StorageStrategy;
43+
import org.apache.cloudstack.storage.volume.datastore.PrimaryDataStoreHelper;
44+
45+
46+
@ExtendWith(MockitoExtension.class)
47+
@MockitoSettings(strictness = Strictness.LENIENT)
48+
public class OntapPrimaryDatastoreLifecycleTest {
49+
@InjectMocks
50+
private OntapPrimaryDatastoreLifecycle ontapPrimaryDatastoreLifecycle;
51+
52+
@Mock
53+
private ClusterDao _clusterDao;
54+
55+
@Mock
56+
private StorageStrategy storageStrategy;
57+
58+
@Mock
59+
private PrimaryDataStoreHelper _dataStoreHelper;
60+
61+
@BeforeEach
62+
void setUp() {
63+
64+
ClusterVO clusterVO = new ClusterVO(1L, 1L, "clusterName");
65+
clusterVO.setHypervisorType("KVM");
66+
when(_clusterDao.findById(1L)).thenReturn(clusterVO);
67+
68+
when(storageStrategy.connect()).thenReturn(true);
69+
when(storageStrategy.getNetworkInterface()).thenReturn("testNetworkInterface");
70+
71+
Volume volume = new Volume();
72+
volume.setUuid("test-volume-uuid");
73+
volume.setName("testVolume");
74+
when(storageStrategy.createStorageVolume(any(), any())).thenReturn(volume);
75+
76+
}
77+
78+
@Test
79+
public void testInitialize_positive() {
80+
81+
Map<String, Object> dsInfos = new HashMap<>();
82+
dsInfos.put("username", "testUser");
83+
dsInfos.put("password", "testPassword");
84+
dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false");
85+
dsInfos.put("zoneId",1L);
86+
dsInfos.put("podId",1L);
87+
dsInfos.put("clusterId", 1L);
88+
dsInfos.put("name", "testStoragePool");
89+
dsInfos.put("providerName", "testProvider");
90+
dsInfos.put("capacityBytes",200000L);
91+
dsInfos.put("managed",true);
92+
dsInfos.put("tags", "testTag");
93+
dsInfos.put("isTagARule", false);
94+
dsInfos.put("details", new HashMap<String, String>());
95+
96+
try(MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) {
97+
storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy);
98+
ontapPrimaryDatastoreLifecycle.initialize(dsInfos);
99+
}
100+
}
101+
102+
@Test
103+
public void testInitialize_null_Arg() {
104+
Exception ex = assertThrows(CloudRuntimeException.class,() ->
105+
ontapPrimaryDatastoreLifecycle.initialize(null));
106+
assertTrue(ex.getMessage().contains("Datastore info map is null, cannot create primary storage"));
107+
}
108+
109+
@Test
110+
public void testInitialize_missingRequiredDetailKey() {
111+
Map<String, Object> dsInfos = new HashMap<>();
112+
dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1");
113+
dsInfos.put("zoneId",1L);
114+
dsInfos.put("podId",1L);
115+
dsInfos.put("clusterId", 1L);
116+
dsInfos.put("name", "testStoragePool");
117+
dsInfos.put("providerName", "testProvider");
118+
dsInfos.put("capacityBytes",200000L);
119+
dsInfos.put("managed",true);
120+
dsInfos.put("tags", "testTag");
121+
dsInfos.put("isTagARule", false);
122+
dsInfos.put("details", new HashMap<String, String>());
123+
124+
try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) {
125+
storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy);
126+
Exception ex = assertThrows(CloudRuntimeException.class, () -> ontapPrimaryDatastoreLifecycle.initialize(dsInfos));
127+
assertTrue(ex.getMessage().contains("missing detail"));
128+
}
129+
}
130+
131+
@Test
132+
public void testInitialize_invalidCapacityBytes() {
133+
Map<String, Object> dsInfos = new HashMap<>();
134+
dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false");
135+
dsInfos.put("zoneId",1L);
136+
dsInfos.put("podId",1L);
137+
dsInfos.put("clusterId", 1L);
138+
dsInfos.put("name", "testStoragePool");
139+
dsInfos.put("providerName", "testProvider");
140+
dsInfos.put("capacityBytes",-1L);
141+
dsInfos.put("managed",true);
142+
dsInfos.put("tags", "testTag");
143+
dsInfos.put("isTagARule", false);
144+
dsInfos.put("details", new HashMap<String, String>());
145+
146+
try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) {
147+
storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy);
148+
ontapPrimaryDatastoreLifecycle.initialize(dsInfos);
149+
}
150+
}
151+
152+
@Test
153+
public void testInitialize_unmanagedStorage() {
154+
Map<String, Object> dsInfos = new HashMap<>();
155+
dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false");
156+
dsInfos.put("zoneId",1L);
157+
dsInfos.put("podId",1L);
158+
dsInfos.put("clusterId", 1L);
159+
dsInfos.put("name", "testStoragePool");
160+
dsInfos.put("providerName", "testProvider");
161+
dsInfos.put("capacityBytes",200000L);
162+
dsInfos.put("managed",false);
163+
dsInfos.put("tags", "testTag");
164+
dsInfos.put("isTagARule", false);
165+
dsInfos.put("details", new HashMap<String, String>());
166+
167+
Exception ex = assertThrows(CloudRuntimeException.class, () -> {
168+
try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) {
169+
storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy);
170+
ontapPrimaryDatastoreLifecycle.initialize(dsInfos);
171+
}
172+
});
173+
assertTrue(ex.getMessage().contains("must be managed"));
174+
}
175+
176+
@Test
177+
public void testInitialize_nullStoragePoolName() {
178+
Map<String, Object> dsInfos = new HashMap<>();
179+
dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false");
180+
dsInfos.put("zoneId",1L);
181+
dsInfos.put("podId",1L);
182+
dsInfos.put("clusterId", 1L);
183+
dsInfos.put("name", null);
184+
dsInfos.put("providerName", "testProvider");
185+
dsInfos.put("capacityBytes",200000L);
186+
dsInfos.put("managed",true);
187+
dsInfos.put("tags", "testTag");
188+
dsInfos.put("isTagARule", false);
189+
dsInfos.put("details", new HashMap<String, String>());
190+
191+
Exception ex = assertThrows(CloudRuntimeException.class, () -> {
192+
try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) {
193+
storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy);
194+
ontapPrimaryDatastoreLifecycle.initialize(dsInfos);
195+
}
196+
});
197+
assertTrue(ex.getMessage().contains("Storage pool name is null or empty"));
198+
}
199+
200+
@Test
201+
public void testInitialize_nullProviderName() {
202+
Map<String, Object> dsInfos = new HashMap<>();
203+
dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false");
204+
dsInfos.put("zoneId",1L);
205+
dsInfos.put("podId",1L);
206+
dsInfos.put("clusterId", 1L);
207+
dsInfos.put("name", "testStoragePool");
208+
dsInfos.put("providerName", null);
209+
dsInfos.put("capacityBytes",200000L);
210+
dsInfos.put("managed",true);
211+
dsInfos.put("tags", "testTag");
212+
dsInfos.put("isTagARule", false);
213+
dsInfos.put("details", new HashMap<String, String>());
214+
215+
Exception ex = assertThrows(CloudRuntimeException.class, () -> {
216+
try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) {
217+
storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy);
218+
ontapPrimaryDatastoreLifecycle.initialize(dsInfos);
219+
}
220+
});
221+
assertTrue(ex.getMessage().contains("Provider name is null or empty"));
222+
}
223+
224+
@Test
225+
public void testInitialize_nullPodAndClusterAndZone() {
226+
Map<String, Object> dsInfos = new HashMap<>();
227+
dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false");
228+
dsInfos.put("zoneId",null);
229+
dsInfos.put("podId",null);
230+
dsInfos.put("clusterId", null);
231+
dsInfos.put("name", "testStoragePool");
232+
dsInfos.put("providerName", "testProvider");
233+
dsInfos.put("capacityBytes",200000L);
234+
dsInfos.put("managed",true);
235+
dsInfos.put("tags", "testTag");
236+
dsInfos.put("isTagARule", false);
237+
dsInfos.put("details", new HashMap<String, String>());
238+
239+
Exception ex = assertThrows(CloudRuntimeException.class, () -> {
240+
try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) {
241+
storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy);
242+
ontapPrimaryDatastoreLifecycle.initialize(dsInfos);
243+
}
244+
});
245+
assertTrue(ex.getMessage().contains("Pod Id, Cluster Id and Zone Id are all null"));
246+
}
247+
248+
@Test
249+
public void testInitialize_clusterNotKVM() {
250+
ClusterVO clusterVO = new ClusterVO(2L, 1L, "clusterName");
251+
clusterVO.setHypervisorType("XenServer");
252+
when(_clusterDao.findById(2L)).thenReturn(clusterVO);
253+
254+
Map<String, Object> dsInfos = new HashMap<>();
255+
dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false");
256+
dsInfos.put("zoneId",1L);
257+
dsInfos.put("podId",1L);
258+
dsInfos.put("clusterId", 2L);
259+
dsInfos.put("name", "testStoragePool");
260+
dsInfos.put("providerName", "testProvider");
261+
dsInfos.put("capacityBytes",200000L);
262+
dsInfos.put("managed",true);
263+
dsInfos.put("tags", "testTag");
264+
dsInfos.put("isTagARule", false);
265+
dsInfos.put("details", new HashMap<String, String>());
266+
267+
Exception ex = assertThrows(CloudRuntimeException.class, () -> {
268+
try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) {
269+
storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy);
270+
ontapPrimaryDatastoreLifecycle.initialize(dsInfos);
271+
}
272+
});
273+
assertTrue(ex.getMessage().contains("ONTAP primary storage is supported only for KVM hypervisor"));
274+
}
275+
276+
@Test
277+
public void testInitialize_unexpectedDetailKey() {
278+
Map<String, Object> dsInfos = new HashMap<>();
279+
dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false;unexpectedKey=unexpectedValue");
280+
dsInfos.put("zoneId",1L);
281+
dsInfos.put("podId",1L);
282+
dsInfos.put("clusterId", 1L);
283+
dsInfos.put("name", "testStoragePool");
284+
dsInfos.put("providerName", "testProvider");
285+
dsInfos.put("capacityBytes",200000L);
286+
dsInfos.put("managed",true);
287+
dsInfos.put("tags", "testTag");
288+
dsInfos.put("isTagARule", false);
289+
dsInfos.put("details", new HashMap<String, String>());
290+
291+
Exception ex = assertThrows(CloudRuntimeException.class, () -> {
292+
try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) {
293+
storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy);
294+
ontapPrimaryDatastoreLifecycle.initialize(dsInfos);
295+
}
296+
});
297+
assertTrue(ex.getMessage().contains("Unexpected ONTAP detail key in URL"));
298+
}
299+
300+
}

0 commit comments

Comments
 (0)