From 0c0f216cc41e5078bb728c39c77dcdb99cdde52f Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 14 Oct 2020 14:40:10 +0200 Subject: [PATCH] Eliminate ParametersUtil Methods here are only used by ReadDataTransactionUtil, rehost them to eliminate some of the spaghetti. Change-Id: I8f1aa463c5b01c9225204c030bf4ac12a56df4a1 Signed-off-by: Robert Varga --- .../rfc8040/rests/utils/ParametersUtil.java | 65 --------------- .../rests/utils/ReadDataTransactionUtil.java | 55 +++++++++++-- .../rests/utils/ParametersUtilTest.java | 80 ------------------- .../utils/ReadDataTransactionUtilTest.java | 49 ++++++++++++ 4 files changed, 98 insertions(+), 151 deletions(-) delete mode 100644 restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ParametersUtil.java delete mode 100644 restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ParametersUtilTest.java diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ParametersUtil.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ParametersUtil.java deleted file mode 100644 index df56c23ecb..0000000000 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ParametersUtil.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.restconf.nb.rfc8040.rests.utils; - -import com.google.common.collect.Sets; -import java.util.List; -import java.util.Set; -import org.eclipse.jdt.annotation.NonNull; -import org.opendaylight.restconf.common.errors.RestconfDocumentedException; -import org.opendaylight.restconf.common.errors.RestconfError; -import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag; -import org.opendaylight.restconf.common.errors.RestconfError.ErrorType; - -final class ParametersUtil { - - private ParametersUtil() { - throw new UnsupportedOperationException("Util class."); - } - - /** - * Check if URI does not contain not allowed parameters for specified operation. - * - * @param operationType - * type of operation (READ, POST, PUT, DELETE...) - * @param usedParameters - * parameters used in URI request - * @param allowedParameters - * allowed parameters for operation - */ - static void checkParametersTypes(final @NonNull String operationType, - final @NonNull Set usedParameters, - final @NonNull String... allowedParameters) { - final Set notAllowedParameters = Sets.newHashSet(usedParameters); - notAllowedParameters.removeAll(Sets.newHashSet(allowedParameters)); - - if (!notAllowedParameters.isEmpty()) { - throw new RestconfDocumentedException( - "Not allowed parameters for " + operationType + " operation: " + notAllowedParameters, - RestconfError.ErrorType.PROTOCOL, - RestconfError.ErrorTag.INVALID_VALUE); - } - } - - /** - * Check if URI does not contain value for the same parameter more than once. - * - * @param parameterValues - * URI parameter values - * @param parameterName - * URI parameter name - */ - static void checkParameterCount(final @NonNull List parameterValues, final @NonNull String parameterName) { - if (parameterValues.size() > 1) { - throw new RestconfDocumentedException( - "Parameter " + parameterName + " can appear at most once in request URI", - ErrorType.PROTOCOL, - ErrorTag.INVALID_VALUE); - } - } -} diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java index a08be258c7..804412ceb2 100644 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java @@ -7,6 +7,8 @@ */ package org.opendaylight.restconf.nb.rfc8040.rests.utils; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.Sets; import com.google.common.primitives.Ints; import com.google.common.util.concurrent.ListenableFuture; import java.util.Collection; @@ -14,6 +16,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; import javax.ws.rs.core.UriInfo; @@ -26,6 +29,8 @@ import org.opendaylight.restconf.common.context.WriterParameters; import org.opendaylight.restconf.common.context.WriterParameters.WriterParametersBuilder; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.common.errors.RestconfError; +import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag; +import org.opendaylight.restconf.common.errors.RestconfError.ErrorType; import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy; import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserFieldsParameter; import org.opendaylight.yangtools.yang.common.QName; @@ -95,8 +100,7 @@ public final class ReadDataTransactionUtil { } // check only allowed parameters - ParametersUtil.checkParametersTypes( - RestconfDataServiceConstant.ReadData.READ_TYPE_TX, + checkParametersTypes(RestconfDataServiceConstant.ReadData.READ_TYPE_TX, uriInfo.getQueryParameters().keySet(), RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH, @@ -118,10 +122,10 @@ public final class ReadDataTransactionUtil { Collections.emptyList()); // parameter can be in URI at most once - ParametersUtil.checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT); - ParametersUtil.checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH); - ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS); - ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS); + checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT); + checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH); + checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS); + checkParameterCount(fields, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS); // check and set content final String contentValue = content.get(0); @@ -223,6 +227,45 @@ public final class ReadDataTransactionUtil { } } + + /** + * Check if URI does not contain value for the same parameter more than once. + * + * @param parameterValues URI parameter values + * @param parameterName URI parameter name + */ + @VisibleForTesting + static void checkParameterCount(final @NonNull List parameterValues, final @NonNull String parameterName) { + if (parameterValues.size() > 1) { + throw new RestconfDocumentedException( + "Parameter " + parameterName + " can appear at most once in request URI", + ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); + } + } + + /** + * Check if URI does not contain not allowed parameters for specified operation. + * + * @param operationType type of operation (READ, POST, PUT, DELETE...) + * @param usedParameters parameters used in URI request + * @param allowedParameters allowed parameters for operation + */ + @VisibleForTesting + static void checkParametersTypes(final @NonNull String operationType, + final @NonNull Set usedParameters, + final @NonNull String... allowedParameters) { + // FIXME: there should be a speedier way to do this + final Set notAllowedParameters = Sets.newHashSet(usedParameters); + notAllowedParameters.removeAll(Sets.newHashSet(allowedParameters)); + + if (!notAllowedParameters.isEmpty()) { + throw new RestconfDocumentedException( + "Not allowed parameters for " + operationType + " operation: " + notAllowedParameters, + RestconfError.ErrorType.PROTOCOL, + RestconfError.ErrorTag.INVALID_VALUE); + } + } + private static NormalizedNode prepareDataByParamWithDef(final NormalizedNode result, final YangInstanceIdentifier path, final String withDefa, final SchemaContext ctx) { boolean trim; diff --git a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ParametersUtilTest.java b/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ParametersUtilTest.java deleted file mode 100644 index 78826431fe..0000000000 --- a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ParametersUtilTest.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.restconf.nb.rfc8040.rests.utils; - -import static org.junit.Assert.assertEquals; - -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; -import org.junit.Assert; -import org.junit.Test; -import org.opendaylight.restconf.common.errors.RestconfDocumentedException; -import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag; -import org.opendaylight.restconf.common.errors.RestconfError.ErrorType; - -/** - * Unit test for {@link ParametersUtil}. - */ -public class ParametersUtilTest { - - /** - * Test when all parameters are allowed. - */ - @Test - public void checkParametersTypesTest() { - ParametersUtil.checkParametersTypes( - RestconfDataServiceConstant.ReadData.READ_TYPE_TX, - Sets.newHashSet("content"), - RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH); - } - - /** - * Test when not allowed parameter type is used. - */ - @Test - public void checkParametersTypesNegativeTest() { - try { - ParametersUtil.checkParametersTypes( - RestconfDataServiceConstant.ReadData.READ_TYPE_TX, - Sets.newHashSet("not-allowed-parameter"), - RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH); - - Assert.fail("Test expected to fail due to not allowed parameter used with operation"); - } catch (final RestconfDocumentedException e) { - assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType()); - assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag()); - assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode()); - } - } - - /** - * Test when parameter is present at most once. - */ - @Test - public void checkParameterCountTest() { - ParametersUtil.checkParameterCount(Lists.newArrayList("all"), RestconfDataServiceConstant.ReadData.CONTENT); - } - - /** - * Test when parameter is present more than once. - */ - @Test - public void checkParameterCountNegativeTest() { - try { - ParametersUtil.checkParameterCount(Lists.newArrayList("config", "nonconfig", "all"), - RestconfDataServiceConstant.ReadData.CONTENT); - - Assert.fail("Test expected to fail due to multiple values of the same parameter"); - } catch (final RestconfDocumentedException e) { - assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType()); - assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag()); - assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode()); - } - } -} \ No newline at end of file diff --git a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java b/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java index 11001be0b7..88b80fd4ed 100644 --- a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java +++ b/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java @@ -11,6 +11,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; @@ -19,7 +20,9 @@ import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediate import com.google.common.collect.ImmutableList; import java.util.Collections; +import java.util.List; import java.util.Optional; +import java.util.Set; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.UriInfo; import org.junit.Before; @@ -572,4 +575,50 @@ public class ReadDataTransactionUtilTest { assertNull(writerParameters.getWithDefault()); assertFalse(writerParameters.isTagged()); } + + /** + * Test when parameter is present at most once. + */ + @Test + public void checkParameterCountTest() { + ReadDataTransactionUtil.checkParameterCount(List.of("all"), RestconfDataServiceConstant.ReadData.CONTENT); + } + + /** + * Test when parameter is present more than once. + */ + @Test + public void checkParameterCountNegativeTest() { + final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, + () -> ReadDataTransactionUtil.checkParameterCount(List.of("config", "nonconfig", "all"), + RestconfDataServiceConstant.ReadData.CONTENT)); + assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType()); + assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag()); + assertEquals("Error status code is not correct", 400, ex.getErrors().get(0).getErrorTag().getStatusCode()); + } + + + /** + * Test when all parameters are allowed. + */ + @Test + public void checkParametersTypesTest() { + ReadDataTransactionUtil.checkParametersTypes(RestconfDataServiceConstant.ReadData.READ_TYPE_TX, + Set.of("content"), + RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH); + } + + /** + * Test when not allowed parameter type is used. + */ + @Test + public void checkParametersTypesNegativeTest() { + final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, + () -> ReadDataTransactionUtil.checkParametersTypes(RestconfDataServiceConstant.ReadData.READ_TYPE_TX, + Set.of("not-allowed-parameter"), + RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH)); + assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType()); + assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag()); + assertEquals("Error status code is not correct", 400, ex.getErrors().get(0).getErrorTag().getStatusCode()); + } } -- 2.36.6