Skip to content

Commit c17effd

Browse files
authored
Merge pull request #6 from oracle/unit_tests
add unit tests_1
2 parents bf3969f + efb95d9 commit c17effd

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.oracle.weblogic.imagetool.cli.cache;
2+
3+
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
4+
import com.oracle.weblogic.imagetool.cli.WLSCommandLine;
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import picocli.CommandLine;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.PrintStream;
11+
12+
import static org.junit.Assert.*;
13+
14+
public class AddEntryTest {
15+
16+
private ByteArrayOutputStream byteArrayOutputStream = null;
17+
private PrintStream printStream = null;
18+
19+
@Before
20+
public void setup() {
21+
byteArrayOutputStream = new ByteArrayOutputStream();
22+
printStream = new PrintStream(byteArrayOutputStream);
23+
}
24+
25+
@After
26+
public void teardown() {
27+
if (printStream != null) {
28+
printStream.close();
29+
}
30+
}
31+
32+
@Test
33+
public void testMissingParameters() {
34+
WLSCommandLine.call(new AddEntry(), printStream, printStream, CommandLine.Help.Ansi.AUTO, true, true);
35+
assertTrue(new String(byteArrayOutputStream.toByteArray()).contains("Missing required options"));
36+
}
37+
38+
@Test
39+
public void testMissingKey() {
40+
WLSCommandLine.call(new AddEntry(), printStream, printStream, CommandLine.Help.Ansi.AUTO, true, true,
41+
"--value", "some_value");
42+
assertTrue(new String(byteArrayOutputStream.toByteArray()).contains("Missing required option '--key=<key>'"));
43+
}
44+
45+
@Test
46+
public void testMissingValue() {
47+
WLSCommandLine.call(new AddEntry(), printStream, printStream, CommandLine.Help.Ansi.AUTO, true, true,
48+
"--key", "some_key");
49+
assertTrue(new String(byteArrayOutputStream.toByteArray()).contains("Missing required option '--value=<location>'"));
50+
}
51+
52+
@Test
53+
public void testInvalidParameters() {
54+
CommandResponse response = WLSCommandLine.call(new AddEntry(), true, "--key", "", "--value", "");
55+
assertEquals(-1, response.getStatus());
56+
}
57+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.oracle.weblogic.imagetool.cli.cache;
2+
3+
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
4+
import com.oracle.weblogic.imagetool.api.model.InstallerType;
5+
import com.oracle.weblogic.imagetool.cli.WLSCommandLine;
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import picocli.CommandLine;
10+
import java.io.ByteArrayOutputStream;
11+
import java.io.PrintStream;
12+
13+
import static org.junit.Assert.*;
14+
15+
public class AddInstallerEntryTest {
16+
17+
private ByteArrayOutputStream byteArrayOutputStream = null;
18+
private PrintStream printStream = null;
19+
20+
@Before
21+
public void setup() {
22+
byteArrayOutputStream = new ByteArrayOutputStream();
23+
printStream = new PrintStream(byteArrayOutputStream);
24+
}
25+
26+
@After
27+
public void teardown() {
28+
if (printStream != null) {
29+
printStream.close();
30+
}
31+
}
32+
33+
@Test
34+
public void testMissingParameters() {
35+
WLSCommandLine.call(new AddInstallerEntry(), printStream, printStream, CommandLine.Help.Ansi.AUTO, true, true);
36+
assertTrue(new String(byteArrayOutputStream.toByteArray()).contains("Missing required options"));
37+
}
38+
39+
@Test
40+
public void testWrongType() {
41+
WLSCommandLine.call(new AddInstallerEntry(), printStream, printStream, CommandLine.Help.Ansi.AUTO, true, true,
42+
"--type", "a2z", "--version", "some_value", "--path", "/path/to/a/file");
43+
assertTrue(new String(byteArrayOutputStream.toByteArray()).contains(
44+
"Invalid value for option '--type'"));
45+
}
46+
47+
@Test
48+
public void testMissingVersion() {
49+
WLSCommandLine.call(new AddInstallerEntry(), printStream, printStream, CommandLine.Help.Ansi.AUTO, true, true,
50+
"--type", InstallerType.WLS.toString(), "--path", "/path/to/a/file");
51+
assertTrue(new String(byteArrayOutputStream.toByteArray()).contains(
52+
"Missing required option '--version=<version>'"));
53+
}
54+
55+
@Test
56+
public void testInvalidParameters() {
57+
CommandResponse response = WLSCommandLine.call(new AddInstallerEntry(), true, "--type",
58+
InstallerType.WLS.toString(), "--version", "", "--path", "/path/to/non/existent/file");
59+
assertEquals(-1, response.getStatus());
60+
}
61+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.oracle.weblogic.imagetool.impl.meta;
2+
3+
import com.oracle.weblogic.imagetool.api.meta.CacheStore;
4+
import com.oracle.weblogic.imagetool.util.Constants;
5+
import org.junit.FixMethodOrder;
6+
import org.junit.Test;
7+
import org.junit.runners.MethodSorters;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertNotNull;
16+
import static org.junit.Assert.assertNull;
17+
import static org.junit.Assert.assertTrue;
18+
19+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
20+
public class FileCacheStoreTest {
21+
22+
private CacheStore cacheStore = new CacheStoreFactory().getCacheStore(Constants.FILE_CACHE);
23+
24+
@Test
25+
public void a1setCacheDir() throws IOException {
26+
Path cacheDirPath = Files.createTempDirectory("test_imagetool");
27+
cacheDirPath.toFile().deleteOnExit();
28+
// change the cache directory from default so it doesn't modify user data
29+
cacheStore.setCacheDir(cacheDirPath.toAbsolutePath().toString());
30+
}
31+
32+
@Test
33+
public void a2getCacheDir() {
34+
assertNotNull(cacheStore.getCacheDir());
35+
File cacheDir = new File(cacheStore.getCacheDir());
36+
assertTrue(cacheDir.exists() && cacheDir.isDirectory());
37+
}
38+
39+
@Test
40+
public void a3addToCache() {
41+
assertTrue(cacheStore.addToCache("abc_xyz_123", "this_is_a_test"));
42+
}
43+
44+
@Test
45+
public void a4getValueFromCache() {
46+
assertEquals("this_is_a_test", cacheStore.getValueFromCache("abc_xyz_123"));
47+
}
48+
49+
@Test
50+
public void a5hasMatchingKeyValue() {
51+
assertTrue(cacheStore.hasMatchingKeyValue("abc_xyz_123", "this_is_a_test"));
52+
}
53+
54+
@Test
55+
public void a6deleteFromCache() {
56+
assertNull(cacheStore.deleteFromCache("non_existent_key"));
57+
assertEquals("this_is_a_test", cacheStore.deleteFromCache("abc_xyz_123"));
58+
}
59+
60+
@Test
61+
public void a7getCacheItems() {
62+
assertNotNull(cacheStore.getCacheItems());
63+
}
64+
}

0 commit comments

Comments
 (0)