33
44package com .oracle .weblogic .imagetool .cachestore ;
55
6+ import java .io .BufferedReader ;
67import java .io .FileNotFoundException ;
78import java .io .IOException ;
9+ import java .io .InputStreamReader ;
10+ import java .lang .reflect .Field ;
811import java .nio .file .Files ;
912import java .nio .file .Path ;
1013import java .util .Arrays ;
1114import java .util .List ;
1215import java .util .logging .Level ;
16+ import java .util .stream .Collectors ;
1317
1418import com .oracle .weblogic .imagetool .logging .LoggingFacade ;
1519import com .oracle .weblogic .imagetool .logging .LoggingFactory ;
20+ import com .oracle .weblogic .imagetool .util .HttpUtil ;
21+ import org .junit .jupiter .api .AfterAll ;
1622import org .junit .jupiter .api .BeforeAll ;
1723import org .junit .jupiter .api .Tag ;
1824import org .junit .jupiter .api .Test ;
1925import org .junit .jupiter .api .io .TempDir ;
2026
2127import 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 ;
2230import static org .junit .jupiter .api .Assertions .assertThrows ;
2331
2432@ Tag ("unit" )
2533public 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 ;
3040
3141 @ 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 );
3345 // build a fake cache with two installers
3446 String key1 = BUGNUMBER + "_" + SOME_VERSION ;
3547 Path path1 = tempDir .resolve ("patch1.zip" );
@@ -38,6 +50,18 @@ static void setup(@TempDir Path tempDir) throws IOException {
3850 cacheStore .addToCache (key1 , path1 .toString ());
3951 cacheStore .addToCache (key2 , path2 .toString ());
4052 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 );
4165 }
4266
4367 @ Test
@@ -56,20 +80,75 @@ void derivedVersion() {
5680
5781 @ Test
5882 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 , "myname@sample.org" , "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 ));
73119 }
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" );
74153 }
75154}
0 commit comments