From c8f07326751a8172b2c6cabbfcd79d11ddf98d8f Mon Sep 17 00:00:00 2001 From: cn337131 <141730190+cn337131@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:09:31 +0000 Subject: [PATCH] Gh-3324: Add OperationChainValidator to FedStore POC (#3325) * add validator * add test * checkstyle * copyright * javadoc * fix * exception test --------- Co-authored-by: tb06904 <141412860+tb06904@users.noreply.github.com> --- .../federated/simple/FederatedStore.java | 8 ++ .../FederatedOperationChainValidator.java | 48 ++++++++++++ .../FederatedOperationChainValidatorTest.java | 73 +++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidator.java create mode 100644 store-implementation/simple-federated-store/src/test/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidatorTest.java diff --git a/store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/FederatedStore.java b/store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/FederatedStore.java index 6c2c0b0fb38..a8fac9d5d5b 100644 --- a/store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/FederatedStore.java +++ b/store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/FederatedStore.java @@ -33,6 +33,7 @@ import uk.gov.gchq.gaffer.federated.simple.access.GraphAccess; import uk.gov.gchq.gaffer.federated.simple.operation.AddGraph; import uk.gov.gchq.gaffer.federated.simple.operation.ChangeGraphId; +import uk.gov.gchq.gaffer.federated.simple.operation.FederatedOperationChainValidator; import uk.gov.gchq.gaffer.federated.simple.operation.GetAllGraphIds; import uk.gov.gchq.gaffer.federated.simple.operation.GetAllGraphInfo; import uk.gov.gchq.gaffer.federated.simple.operation.RemoveGraph; @@ -63,9 +64,11 @@ import uk.gov.gchq.gaffer.store.operation.DeleteAllData; import uk.gov.gchq.gaffer.store.operation.GetSchema; import uk.gov.gchq.gaffer.store.operation.GetTraits; +import uk.gov.gchq.gaffer.store.operation.OperationChainValidator; import uk.gov.gchq.gaffer.store.operation.handler.OperationHandler; import uk.gov.gchq.gaffer.store.operation.handler.OutputOperationHandler; import uk.gov.gchq.gaffer.store.schema.Schema; +import uk.gov.gchq.gaffer.store.schema.ViewValidator; import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; @@ -351,6 +354,11 @@ protected OperationHandler> getOperationChainHandler return new FederatedOperationHandler<>(); } + @Override + protected OperationChainValidator createOperationChainValidator() { + return new FederatedOperationChainValidator(new ViewValidator()); + } + @Override protected OutputOperationHandler> getGetElementsHandler() { return new FederatedOutputHandler<>(); diff --git a/store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidator.java b/store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidator.java new file mode 100644 index 00000000000..e2fc8e339c7 --- /dev/null +++ b/store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidator.java @@ -0,0 +1,48 @@ +/* + * Copyright 2024 Crown Copyright + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.gaffer.federated.simple.operation; + +import uk.gov.gchq.gaffer.core.exception.GafferRuntimeException; +import uk.gov.gchq.gaffer.operation.Operation; +import uk.gov.gchq.gaffer.operation.OperationException; +import uk.gov.gchq.gaffer.store.Context; +import uk.gov.gchq.gaffer.store.Store; +import uk.gov.gchq.gaffer.store.operation.GetSchema; +import uk.gov.gchq.gaffer.store.operation.OperationChainValidator; +import uk.gov.gchq.gaffer.store.schema.Schema; +import uk.gov.gchq.gaffer.store.schema.ViewValidator; +import uk.gov.gchq.gaffer.user.User; + +/** + * Extends {@link OperationChainValidator} and uses the FederatedStore to get + * the merged schema based on the operation options. + */ +public class FederatedOperationChainValidator extends OperationChainValidator { + + public FederatedOperationChainValidator(final ViewValidator viewValidator) { + super(viewValidator); + } + + @Override + protected Schema getSchema(final Operation operation, final User user, final Store store) { + try { + return store.execute(new GetSchema.Builder().options(operation.getOptions()).build(), new Context(user)); + } catch (final OperationException e) { + throw new GafferRuntimeException("Unable to get merged schema for graph " + store.getGraphId(), e); + } + } +} diff --git a/store-implementation/simple-federated-store/src/test/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidatorTest.java b/store-implementation/simple-federated-store/src/test/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidatorTest.java new file mode 100644 index 00000000000..54b783d9d6a --- /dev/null +++ b/store-implementation/simple-federated-store/src/test/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidatorTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2024 Crown Copyright + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.gaffer.federated.simple.operation; + +import org.junit.jupiter.api.Test; + +import uk.gov.gchq.gaffer.core.exception.GafferRuntimeException; +import uk.gov.gchq.gaffer.federated.simple.FederatedStore; +import uk.gov.gchq.gaffer.operation.Operation; +import uk.gov.gchq.gaffer.operation.OperationException; +import uk.gov.gchq.gaffer.store.Context; +import uk.gov.gchq.gaffer.store.operation.GetSchema; +import uk.gov.gchq.gaffer.store.schema.Schema; +import uk.gov.gchq.gaffer.store.schema.ViewValidator; +import uk.gov.gchq.gaffer.user.User; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class FederatedOperationChainValidatorTest { + final ViewValidator viewValidator = mock(ViewValidator.class); + final FederatedOperationChainValidator validator = new FederatedOperationChainValidator(viewValidator); + final FederatedStore store = mock(FederatedStore.class); + final User user = mock(User.class); + final Operation op = mock(Operation.class); + final Schema schema = mock(Schema.class); + + @Test + void shouldGetMergedSchema() throws OperationException { + // Given + when(store.execute(any(GetSchema.class), any(Context.class))).thenReturn(schema); + + // When + final Schema result = validator.getSchema(op, user, store); + + verify(store).execute(any(GetSchema.class), any(Context.class)); + // Then + assertThat(result).isEqualTo(schema); + } + + @Test + void shouldThrowException() throws OperationException { + // Given + when(store.execute(any(GetSchema.class), any(Context.class))).thenThrow(GafferRuntimeException.class); + + // When/Then + try { + validator.getSchema(op, user, store); + fail("Exception expected"); + } catch (final Exception e) { + verify(store).execute(any(GetSchema.class), any(Context.class)); + assertThat(e).isInstanceOf(GafferRuntimeException.class); + } + } +}