From 7cfe8d9567852630029fce927176cf835ac0b910 Mon Sep 17 00:00:00 2001 From: Thomas Florio Date: Tue, 17 Oct 2023 16:11:23 +0200 Subject: [PATCH 1/2] Improve Exceptions handling class --- java/code/src/com/suse/utils/Exceptions.java | 44 ++++++++++++++------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/java/code/src/com/suse/utils/Exceptions.java b/java/code/src/com/suse/utils/Exceptions.java index 33f187436deb..4a9e61546af4 100644 --- a/java/code/src/com/suse/utils/Exceptions.java +++ b/java/code/src/com/suse/utils/Exceptions.java @@ -20,28 +20,45 @@ public final class Exceptions { /** * Represents an operation that does not accept inputs and returns no result, but may throw an exception. + * @param type of exception */ @FunctionalInterface - public interface ThrowingOperation { + public interface ThrowingRunnable { /** * Performs this operation. - * @throws Exception when an error occurs during the execution + * @throws E when an error occurs during the execution */ - void execute() throws Exception; + void run() throws E; } /** * Represents an operation that does not accept inputs and returns a value, but may throw an exception. * @param the type of the return value + * @param type of exception */ @FunctionalInterface - public interface ThrowingSupplier { + public interface ThrowingSupplier { /** * Performs this operation. * @return the result of the operation - * @throws Exception when an error occurs during the execution + * @throws E when an error occurs during the execution */ - T execute() throws Exception; + T get() throws E; + } + + /** + * Represents an operation that accepts a single input argument and returns no result, but may throw an exception. + * @param the type of the return value + * @param type of exception + */ + @FunctionalInterface + public interface ThrowingConsumer { + /** + * Performs this operation. + * @param value the value to consume + * @throws E when an error occurs during the execution + */ + void accept(T value) throws E; } private Exceptions() { @@ -51,11 +68,12 @@ private Exceptions() { /** * Executes an operation and returns the exception if occurred during the execution. * @param operation the operation to perform + * @param type of exception * @return an optional wrapping the exception, or empty if the operation completes successfully. */ - public static Optional handleByReturning(ThrowingOperation operation) { + public static Optional handleByReturning(ThrowingRunnable operation) { try { - operation.execute(); + operation.run(); return Optional.empty(); } catch (Exception ex) { @@ -66,11 +84,12 @@ public static Optional handleByReturning(ThrowingOperation /** * Executes an operation and wraps any exception into a runtime exception. * @param operation the operation to perform + * @param type of exception * @throws RuntimeException if an exception occurs during the operation. */ - public static void handleByWrapping(ThrowingOperation operation) { + public static void handleByWrapping(ThrowingRunnable operation) { handleByWrapping(() -> { - operation.execute(); + operation.run(); return null; }); } @@ -79,12 +98,13 @@ public static void handleByWrapping(ThrowingOperation operation) { * Executes an operation and wraps any exception into a runtime exception. * @param operation the operation to perform * @param the type of the return value of the operation + * @param type of exception * @return the result value of the operation * @throws RuntimeException if an exception occurs during the operation. */ - public static T handleByWrapping(ThrowingSupplier operation) { + public static T handleByWrapping(ThrowingSupplier operation) { try { - return operation.execute(); + return operation.get(); } catch (Exception ex) { throw new RuntimeException("Unable to execute operation", ex); From 9a9af016000b2f16437d7d148d1409eee8a123f5 Mon Sep 17 00:00:00 2001 From: Thomas Florio Date: Tue, 17 Oct 2023 16:23:21 +0200 Subject: [PATCH 2/2] Add test to ensure updateRepositoriesPayg() does not call SCC --- .../manager/content/ContentSyncManager.java | 2 +- .../content/test/ContentSyncManagerTest.java | 31 +++++++++++++++++-- .../rhn/testing/MockObjectTestCase.java | 14 +++++---- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/java/code/src/com/redhat/rhn/manager/content/ContentSyncManager.java b/java/code/src/com/redhat/rhn/manager/content/ContentSyncManager.java index 718ccfec6e53..463cdb11c6c3 100644 --- a/java/code/src/com/redhat/rhn/manager/content/ContentSyncManager.java +++ b/java/code/src/com/redhat/rhn/manager/content/ContentSyncManager.java @@ -2488,7 +2488,7 @@ protected boolean accessibleUrl(String url, String user, String password) { * @throws SCCClientException when access is not possible * @return {@link SCCWebClient} */ - private SCCClient getSCCClient(Credentials credentials) + protected SCCClient getSCCClient(Credentials credentials) throws URISyntaxException, SCCClientException { // check that URL is valid URI url = new URI(Config.get().getString(ConfigDefaults.SCC_URL)); diff --git a/java/code/src/com/redhat/rhn/manager/content/test/ContentSyncManagerTest.java b/java/code/src/com/redhat/rhn/manager/content/test/ContentSyncManagerTest.java index 7621f2f47ed4..93fd702912dd 100644 --- a/java/code/src/com/redhat/rhn/manager/content/test/ContentSyncManagerTest.java +++ b/java/code/src/com/redhat/rhn/manager/content/test/ContentSyncManagerTest.java @@ -16,6 +16,8 @@ import static com.redhat.rhn.domain.channel.test.ChannelFactoryTest.createTestClonedChannel; +import static com.redhat.rhn.testing.RhnBaseTestCase.assertContains; +import static com.redhat.rhn.testing.RhnBaseTestCase.assertNotEmpty; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -67,7 +69,7 @@ import com.redhat.rhn.manager.content.ProductTreeEntry; import com.redhat.rhn.manager.setup.MirrorCredentialsManager; import com.redhat.rhn.manager.system.SystemManager; -import com.redhat.rhn.testing.BaseTestCaseWithUser; +import com.redhat.rhn.testing.JMockBaseTestCaseWithUser; import com.redhat.rhn.testing.TestUtils; import com.suse.cloud.CloudPaygManager; @@ -75,6 +77,7 @@ import com.suse.manager.webui.services.pillar.MinionPillarManager; import com.suse.mgrsync.MgrSyncStatus; import com.suse.salt.netapi.parser.JsonParser; +import com.suse.scc.client.SCCClient; import com.suse.scc.model.ChannelFamilyJson; import com.suse.scc.model.SCCProductJson; import com.suse.scc.model.SCCRepositoryJson; @@ -111,7 +114,7 @@ /** * Tests for {@link ContentSyncManager}. */ -public class ContentSyncManagerTest extends BaseTestCaseWithUser { +public class ContentSyncManagerTest extends JMockBaseTestCaseWithUser { // Files we read private static final String JARPATH = "/com/redhat/rhn/manager/content/test/"; @@ -2380,6 +2383,30 @@ public void testBuildRepoFileUrl() throws Exception { assertEquals(2, csm.buildRepoFileUrl(repourl, rpmrepo).size()); } + @Test + public void updateRepositoriesForPaygDoNotCallSCC() { + Credentials credentials = CredentialsFactory.createSCCCredentials(); + credentials.setPassword("dummy"); + credentials.setUrl("dummy"); + credentials.setUsername("dummy"); + credentials.setUser(user); + CredentialsFactory.storeCredentials(credentials); + + SCCClient sccClient = mock(SCCClient.class); + + checking(expectations -> { + expectations.never(sccClient).listRepositories(); + }); + + ContentSyncManager csm = new ContentSyncManager() { + @Override + protected SCCClient getSCCClient(Credentials credentials) { + return sccClient; + } + }; + + csm.updateRepositoriesPayg(); + } /** * {@inheritDoc} diff --git a/java/code/src/com/redhat/rhn/testing/MockObjectTestCase.java b/java/code/src/com/redhat/rhn/testing/MockObjectTestCase.java index 18220333c48e..c8e0ad50550a 100644 --- a/java/code/src/com/redhat/rhn/testing/MockObjectTestCase.java +++ b/java/code/src/com/redhat/rhn/testing/MockObjectTestCase.java @@ -15,6 +15,8 @@ package com.redhat.rhn.testing; +import com.suse.utils.Exceptions; + import org.jmock.Expectations; import org.jmock.Mockery; import org.jmock.States; @@ -24,8 +26,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.RegisterExtension; -import java.util.function.Consumer; - /** * jMock boilerplate. */ @@ -59,10 +59,12 @@ public void checking(ExpectationBuilder expectations) { /** * @param expectationsConsumer consumer to build the expectations */ - public void checking(Consumer expectationsConsumer) { - Expectations expectations = new Expectations(); - expectationsConsumer.accept(expectations); - context.checking(expectations); + public void checking(Exceptions.ThrowingConsumer expectationsConsumer) { + Exceptions.handleByWrapping(() -> { + Expectations expectations = new Expectations(); + expectationsConsumer.accept(expectations); + context.checking(expectations); + }); } /**