-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…2569) * gh-2559 added hasTrait, its handler and corresponding tests * gh-2559 amended comment in HasTrait, added additional tests for the handler * gh-2559 null check & tidy test Co-authored-by: t92549 <80890692+t92549@users.noreply.github.com> Co-authored-by: p3430233 <61738524+p3430233@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
453 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
core/store/src/main/java/uk/gov/gchq/gaffer/store/operation/HasTrait.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, and is used | ||
* to check if the provided trait argument exists in either | ||
* the store default (false) or the schema current traits (true). | ||
* By default, it will check against the 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(); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
core/store/src/main/java/uk/gov/gchq/gaffer/store/operation/handler/HasTraitHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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 | ||
); | ||
if (null == traits) { | ||
return false; | ||
} | ||
return traits.contains(operation.getTrait()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/HasTraitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.