Martin Peřina has uploaded a new change for review. Change subject: core: Introduce FenceProxySourceType ......................................................................
core: Introduce FenceProxySourceType 1. Introduces FenceProxySourceType enum which holds all available types of fence proxy sources 2. Introduces FenceProxySourceTypeHelper to provide conversion of fence proxy source from/to string Change-Id: I7ee90c85c80f80208025a7ccbb2170f4c3611e1a Bug-Url: https://bugzilla.redhat.com/1182510 Signed-off-by: Martin Perina <mper...@redhat.com> --- A backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/pm/FenceProxySourceType.java A backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/pm/FenceProxySourceTypeHelper.java A backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/pm/FenceProxySourceTypeHelperTest.java M frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml 4 files changed, 267 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/58/39758/1 diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/pm/FenceProxySourceType.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/pm/FenceProxySourceType.java new file mode 100644 index 0000000..70b000c --- /dev/null +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/pm/FenceProxySourceType.java @@ -0,0 +1,60 @@ +package org.ovirt.engine.core.common.businessentities.pm; + +/** + * Type of source which fence proxies to execute particular fence action are selected from + */ +public enum FenceProxySourceType { + /** + * Fence proxy is selected from the same cluster as fenced host + */ + CLUSTER("cluster"), + + /** + * Fence proxy is selected from the same data center as fenced host + */ + DC("dc"), + + /** + * Fence proxy is selected from a different data center than fenced host + */ + OTHER_DC("other_dc"); + + /** + * String representation of proxy source type + */ + private String value; + + FenceProxySourceType(String value) { + this.value = value; + } + /** + * Returns string representation of fence action + */ + public String getValue() { + return this.value; + } + + /** + * Tries to parse fence proxy source type from string + * + * @param value + * string representation of fence proxy source type + * @throws IllegalArgumentException + * if invalid value was specified + * @return parsed fence proxy source type + */ + public static FenceProxySourceType forValue(String value) { + if (value != null && value.length() > 0) { + String lowerCase = value.toLowerCase(); + if ("cluster".equals(lowerCase)) { + return CLUSTER; + } else if ("dc".equals(lowerCase)) { + return DC; + } else if ("other_dc".equals(lowerCase)) { + return OTHER_DC; + } + } + // TODO: Change to String.format() when this won't be needed in GWT + throw new IllegalArgumentException("Invalid value '" + value + "'"); + } +} diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/pm/FenceProxySourceTypeHelper.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/pm/FenceProxySourceTypeHelper.java new file mode 100644 index 0000000..ba5efec --- /dev/null +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/pm/FenceProxySourceTypeHelper.java @@ -0,0 +1,61 @@ +package org.ovirt.engine.core.common.utils.pm; + +import java.util.LinkedList; +import java.util.List; + +import org.ovirt.engine.core.common.businessentities.pm.FenceProxySourceType; + +/** + * Contains helper methods for {@code FenceProxySourceType} + */ +public class FenceProxySourceTypeHelper { + /** + * Parses the list of fence proxy source type from comma separated string. The resulting list is ordered + * in the same way as it's specified in the string + * + * @param stringValue + * comma separated string of fence proxy source types + * @throws IllegalArgumentException + * if invalid fence proxy source type string value was specified in {@code stringValue} + * @return list of parsed fence proxy source type + */ + public static List<FenceProxySourceType> parseFromString(String stringValue) { + List<FenceProxySourceType> parsedTypes = new LinkedList<>(); + if (stringValue != null && stringValue.length() > 0) { + String[] stringParts = stringValue.split(","); + for (String part : stringParts) { + parsedTypes.add(FenceProxySourceType.forValue(part)); + } + } + return parsedTypes; + } + + /** + * Saves list of fence proxy source type to comma separated string. Fence proxy source types in the resulting + * string are ordered in the same way as in the specified list + * + * @param fenceProxySourceTypes + * list of fence proxy source types + * @throws IllegalArgumentException + * if {@code null} value was contained is the specified list + * @return comma separated string of fence proxy source types + */ + public static String saveAsString(List<FenceProxySourceType> fenceProxySourceTypes) { + if (fenceProxySourceTypes == null || fenceProxySourceTypes.isEmpty()) { + return null; + } + + StringBuilder sb = new StringBuilder(); + for (FenceProxySourceType sourceType : fenceProxySourceTypes) { + if (sourceType == null) { + throw new IllegalArgumentException("Null value found in the specified list"); + } + + if (sb.length() > 0) { + sb.append(","); + } + sb.append(sourceType.getValue()); + } + return sb.toString(); + } +} diff --git a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/pm/FenceProxySourceTypeHelperTest.java b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/pm/FenceProxySourceTypeHelperTest.java new file mode 100644 index 0000000..2840f56 --- /dev/null +++ b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/pm/FenceProxySourceTypeHelperTest.java @@ -0,0 +1,142 @@ +package org.ovirt.engine.core.common.utils.pm; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; +import static org.ovirt.engine.core.common.businessentities.pm.FenceProxySourceType.CLUSTER; +import static org.ovirt.engine.core.common.businessentities.pm.FenceProxySourceType.DC; +import static org.ovirt.engine.core.common.businessentities.pm.FenceProxySourceType.OTHER_DC; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; +import org.ovirt.engine.core.common.businessentities.pm.FenceProxySourceType; + +public class FenceProxySourceTypeHelperTest { + /** + * Tests if empty list is returned when parsing {@code null} string + */ + @Test + public void parseNullString() { + List<FenceProxySourceType> result = FenceProxySourceTypeHelper.parseFromString(null); + + assertNotNull(result); + assertEquals(0, result.size()); + } + + /** + * Tests if empty list is returned when parsing empty string + */ + @Test + public void parseEmptyString() { + List<FenceProxySourceType> result = FenceProxySourceTypeHelper.parseFromString(null); + + assertNotNull(result); + assertEquals(0, result.size()); + } + + /** + * Tests parsing valid strings + */ + @Test + public void parseValidString() { + String[] validValues = { + CLUSTER.getValue(), + DC.getValue(), + OTHER_DC.getValue(), + CLUSTER.getValue() + "," + DC.getValue() + "," + OTHER_DC.getValue() + }; + + for (String invalidValue : validValues) { + FenceProxySourceTypeHelper.parseFromString(invalidValue); + } + } + + /** + * Tests if {@code IllegalArgumentException} is thrown when parsing invalid string + */ + @Test + public void parseInvalidString() { + String[] invalidValues = { + "clust", // invalid fence proxy source type + "clust,dc", // invalid fence proxy source type + "cluster,d", // invalid fence proxy source type + "cluster, dc" // space should not be used + }; + + for (String invalidValue : invalidValues) { + try { + FenceProxySourceTypeHelper.parseFromString(invalidValue); + fail(String.format( + "Value '%s' is not valid argument of FenceProxySourceTypeHelper.parseFromString.", + invalidValue)); + } catch (IllegalArgumentException ex) { + } + } + } + + /** + * Tests if null is returned when saving {@code null} list + */ + @Test + public void saveNullList() { + String result = FenceProxySourceTypeHelper.saveAsString(null); + + assertNull(result); + } + + /** + * Tests if null string is returned when saving empty list + */ + @Test + public void saveEmptyList() { + String result = FenceProxySourceTypeHelper.saveAsString(Collections.<FenceProxySourceType>emptyList()); + + assertNull(result); + } + + /** + * Tests saving lists with valid values + */ + @Test + public void saveListWithValidValues() { + List<List<FenceProxySourceType>> validLists = new ArrayList<>(); + validLists.add(Arrays.asList(CLUSTER)); + validLists.add(Arrays.asList(DC)); + validLists.add(Arrays.asList(OTHER_DC)); + validLists.add(Arrays.asList(CLUSTER, DC, OTHER_DC)); + + for (List<FenceProxySourceType> validList : validLists) { + FenceProxySourceTypeHelper.saveAsString(validList); + } + } + + /** + * Tests if {@code IllegalArgumentException} is thrown when saving a list containing invalid values + */ + @Test + public void saveListWithInvalidValues() { + List<List<FenceProxySourceType>> invalidLists = new ArrayList<>(); + + List<FenceProxySourceType> listWithNullValue = new ArrayList<>(); + listWithNullValue.add(null); + invalidLists.add(listWithNullValue); + + invalidLists.add(Arrays.asList(CLUSTER, null)); + + for (List<FenceProxySourceType> invalidList : invalidLists) { + try { + FenceProxySourceTypeHelper.saveAsString(invalidList); + fail(String.format( + "Value '%s' is not valid argument of FenceProxySourceTypeHelper.parseFromString.", + Arrays.toString(invalidList.toArray()))); + } catch (IllegalArgumentException ex) { + } + } + } + +} diff --git a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml index 70a95aa..4a39849 100644 --- a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml +++ b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml @@ -254,9 +254,13 @@ <include name="common/businessentities/Entities.java"/> <include name="common/utils/ToStringBuilder.java"/> + <!-- TODO: Remove when UI will be refactored to use FenceProxySourceType --> + <include name="common/utils/pm/FenceProxySourceTypeHelper.java"/> + <!-- Required by frontend --> <include name="common/interfaces/SearchType.java" /> <include name="common/businessentities/pm/FenceOperationResult.java" /> + <include name="common/businessentities/pm/FenceProxySourceType.java" /> <include name="common/businessentities/pm/PowerStatus.java" /> <include name="common/businessentities/AsyncTaskStatus.java" /> <include name="common/businessentities/AsyncTaskStatusEnum.java" /> -- To view, visit https://gerrit.ovirt.org/39758 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I7ee90c85c80f80208025a7ccbb2170f4c3611e1a Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Martin Peřina <mper...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches