|
| 1 | +// Copyright (c) 2022, Oracle 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.cli.menu; |
| 5 | + |
| 6 | +import java.lang.reflect.Field; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.logging.Level; |
| 10 | + |
| 11 | +import com.oracle.weblogic.imagetool.aru.AruException; |
| 12 | +import com.oracle.weblogic.imagetool.aru.AruPatch; |
| 13 | +import com.oracle.weblogic.imagetool.aru.AruUtil; |
| 14 | +import com.oracle.weblogic.imagetool.aru.InvalidCredentialException; |
| 15 | +import com.oracle.weblogic.imagetool.installer.FmwInstallerType; |
| 16 | +import com.oracle.weblogic.imagetool.logging.LoggingFacade; |
| 17 | +import com.oracle.weblogic.imagetool.logging.LoggingFactory; |
| 18 | +import com.oracle.weblogic.imagetool.util.InvalidPatchIdFormatException; |
| 19 | +import org.junit.jupiter.api.AfterAll; |
| 20 | +import org.junit.jupiter.api.BeforeAll; |
| 21 | +import org.junit.jupiter.api.Tag; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import picocli.CommandLine; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 29 | + |
| 30 | +@Tag("unit") |
| 31 | +class CommonPatchingOptionsTest { |
| 32 | + private static final LoggingFacade logger = LoggingFactory.getLogger(AruUtil.class); |
| 33 | + private static Level oldLevel; |
| 34 | + |
| 35 | + @BeforeAll |
| 36 | + static void setUp() { |
| 37 | + oldLevel = logger.getLevel(); |
| 38 | + logger.setLevel(Level.SEVERE); |
| 39 | + } |
| 40 | + |
| 41 | + public static class TestAruUtil extends AruUtil { |
| 42 | + |
| 43 | + public TestAruUtil() { |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean checkCredentials(String username, String password) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public List<AruPatch> getRecommendedPatches(FmwInstallerType type, String version, |
| 53 | + String userId, String password) throws AruException { |
| 54 | + List<AruPatch> list = new ArrayList<>(); |
| 55 | + list.add(new AruPatch().patchId("1").description("psu").psuBundle("x")); |
| 56 | + list.add(new AruPatch().patchId("2").description("blah")); |
| 57 | + list.add(new AruPatch().patchId("3").description("ADR FOR WEBLOGIC SERVER")); |
| 58 | + return list; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @AfterAll |
| 63 | + static void tearDown() { |
| 64 | + logger.setLevel(oldLevel); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void noPassword() { |
| 69 | + CreateImage createImage = new CreateImage(); |
| 70 | + new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek"); |
| 71 | + assertThrows(InvalidCredentialException.class, createImage::initializeOptions); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void withPassword() throws Exception { |
| 76 | + CreateImage createImage = new CreateImage(); |
| 77 | + new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx"); |
| 78 | + |
| 79 | + // insert test class into AruUtil to intercept REST calls to ARU |
| 80 | + Field aruRest = AruUtil.class.getDeclaredField("instance"); |
| 81 | + aruRest.setAccessible(true); |
| 82 | + aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil()); |
| 83 | + |
| 84 | + assertFalse(createImage.applyingPatches(), "No patches on the command line, but applyingPatches is true"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void invalidPatchId() throws Exception { |
| 89 | + CreateImage createImage = new CreateImage(); |
| 90 | + new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx", |
| 91 | + "--patches", "abc"); |
| 92 | + |
| 93 | + // insert test class into AruUtil to intercept REST calls to ARU |
| 94 | + Field aruRest = AruUtil.class.getDeclaredField("instance"); |
| 95 | + aruRest.setAccessible(true); |
| 96 | + aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil()); |
| 97 | + |
| 98 | + assertThrows(InvalidPatchIdFormatException.class, createImage::initializeOptions); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void invalidPatchIdTooFewDigits() throws Exception { |
| 103 | + CreateImage createImage = new CreateImage(); |
| 104 | + new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx", |
| 105 | + "--patches", "1234567"); |
| 106 | + |
| 107 | + // insert test class into AruUtil to intercept REST calls to ARU |
| 108 | + Field aruRest = AruUtil.class.getDeclaredField("instance"); |
| 109 | + aruRest.setAccessible(true); |
| 110 | + aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil()); |
| 111 | + |
| 112 | + assertThrows(InvalidPatchIdFormatException.class, createImage::initializeOptions); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void validPatchId() throws Exception { |
| 117 | + CreateImage createImage = new CreateImage(); |
| 118 | + new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx", |
| 119 | + "--patches", "12345678"); |
| 120 | + |
| 121 | + // insert test class into AruUtil to intercept REST calls to ARU |
| 122 | + Field aruRest = AruUtil.class.getDeclaredField("instance"); |
| 123 | + aruRest.setAccessible(true); |
| 124 | + aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil()); |
| 125 | + |
| 126 | + createImage.initializeOptions(); |
| 127 | + assertTrue(createImage.applyingPatches(), "User provided patches but applyingPatches still false"); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void doNotGetRecommendedPatches() throws Exception { |
| 132 | + CreateImage createImage = new CreateImage(); |
| 133 | + new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx", |
| 134 | + "--patches", "12345678"); |
| 135 | + |
| 136 | + // insert test class into AruUtil to intercept REST calls to ARU |
| 137 | + Field aruRest = AruUtil.class.getDeclaredField("instance"); |
| 138 | + aruRest.setAccessible(true); |
| 139 | + aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil()); |
| 140 | + |
| 141 | + List<AruPatch> patches = createImage.getRecommendedPatchList(FmwInstallerType.WLS); |
| 142 | + assertTrue(patches.isEmpty(), "Neither latestPSU nor recommendedPatches was specified, but returned patches"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void getRecommendedPatches() throws Exception { |
| 147 | + CreateImage createImage = new CreateImage(); |
| 148 | + new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx", |
| 149 | + "--patches", "12345678", "--recommendedPatches"); |
| 150 | + |
| 151 | + // insert test class into AruUtil to intercept REST calls to ARU |
| 152 | + Field aruRest = AruUtil.class.getDeclaredField("instance"); |
| 153 | + aruRest.setAccessible(true); |
| 154 | + aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil()); |
| 155 | + |
| 156 | + createImage.initializeOptions(); |
| 157 | + List<AruPatch> patches = createImage.getRecommendedPatchList(FmwInstallerType.WLS); |
| 158 | + assertFalse(patches.isEmpty(), "recommendedPatches was specified, but returned patches was empty"); |
| 159 | + assertEquals(2, patches.size(),"ADR patch was not removed?"); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void getRecommendedPatchesWithoutCredentials() throws Exception { |
| 164 | + CreateImage createImage = new CreateImage(); |
| 165 | + new CommandLine(createImage).parseArgs("--tag", "tag:1", |
| 166 | + "--patches", "12345678", "--recommendedPatches"); |
| 167 | + |
| 168 | + // insert test class into AruUtil to intercept REST calls to ARU |
| 169 | + Field aruRest = AruUtil.class.getDeclaredField("instance"); |
| 170 | + aruRest.setAccessible(true); |
| 171 | + aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil()); |
| 172 | + |
| 173 | + createImage.initializeOptions(); |
| 174 | + assertThrows(IllegalArgumentException.class, () -> createImage.getRecommendedPatchList(FmwInstallerType.WLS)); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments