Skip to content

Commit

Permalink
gh-2559 added hasTrait, its handler and corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
p3430233 committed Jan 11, 2022
1 parent 296c453 commit 8e95605
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 26 deletions.
30 changes: 4 additions & 26 deletions core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,36 +95,12 @@
import uk.gov.gchq.gaffer.serialisation.Serialiser;
import uk.gov.gchq.gaffer.store.library.GraphLibrary;
import uk.gov.gchq.gaffer.store.library.NoGraphLibrary;
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.OperationUtil;
import uk.gov.gchq.gaffer.store.operation.*;
import uk.gov.gchq.gaffer.store.operation.add.AddSchemaToLibrary;
import uk.gov.gchq.gaffer.store.operation.add.AddStorePropertiesToLibrary;
import uk.gov.gchq.gaffer.store.operation.declaration.OperationDeclaration;
import uk.gov.gchq.gaffer.store.operation.declaration.OperationDeclarations;
import uk.gov.gchq.gaffer.store.operation.handler.AddSchemaToLibraryHandler;
import uk.gov.gchq.gaffer.store.operation.handler.AddStorePropertiesToLibraryHandler;
import uk.gov.gchq.gaffer.store.operation.handler.CountGroupsHandler;
import uk.gov.gchq.gaffer.store.operation.handler.CountHandler;
import uk.gov.gchq.gaffer.store.operation.handler.DiscardOutputHandler;
import uk.gov.gchq.gaffer.store.operation.handler.ForEachHandler;
import uk.gov.gchq.gaffer.store.operation.handler.GetSchemaHandler;
import uk.gov.gchq.gaffer.store.operation.handler.GetTraitsHandler;
import uk.gov.gchq.gaffer.store.operation.handler.GetVariableHandler;
import uk.gov.gchq.gaffer.store.operation.handler.GetVariablesHandler;
import uk.gov.gchq.gaffer.store.operation.handler.GetWalksHandler;
import uk.gov.gchq.gaffer.store.operation.handler.IfHandler;
import uk.gov.gchq.gaffer.store.operation.handler.LimitHandler;
import uk.gov.gchq.gaffer.store.operation.handler.MapHandler;
import uk.gov.gchq.gaffer.store.operation.handler.OperationChainHandler;
import uk.gov.gchq.gaffer.store.operation.handler.OperationHandler;
import uk.gov.gchq.gaffer.store.operation.handler.OutputOperationHandler;
import uk.gov.gchq.gaffer.store.operation.handler.ReduceHandler;
import uk.gov.gchq.gaffer.store.operation.handler.SetVariableHandler;
import uk.gov.gchq.gaffer.store.operation.handler.ValidateHandler;
import uk.gov.gchq.gaffer.store.operation.handler.ValidateOperationChainHandler;
import uk.gov.gchq.gaffer.store.operation.handler.WhileHandler;
import uk.gov.gchq.gaffer.store.operation.handler.*;
import uk.gov.gchq.gaffer.store.operation.handler.compare.MaxHandler;
import uk.gov.gchq.gaffer.store.operation.handler.compare.MinHandler;
import uk.gov.gchq.gaffer.store.operation.handler.compare.SortHandler;
Expand Down Expand Up @@ -1072,6 +1048,8 @@ private void addCoreOpHandlers() {
addOperationHandler(AddStorePropertiesToLibrary.class, new AddStorePropertiesToLibraryHandler());
}

// Traits
addOperationHandler(HasTrait.class, new HasTraitHandler());
addOperationHandler(GetTraits.class, new GetTraitsHandler());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2022 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.store.operation;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.exception.CloneFailedException;

import uk.gov.gchq.gaffer.operation.Operation;
import uk.gov.gchq.gaffer.operation.io.Output;
import uk.gov.gchq.gaffer.operation.serialisation.TypeReferenceImpl;
import uk.gov.gchq.gaffer.store.StoreTrait;
import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;

import java.util.HashMap;
import java.util.Map;

/**
* An Operation that will see if a Store has a given trait.
*/
@JsonPropertyOrder(alphabetic = true)
@Since("2.0.0")
@Summary("An Operation that will see if a Store has a given trait")
public class HasTrait implements Operation, Output<Boolean> {
public static final boolean DEFAULT_CURRENT_TRAITS = true;

/**
* The currentTraits holds a boolean value, which if false
* will return a list of all supported traits from the store.
* If true, it will return a list of current traits.
* By default it will return a list of current traits.
*/
private boolean currentTraits = DEFAULT_CURRENT_TRAITS;
private StoreTrait trait;
private Map<String, String> options = new HashMap<>();

@Override
public HasTrait shallowClone() throws CloneFailedException {
return new Builder()
.options(options)
.currentTraits(currentTraits)
.trait(trait)
.build();
}

public boolean isCurrentTraits() {
return currentTraits;
}

public void setCurrentTraits(final boolean currentTraits) {
this.currentTraits = currentTraits;
}

public StoreTrait getTrait() {
return trait;
}

public void setTrait(final StoreTrait trait) {
this.trait = trait;
}

@Override
public Map<String, String> getOptions() {
return options;
}

@Override
public void setOptions(final Map<String, String> options) {
this.options = options;
}

@Override
public TypeReference<Boolean> getOutputTypeReference() {
return new TypeReferenceImpl.Boolean();
}

public static class Builder extends BaseBuilder<HasTrait, Builder> {
public Builder() {
super(new HasTrait());
}

public Builder currentTraits(final boolean currentTraits) {
_getOp().setCurrentTraits(currentTraits);
return _self();
}

public Builder trait(final StoreTrait trait) {
_getOp().setTrait(trait);
return _self();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 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.store.operation.handler;

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.StoreTrait;
import uk.gov.gchq.gaffer.store.operation.GetTraits;
import uk.gov.gchq.gaffer.store.operation.HasTrait;

import java.util.Set;


public class HasTraitHandler implements OutputOperationHandler<HasTrait, Boolean> {

@Override
public Boolean doOperation(final HasTrait operation, final Context context, final Store store) throws OperationException {
final Set<StoreTrait> traits = store.execute(new GetTraits.Builder()
.currentTraits(operation.isCurrentTraits())
.options(operation.getOptions())
.build(), context
);
return null != traits && traits.contains(operation.getTrait());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2022 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.store.operation;

import org.junit.jupiter.api.Test;

import uk.gov.gchq.gaffer.operation.OperationTest;
import uk.gov.gchq.gaffer.store.operation.HasTrait.Builder;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static uk.gov.gchq.gaffer.store.StoreTrait.VISIBILITY;

public class HasTraitTest extends OperationTest<HasTrait> {

@Override
protected HasTrait getTestObject() {
return new HasTrait();
}

@Test
@Override
public void builderShouldCreatePopulatedOperation() {
HasTrait op = new Builder()
.currentTraits(false)
.trait(VISIBILITY)
.build();

assertEquals(false, op.isCurrentTraits());
assertEquals(VISIBILITY, op.getTrait());
}

@Test
@Override
public void shouldShallowCloneOperation() {
HasTrait op = new Builder()
.currentTraits(false)
.trait(VISIBILITY)
.build();

HasTrait clone = op.shallowClone();

assertEquals(op.isCurrentTraits(), clone.isCurrentTraits());
assertEquals(op.getTrait(), clone.getTrait());
}

@Test
@Override
public void shouldJsonSerialiseAndDeserialise() {
// Given
final HasTrait obj = new HasTrait.Builder()
.currentTraits(true)
.trait(VISIBILITY)
.build();

// When
final byte[] json = toJson(obj);
final HasTrait deserialisedObj = fromJson(json);

// Then
assertEquals(obj.isCurrentTraits(), deserialisedObj.isCurrentTraits());
assertEquals(obj.getTrait(), deserialisedObj.getTrait());
}
}
Loading

0 comments on commit 8e95605

Please sign in to comment.