3
3
4
4
package com .oracle .weblogic .imagetool .cachestore ;
5
5
6
+ import java .io .BufferedReader ;
6
7
import java .io .FileNotFoundException ;
7
8
import java .io .IOException ;
9
+ import java .io .InputStreamReader ;
10
+ import java .lang .reflect .Field ;
8
11
import java .nio .file .Files ;
9
12
import java .nio .file .Path ;
10
13
import java .util .Arrays ;
11
14
import java .util .List ;
12
15
import java .util .logging .Level ;
16
+ import java .util .stream .Collectors ;
13
17
14
18
import com .oracle .weblogic .imagetool .logging .LoggingFacade ;
15
19
import com .oracle .weblogic .imagetool .logging .LoggingFactory ;
20
+ import com .oracle .weblogic .imagetool .util .HttpUtil ;
21
+ import org .junit .jupiter .api .AfterAll ;
16
22
import org .junit .jupiter .api .BeforeAll ;
17
23
import org .junit .jupiter .api .Tag ;
18
24
import org .junit .jupiter .api .Test ;
19
25
import org .junit .jupiter .api .io .TempDir ;
20
26
21
27
import static org .junit .jupiter .api .Assertions .assertEquals ;
28
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
29
+ import static org .junit .jupiter .api .Assertions .assertNull ;
22
30
import static org .junit .jupiter .api .Assertions .assertThrows ;
23
31
24
32
@ Tag ("unit" )
25
33
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" ;
34
+ static Path cacheDir ;
35
+ static CacheStore cacheStore ;
36
+ static final List <String > fileContents = Arrays .asList ("A" , "B" , "C" );
37
+ static final String BUGNUMBER = "456789" ;
38
+ static final String SOME_VERSION = "12.2.1.3.0" ;
39
+ static Level originalLogLevel ;
30
40
31
41
@ BeforeAll
32
- static void setup (@ TempDir Path tempDir ) throws IOException {
42
+ static void setup (@ TempDir Path tempDir , @ TempDir Path cacheDir ) throws IOException {
43
+ PatchFileTest .cacheDir = cacheDir ;
44
+ cacheStore = new CacheStoreTestImpl (cacheDir );
33
45
// build a fake cache with two installers
34
46
String key1 = BUGNUMBER + "_" + SOME_VERSION ;
35
47
Path path1 = tempDir .resolve ("patch1.zip" );
@@ -38,6 +50,18 @@ static void setup(@TempDir Path tempDir) throws IOException {
38
50
cacheStore .addToCache (key1 , path1 .toString ());
39
51
cacheStore .addToCache (key2 , path2 .toString ());
40
52
Files .write (path1 , fileContents );
53
+
54
+ // disable console logging
55
+ LoggingFacade logger = LoggingFactory .getLogger (PatchFile .class );
56
+ originalLogLevel = logger .getLevel ();
57
+ logger .setLevel (Level .OFF );
58
+ }
59
+
60
+ @ AfterAll
61
+ static void teardown () {
62
+ // restore original logging level after this test suite completes
63
+ LoggingFacade logger = LoggingFactory .getLogger (PatchFile .class );
64
+ logger .setLevel (originalLogLevel );
41
65
}
42
66
43
67
@ Test
@@ -56,20 +80,75 @@ void derivedVersion() {
56
80
57
81
@ Test
58
82
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 );
83
+ // resolve should fail for a PatchFile that is not in the store
84
+ PatchFile p1 = new PatchFile ("99999" , SOME_VERSION , null , null );
85
+ assertThrows (FileNotFoundException .class , () -> p1 .resolve (cacheStore ));
86
+
87
+ // PatchFile resolve should result in the same behavior has getting the path from the cache store
88
+ PatchFile patch2 = new PatchFile (BUGNUMBER , SOME_VERSION , null , null );
89
+ String expected = cacheStore .getValueFromCache (BUGNUMBER + "_" + SOME_VERSION );
90
+ assertEquals (expected , patch2 .resolve (cacheStore ), "failed to resolve patch in cache" );
91
+ }
92
+
93
+
94
+ private static class TestPatchFile extends PatchFile {
95
+ TestPatchFile (String patchId , String version , String userid , String password ) {
96
+ super (patchId , version , userid , password );
97
+ }
98
+
99
+ @ Override
100
+ public void downloadFile (String url , String fileName , String username , String password ) throws IOException {
101
+ Path newFile = cacheDir .resolve (fileName );
102
+ Files .write (newFile , fileContents );
103
+ }
104
+ }
105
+
106
+
107
+ private PatchFile getPatchFileWithAruInfo (String patchId , String version , String aruXml )
108
+ throws IOException , NoSuchFieldException , IllegalAccessException {
109
+
110
+ Field reader = PatchFile .class .getDeclaredField ("aruInfo" );
111
+ reader .setAccessible (true );
112
+ assertNull (cacheStore .getValueFromCache (patchId ), "ERROR, patch should not exist in cache before test starts" );
113
+ PatchFile patchFile =
new TestPatchFile (
patchId ,
version ,
"[email protected] " ,
"pass" );
114
+ try (BufferedReader buffer = new BufferedReader (new InputStreamReader (
115
+ this .getClass ().getResourceAsStream (aruXml )))) {
116
+
117
+ String aruInfo = buffer .lines ().collect (Collectors .joining ("\n " ));
118
+ reader .set (patchFile , HttpUtil .parseXmlString (aruInfo ));
73
119
}
120
+ return patchFile ;
121
+ }
122
+
123
+ @ Test
124
+ void gettingNewPatch () throws NoSuchFieldException , IllegalAccessException , IOException {
125
+ String patchId = "1110001_12.2.1.3.0" ;
126
+ PatchFile patchFile = getPatchFileWithAruInfo (patchId , "12.2.1.3.0" , "/patch-1110001.xml" );
127
+
128
+ String filePath = patchFile .resolve (cacheStore );
129
+
130
+ assertNotNull (filePath , "Patch resolve() failed to get file path from XML" );
131
+ String filePathFromCache = cacheStore .getValueFromCache (patchId );
132
+ assertNotNull (filePathFromCache , "Could not find new patch in cache" );
133
+ assertEquals (filePath , filePathFromCache , "Patch in cache does not match" );
134
+
135
+ assertEquals ("600000000073715" , patchFile .getReleaseNumber (), "Patch did not find release number" );
136
+ }
137
+
138
+ @ Test
139
+ void gettingNewPatchWithoutVersion () throws NoSuchFieldException , IllegalAccessException , IOException {
140
+ // without the version in the patch ID, the ARU info must contain only one patch
141
+ String patchId = "1110002" ;
142
+ // patch version in XML is actually 12.2.1.1.0, code will warn user and reset patch version
143
+ PatchFile patchFile = getPatchFileWithAruInfo (patchId , "12.2.1.2.0" , "/patch-1110002.xml" );
144
+
145
+ String filePath = patchFile .resolve (cacheStore );
146
+
147
+ assertNotNull (filePath , "Patch resolve() failed to get file path from XML" );
148
+ String filePathFromCache = cacheStore .getValueFromCache (patchId + "_12.2.1.1.0" );
149
+ assertNotNull (filePathFromCache , "Could not find new patch in cache" );
150
+ assertEquals (filePath , filePathFromCache , "Patch in cache does not match" );
151
+
152
+ assertEquals ("600000000055130" , patchFile .getReleaseNumber (), "Patch did not find release number" );
74
153
}
75
154
}
0 commit comments