Skip to content

Commit 491028f

Browse files
authored
Added unit tests for CachedFile and PatchFile (#150)
* adding unit tests for Cache and Patch file classes * added unit test for system proxy setting * added OPatchFile unit test
1 parent 598d8af commit 491028f

File tree

7 files changed

+258
-35
lines changed

7 files changed

+258
-35
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/cachestore/CacheStoreFactory.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ public class CacheStoreFactory implements Supplier<CacheStore> {
1717
cashStoreMap.put(Constants.FILE_CACHE, FileCacheStore.CACHE_STORE);
1818
}
1919

20-
public CacheStore getCacheStore(String backingType) {
21-
return cashStoreMap.getOrDefault(backingType.toUpperCase(), FileCacheStore.CACHE_STORE);
22-
}
23-
2420
@Override
2521
public CacheStore get() {
2622
return FileCacheStore.CACHE_STORE;

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/WLSInstallHelper.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
package com.oracle.weblogic.imagetool.cli.menu;
55

66
import java.io.File;
7-
import java.util.List;
87
import java.util.Properties;
98

10-
import com.oracle.weblogic.imagetool.api.model.CachedFile;
11-
import com.oracle.weblogic.imagetool.installer.InstallerType;
129
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
1310
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
1411
import com.oracle.weblogic.imagetool.util.Constants;
@@ -57,29 +54,4 @@ public static void copyOptionsFromImage(String fromImage, DockerfileOptions dock
5754
dockerfileOptions.setPackageInstaller(Constants.YUM);
5855
}
5956
}
60-
61-
/**
62-
* Return a list of basic installers.
63-
*
64-
* @param initialList An initial non-null list of installers
65-
* @param installerType installer type
66-
* @param installerVersion installer version
67-
* @param jdkVersion jdkVersion of the installer
68-
* @param dockerfileOptions non null docker options
69-
* @return list of installers
70-
*/
71-
public static List<CachedFile> getBasicInstallers(List<CachedFile> initialList,
72-
String installerType, String installerVersion,
73-
String jdkVersion, DockerfileOptions dockerfileOptions) {
74-
logger.finer("IMG-0001", installerType, installerVersion);
75-
initialList.add(new CachedFile(InstallerType.fromValue(installerType),
76-
installerVersion));
77-
if (dockerfileOptions.installJava()) {
78-
logger.finer("IMG-0001", InstallerType.JDK, jdkVersion);
79-
initialList.add(new CachedFile(InstallerType.JDK, jdkVersion));
80-
}
81-
logger.exiting(initialList.size());
82-
return initialList;
83-
84-
}
8557
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) 2020, Oracle Corporation and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.api.model;
5+
6+
import java.io.FileNotFoundException;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
import java.util.logging.Level;
13+
14+
import com.oracle.weblogic.imagetool.cachestore.CacheStore;
15+
import com.oracle.weblogic.imagetool.cachestore.CacheStoreTestImpl;
16+
import com.oracle.weblogic.imagetool.cachestore.OPatchFile;
17+
import com.oracle.weblogic.imagetool.installer.InstallerType;
18+
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
19+
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Tag;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.io.TempDir;
24+
25+
import static com.oracle.weblogic.imagetool.cachestore.OPatchFile.DEFAULT_BUG_NUM;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
29+
30+
@Tag("unit")
31+
public class CachedFileTest {
32+
33+
private static final CacheStore cacheStore = new CacheStoreTestImpl();
34+
private static final List<String> fileContents = Arrays.asList("A", "B", "C");
35+
private static final String SOME_VERSION = "12.2.1.3.0";
36+
37+
@BeforeAll
38+
static void setup(@TempDir Path tempDir) throws IOException {
39+
// build a fake cache with two installers
40+
String key1 = "wls_" + SOME_VERSION;
41+
Path path1 = tempDir.resolve("installer.file.122130.jar");
42+
Files.write(path1, fileContents);
43+
cacheStore.addToCache(key1, path1.toString());
44+
cacheStore.addToCache("wls_12.2.1.4.0", "/dont/care");
45+
46+
// OPatch files
47+
cacheStore.addToCache(DEFAULT_BUG_NUM + "_13.9.2.0.0", "/dont/care");
48+
cacheStore.addToCache(DEFAULT_BUG_NUM + "_13.9.4.0.0", "/dont/care");
49+
cacheStore.addToCache(DEFAULT_BUG_NUM + "_13.9.2.2.2", "/dont/care");
50+
}
51+
52+
@Test
53+
void versionString() {
54+
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, SOME_VERSION);
55+
56+
assertEquals("wls_" + SOME_VERSION, wlsInstallerFile.getKey(),
57+
"Cached file getKey failed for WLS installer");
58+
59+
assertEquals(SOME_VERSION, wlsInstallerFile.getVersion(), "CachedFile returned wrong version");
60+
61+
// if the file ID has the version separator, CachedFile should ignore the version passed, and use the version
62+
// in the ID.
63+
CachedFile cf = new CachedFile("something_versionString", SOME_VERSION);
64+
assertEquals("something_versionString", cf.getKey(),
65+
"Cached file getKey failed for version string");
66+
67+
// getVersion should always return the version that was provided in the constructor, not the one in the ID
68+
assertEquals(SOME_VERSION, cf.getVersion(), "CachedFile returned wrong version");
69+
}
70+
71+
@Test
72+
void resolveFile() throws IOException {
73+
// resolve should fail for a CachedFile that is not in the store
74+
CachedFile fakeFile = new CachedFile(InstallerType.WLS, "10.3.6.0.0");
75+
assertThrows(FileNotFoundException.class, () -> fakeFile.resolve(cacheStore));
76+
77+
// CachedFile resolve should result in the same behavior has getting the path from the cache store
78+
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, SOME_VERSION);
79+
String expected = cacheStore.getValueFromCache("wls_" + SOME_VERSION);
80+
assertEquals(expected, wlsInstallerFile.resolve(cacheStore), "resolve failed for CachedFile");
81+
}
82+
83+
@Test
84+
void copyFile(@TempDir Path contextDir) throws IOException {
85+
LoggingFacade logger = LoggingFactory.getLogger(CachedFile.class);
86+
Level oldLevel = logger.getLevel();
87+
logger.setLevel(Level.OFF);
88+
try {
89+
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, SOME_VERSION);
90+
// copy the file from the cache store to the fake build context directory
91+
Path result = wlsInstallerFile.copyFile(cacheStore, contextDir.toString());
92+
// check to see if the file was copied correctly by examining the contents of the resulting file
93+
assertLinesMatch(fileContents, Files.readAllLines(result),
94+
"copied file contents do not match source");
95+
} finally {
96+
logger.setLevel(oldLevel);
97+
}
98+
}
99+
100+
@Test
101+
void latestOpatchVersion() {
102+
// OPatch file should default to the default OPatch bug number and the latest version found in cache
103+
OPatchFile patchFile = new OPatchFile(null, null, null, cacheStore);
104+
assertEquals(DEFAULT_BUG_NUM + "_13.9.4.0.0", patchFile.getKey(),
105+
"failed to get correct versoin of the latest OPatch version from cache");
106+
}
107+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2020, Oracle Corporation and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cachestore;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class CacheStoreTestImpl implements CacheStore {
10+
11+
HashMap<String, String> cache = new HashMap<>();
12+
13+
@Override
14+
public String getCacheDir() {
15+
return null;
16+
}
17+
18+
@Override
19+
public String getValueFromCache(String key) {
20+
return cache.get(key);
21+
}
22+
23+
@Override
24+
public boolean hasMatchingKeyValue(String key, String value) {
25+
return false;
26+
}
27+
28+
@Override
29+
public boolean addToCache(String key, String value) {
30+
cache.put(key, value);
31+
return true;
32+
}
33+
34+
@Override
35+
public String deleteFromCache(String key) {
36+
return null;
37+
}
38+
39+
@Override
40+
public Map<String, String> getCacheItems() {
41+
return cache;
42+
}
43+
}

imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/meta/FileCacheStoreTest.java renamed to imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStoreTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Copyright (c) 2019, 2020, Oracle Corporation and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

4-
package com.oracle.weblogic.imagetool.cachestore.meta;
4+
package com.oracle.weblogic.imagetool.cachestore;
55

6-
import com.oracle.weblogic.imagetool.cachestore.CacheStore;
7-
import com.oracle.weblogic.imagetool.cachestore.CacheStoreFactory;
86
import org.junit.jupiter.api.MethodOrderer.Alphanumeric;
97
import org.junit.jupiter.api.Tag;
108
import org.junit.jupiter.api.Test;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2020, Oracle Corporation and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cachestore;
5+
6+
import java.io.FileNotFoundException;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
import java.util.logging.Level;
13+
14+
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
15+
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
16+
import org.junit.jupiter.api.BeforeAll;
17+
import org.junit.jupiter.api.Tag;
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.io.TempDir;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
@Tag("unit")
25+
public class PatchFileTest {
26+
private static final CacheStore cacheStore = new CacheStoreTestImpl();
27+
private static final List<String> fileContents = Arrays.asList("A", "B", "C");
28+
private static final String BUGNUMBER = "123456";
29+
private static final String SOME_VERSION = "12.2.1.3.0";
30+
31+
@BeforeAll
32+
static void setup(@TempDir Path tempDir) throws IOException {
33+
// build a fake cache with two installers
34+
String key1 = BUGNUMBER + "_" + SOME_VERSION;
35+
Path path1 = tempDir.resolve("patch1.zip");
36+
String key2 = "wls_12.2.1.4.0";
37+
Path path2 = tempDir.resolve("installer.file.122140.jar");
38+
cacheStore.addToCache(key1, path1.toString());
39+
cacheStore.addToCache(key2, path2.toString());
40+
Files.write(path1, fileContents);
41+
}
42+
43+
@Test
44+
void simpleVersion() {
45+
PatchFile p1 = new PatchFile(BUGNUMBER, SOME_VERSION, null, null);
46+
assertEquals(SOME_VERSION, p1.getVersion(), "simple patch did not return version provided");
47+
assertEquals(BUGNUMBER, p1.getBugNumber(), "simple patch did not return bug number provided");
48+
}
49+
50+
@Test
51+
void derivedVersion() {
52+
PatchFile p1 = new PatchFile(BUGNUMBER + "_7890", SOME_VERSION, null, null);
53+
assertEquals("7890", p1.getVersion(), "simple patch did not return derived version");
54+
assertEquals(BUGNUMBER, p1.getBugNumber(), "simple patch did not return derived bug number");
55+
}
56+
57+
@Test
58+
void resolveFile() throws IOException {
59+
LoggingFacade logger = LoggingFactory.getLogger(PatchFile.class);
60+
Level oldLevel = logger.getLevel();
61+
logger.setLevel(Level.OFF);
62+
try {
63+
// resolve should fail for a PatchFile that is not in the store
64+
PatchFile p1 = new PatchFile("99999", SOME_VERSION, null, null);
65+
assertThrows(FileNotFoundException.class, () -> p1.resolve(cacheStore));
66+
67+
// PatchFile resolve should result in the same behavior has getting the path from the cache store
68+
PatchFile patch2 = new PatchFile(BUGNUMBER, SOME_VERSION, null, null);
69+
String expected = cacheStore.getValueFromCache(BUGNUMBER + "_" + SOME_VERSION);
70+
assertEquals(expected, patch2.resolve(cacheStore), "failed to resolve patch in cache");
71+
} finally {
72+
logger.setLevel(oldLevel);
73+
}
74+
}
75+
}

