Skip to content

Commit 24dc317

Browse files
authored
Testcases Added (#9116)
* added a news tester file and directory for GsonHelper.java. Also created a new test in OVAProcessorTest.java * added testcase to PasswordPolicyImplTest.java * added proper imports for GsonHelperTest.java * expected changed based on commit response * adhere to checkstyle ruleset
1 parent f2a1ee5 commit 24dc317

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
18+
package com.cloud.serializer;
19+
20+
import com.cloud.agent.api.to.NfsTO;
21+
import com.cloud.storage.DataStoreRole;
22+
import com.google.gson.Gson;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.Assert.assertTrue;
29+
30+
/**
31+
* Test cases to verify working order of GsonHelper.java
32+
* with regards to a concrete implementation of the DataStoreTO
33+
* interface
34+
*/
35+
public class GsonHelperTest {
36+
37+
private Gson gson;
38+
private Gson gsonLogger;
39+
private NfsTO nfsTO;
40+
41+
@Before
42+
public void setUp() {
43+
gson = GsonHelper.getGson();
44+
gsonLogger = GsonHelper.getGsonLogger();
45+
nfsTO = new NfsTO("http://example.com", DataStoreRole.Primary);
46+
}
47+
48+
@Test
49+
public void testGsonSerialization() {
50+
String json = gson.toJson(nfsTO);
51+
assertNotNull(json);
52+
assertTrue(json.contains("\"_url\":\"http://example.com\""));
53+
assertTrue(json.contains("\"_role\":\"Primary\""));
54+
}
55+
56+
@Test
57+
public void testGsonDeserialization() {
58+
String json = "{\"_url\":\"http://example.com\",\"_role\":\"Primary\"}";
59+
NfsTO deserializedNfsTO = gson.fromJson(json, NfsTO.class);
60+
assertNotNull(deserializedNfsTO);
61+
assertEquals("http://example.com", deserializedNfsTO.getUrl());
62+
assertEquals(DataStoreRole.Primary, deserializedNfsTO.getRole());
63+
}
64+
65+
@Test
66+
public void testGsonLoggerSerialization() {
67+
String json = gsonLogger.toJson(nfsTO);
68+
assertNotNull(json);
69+
assertTrue(json.contains("\"_url\":\"http://example.com\""));
70+
assertTrue(json.contains("\"_role\":\"Primary\""));
71+
}
72+
73+
@Test
74+
public void testGsonLoggerDeserialization() {
75+
String json ="{\"_url\":\"http://example.com\",\"_role\":\"Primary\"}";
76+
NfsTO deserializedNfsTO = gsonLogger.fromJson(json, NfsTO.class);
77+
assertNotNull(deserializedNfsTO);
78+
assertEquals("http://example.com", deserializedNfsTO.getUrl());
79+
assertEquals(DataStoreRole.Primary, deserializedNfsTO.getRole());
80+
}
81+
}

core/src/test/java/com/cloud/storage/template/OVAProcessorTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,25 @@ public void testGetVirtualSize() throws Exception {
131131
Assert.assertEquals(virtualSize, processor.getVirtualSize(mockFile));
132132
Mockito.verify(mockFile, Mockito.times(0)).length();
133133
}
134+
@Test
135+
public void testProcessWithLargeFileSize() throws Exception {
136+
String templatePath = "/tmp";
137+
String templateName = "large_template";
138+
long virtualSize = 10_000_000_000L;
139+
long actualSize = 5_000_000_000L;
140+
141+
Mockito.when(mockStorageLayer.exists(Mockito.anyString())).thenReturn(true);
142+
Mockito.when(mockStorageLayer.getSize(Mockito.anyString())).thenReturn(actualSize);
143+
Mockito.doReturn(virtualSize).when(processor).getTemplateVirtualSize(Mockito.anyString(), Mockito.anyString());
134144

145+
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class, (mock, context) -> {
146+
Mockito.when(mock.execute()).thenReturn(null);
147+
})) {
148+
Processor.FormatInfo info = processor.process(templatePath, null, templateName);
149+
Assert.assertEquals(Storage.ImageFormat.OVA, info.format);
150+
Assert.assertEquals("actual size:", actualSize, info.size);
151+
Assert.assertEquals("virtual size:", virtualSize, info.virtualSize);
152+
Assert.assertEquals("template name:", templateName + ".ova", info.filename);
153+
}
154+
}
135155
}

server/src/test/java/com/cloud/user/PasswordPolicyImplTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,22 @@ public void validateRegexTestWithoutRegex() {
160160
passwordPolicySpy.validateIfPasswordMatchesRegex("abcd123", "user", null);
161161
}
162162

163+
@Test
164+
public void validateCombinationOfPolicies() {
165+
Mockito.doReturn(2).when(passwordPolicySpy).getPasswordPolicyMinimumSpecialCharacters(null);
166+
Mockito.doReturn(1).when(passwordPolicySpy).getPasswordPolicyMinimumUpperCaseLetters(null);
167+
Mockito.doReturn(1).when(passwordPolicySpy).getPasswordPolicyMinimumLowerCaseLetters(null);
168+
Mockito.doReturn(1).when(passwordPolicySpy).getPasswordPolicyMinimumDigits(null);
169+
Mockito.doReturn(8).when(passwordPolicySpy).getPasswordPolicyMinimumLength(null);
170+
Mockito.doReturn(false).when(passwordPolicySpy).getPasswordPolicyAllowPasswordToContainUsername(null);
171+
172+
String password = "Ab1!@#cd";
173+
passwordPolicySpy.validateIfPasswordContainsTheMinimumNumberOfSpecialCharacters(2, password, null);
174+
passwordPolicySpy.validateIfPasswordContainsTheMinimumNumberOfUpperCaseLetters(1, password, null);
175+
passwordPolicySpy.validateIfPasswordContainsTheMinimumNumberOfLowerCaseLetters(1, password, null);
176+
passwordPolicySpy.validateIfPasswordContainsTheMinimumNumberOfDigits(1, password, null);
177+
passwordPolicySpy.validateIfPasswordContainsTheMinimumLength(password, "user", null);
178+
passwordPolicySpy.validateIfPasswordContainsTheUsername(password, "user", null);
179+
}
180+
163181
}

0 commit comments

Comments
 (0)