Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh-3324: Add OperationChainValidator to FedStore POC #3325

Merged
merged 8 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -351,6 +354,11 @@ protected OperationHandler<? extends OperationChain<?>> getOperationChainHandler
return new FederatedOperationHandler<>();
}

@Override
protected OperationChainValidator createOperationChainValidator() {
return new FederatedOperationChainValidator(new ViewValidator());
}

@Override
protected OutputOperationHandler<GetElements, Iterable<? extends Element>> getGetElementsHandler() {
return new FederatedOutputHandler<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Check warning on line 45 in store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidator.java

View check run for this annotation

Codecov / codecov/patch

store-implementation/simple-federated-store/src/main/java/uk/gov/gchq/gaffer/federated/simple/operation/FederatedOperationChainValidator.java#L44-L45

Added lines #L44 - L45 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading