Skip to content

Commit

Permalink
Merge branch 'develop' into gh-2840-mapstore-matchedvertex-inconsistent
Browse files Browse the repository at this point in the history
  • Loading branch information
wb36499 authored Oct 22, 2024
2 parents c368c66 + c8f0732 commit 844ad48
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
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);
}
}
}
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);
}
}
}

0 comments on commit 844ad48

Please sign in to comment.