+++ /dev/null
-/*
- * 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<String> usedParameters,
- final @NonNull String... allowedParameters) {
- final Set<String> 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<String> 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);
- }
- }
-}
*/
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;
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;
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;
}
// 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,
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);
}
}
+
+ /**
+ * 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<String> 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<String> usedParameters,
+ final @NonNull String... allowedParameters) {
+ // FIXME: there should be a speedier way to do this
+ final Set<String> 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;
+++ /dev/null
-/*
- * 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
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;
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;
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());
+ }
}