Skip to content

Commit

Permalink
Added unit tests and javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
rjpmestre committed Feb 21, 2025
1 parent 37e4b03 commit ff29e9e
Show file tree
Hide file tree
Showing 27 changed files with 1,556 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
import java.util.WeakHashMap;
import java.util.function.Supplier;

/**
* This class provides various strategies for error reporting and logging.
* Base validation reporting strategy throws a {@link RhnGeneralException} if there are errors.
*/
public class ErrorReportingStrategies {

private ErrorReportingStrategies() {
Expand Down
2 changes: 1 addition & 1 deletion java/code/src/com/redhat/rhn/common/RhnError.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.io.Serializable;

/**
* Represents a base error
* Represents a generic error in scope of RHN
*/
public class RhnError implements Serializable {
private final String message;
Expand Down
3 changes: 3 additions & 0 deletions java/code/src/com/redhat/rhn/common/RhnErrorReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.util.Collections;
import java.util.List;

/**
* Represents a collection of errors that can be reported.
*/
public class RhnErrorReport {
private final List<RhnError> errors = Collections.synchronizedList(new ArrayList<>());

Expand Down
5 changes: 4 additions & 1 deletion java/code/src/com/redhat/rhn/common/RhnReportStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

import java.util.List;

/**
* Interface for strategy reporting errors
* @param <E> the type of errors
*/
public interface RhnReportStrategy<E> {

/**
* Report a list of errors
* @param errors the list of errors
*/
void report(List<E> errors);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2025 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/

package com.redhat.rhn.common.test;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.redhat.rhn.common.ErrorReportingStrategies;
import com.redhat.rhn.common.RhnError;
import com.redhat.rhn.common.RhnGeneralException;
import com.redhat.rhn.common.RhnReportStrategy;
import com.redhat.rhn.common.RhnRuntimeException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.function.Supplier;

public class ErrorReportingStrategiesTest {

@Test
public void testValidationReportingStrategyThrowsExceptionOnErrors() {
RhnReportStrategy<RhnError> strategy = ErrorReportingStrategies.validationReportingStrategy();
List<RhnError> errors = List.of(new RhnError("Test error"));

assertThrows(RhnGeneralException.class, () -> strategy.report(errors));
}

@Test
public void testRaiseAndLog() {
String testMessage = "Test error message";
Supplier<RhnRuntimeException> exceptionSupplier = ErrorReportingStrategies.raiseAndLog(this, testMessage);

RhnRuntimeException exception = exceptionSupplier.get();
assertTrue(exception.getMessage().contains(testMessage));
}

@Test
public void testRaiseAndLogLogsMessage() {
String testMessage = "Test error message";
Supplier<RhnRuntimeException> exceptionSupplier = ErrorReportingStrategies.raiseAndLog(this, testMessage);

// Capture the log output
Logger logger = LogManager.getLogger(this.getClass().getName());
logger.error(testMessage);

RhnRuntimeException exception = exceptionSupplier.get();
assertTrue(exception.getMessage().contains(testMessage));
}
}
79 changes: 79 additions & 0 deletions java/code/src/com/redhat/rhn/common/test/RhnErrorReportTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2025 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/

package com.redhat.rhn.common.test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.redhat.rhn.common.RhnError;
import com.redhat.rhn.common.RhnErrorReport;
import com.redhat.rhn.common.RhnGeneralException;
import com.redhat.rhn.common.RhnReportStrategy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

public class RhnErrorReportTest {

public static final String DUMMY_ERROR_MESSAGE = "dummy error";
private RhnErrorReport errorReport;

@BeforeEach
public void setUp() {
errorReport = new RhnErrorReport();
}


@Test
public void testHasErrors() {
assertFalse(errorReport.hasErrors());
errorReport.register(DUMMY_ERROR_MESSAGE);
assertTrue(errorReport.hasErrors());
}

@Test
public void testGetErrors() {
errorReport.register("Test error 1");
errorReport.register("Test error 2");
List<RhnError> errors = errorReport.getErrors();
assertEquals(2, errors.size());
assertEquals("Test error 1", errors.get(0).getMessage());
assertEquals("Test error 2", errors.get(1).getMessage());
}

@Test
public void testReportWithStrategy() {
RhnReportStrategy<RhnError> strategy = errors -> assertEquals(1, errors.size());
errorReport.register(DUMMY_ERROR_MESSAGE);
errorReport.report(strategy);
}

@Test
public void testReportWithDefaultStrategyThrowsException() {
errorReport.register(DUMMY_ERROR_MESSAGE);
assertThrows(RhnGeneralException.class, () -> errorReport.report());
}

@Test
public void testReportWithDefaultStrategyNoException() {
assertDoesNotThrow(() -> errorReport.report());
}
}
32 changes: 32 additions & 0 deletions java/code/src/com/redhat/rhn/common/test/RhnErrorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2025 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/

package com.redhat.rhn.common.test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.redhat.rhn.common.RhnError;

import org.junit.jupiter.api.Test;

public class RhnErrorTest {

@Test
public void testGetMessage() {
String testMessage = "Test error message";
RhnError error = new RhnError(testMessage);
assertEquals(testMessage, error.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/

package com.redhat.rhn.common.test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.redhat.rhn.common.RhnError;
import com.redhat.rhn.common.RhnGeneralException;

import org.junit.jupiter.api.Test;

import java.util.List;

public class RhnGeneralExceptionTest {

@Test
public void testGetErrors() {
final String dummyError1 = "Error 1";
final String dummyError2 = "Error 2";
String[] expectedMessages = {dummyError1, dummyError2};

RhnError error1 = new RhnError(dummyError1);
RhnError error2 = new RhnError(dummyError2);

List<RhnError> errors = List.of(error1, error2);
RhnGeneralException exception = new RhnGeneralException(errors);

assertEquals(errors, exception.getErrors());
assertArrayEquals(expectedMessages, exception.getErrorMessages());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2025 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/

package com.redhat.rhn.common.test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.redhat.rhn.common.RhnReportStrategy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

public class RhnReportStrategyTest {

private RhnReportStrategy<String> strategy;

@BeforeEach
public void setUp() {
strategy = errors -> {
if (errors == null || errors.isEmpty()) {
throw new IllegalArgumentException("Errors list cannot be null or empty");
}
};
}

@Test
public void testReportWithErrors() {
List<String> errors = List.of("Error 1", "Error 2");
assertDoesNotThrow(() -> strategy.report(errors));
}

@Test
public void testReportWithEmptyErrors() {
List<String> errors = List.of();
assertThrows(IllegalArgumentException.class, () -> strategy.report(errors));
}

@Test
public void testReportWithNullErrors() {
assertThrows(IllegalArgumentException.class, () -> strategy.report(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.redhat.rhn.domain.action;


import com.redhat.rhn.domain.org.Org;
import com.redhat.rhn.domain.server.MinionSummary;
import com.redhat.rhn.domain.server.Pillar;

Expand All @@ -42,11 +43,13 @@ public class ProxyConfigurationApplyAction extends Action {
* Default constructor
* @param pillarIn the pillar
* @param proxyConfigFilesIn the proxy configuration files
* @param orgIn the organization
*/
public ProxyConfigurationApplyAction(Pillar pillarIn, Map<String, Object> proxyConfigFilesIn) {
public ProxyConfigurationApplyAction(Pillar pillarIn, Map<String, Object> proxyConfigFilesIn, Org orgIn) {
this.setActionType(ActionFactory.TYPE_PROXY_CONFIGURATION_APPLY);
this.pillar = pillarIn;
this.proxyConfigFiles = proxyConfigFilesIn;
this.setOrg(orgIn);
}

public Pillar getPillar() {
Expand Down
Loading

0 comments on commit ff29e9e

Please sign in to comment.