imagetool/src/test/java/com/oracle/weblogic/imagetool/util/UtilsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package com.oracle.weblogic.imagetool.util;
55

6+
import java.io.IOException;
7+
68
import org.junit.jupiter.api.Tag;
79
import org.junit.jupiter.api.Test;
810

@@ -23,4 +25,34 @@ void compareVersions() {
2325
void isEmptyString() {
2426
assertTrue(Utils.isEmptyString(""));
2527
}
28+
29+
@Test
30+
void settingProxy() throws IOException {
31+
String host = "www-proxy.mycompany.com";
32+
String port = "80";
33+
String simpleProxy = host + ":" + port;
34+
String simpleHttp = "http://" + simpleProxy;
35+
String simpleHttps = "https://" + simpleProxy;
36+
37+
// proxy value with matching protocol, http for http, and https for https
38+
Utils.setProxyIfRequired(simpleHttp, simpleHttps, "");
39+
assertEquals(host, System.getProperty("http.proxyHost"));
40+
assertEquals(port, System.getProperty("http.proxyPort"));
41+
assertEquals(host, System.getProperty("https.proxyHost"));
42+
assertEquals(port, System.getProperty("https.proxyPort"));
43+
44+
// proxy value with same protocol, http for http, and same for https
45+
Utils.setProxyIfRequired(simpleHttp, simpleHttp, "");
46+
assertEquals(host, System.getProperty("http.proxyHost"));
47+
assertEquals(port, System.getProperty("http.proxyPort"));
48+
assertEquals(host, System.getProperty("https.proxyHost"));
49+
assertEquals(port, System.getProperty("https.proxyPort"));
50+
51+
// proxy value with no protocol
52+
Utils.setProxyIfRequired(simpleProxy, simpleProxy, "");
53+
assertEquals(host, System.getProperty("http.proxyHost"));
54+
assertEquals(port, System.getProperty("http.proxyPort"));
55+
assertEquals(host, System.getProperty("https.proxyHost"));
56+
assertEquals(port, System.getProperty("https.proxyPort"));
57+
}
2658
}

0 commit comments

Comments
 (0)