Skip to content

Commit

Permalink
#38: implemented disable / enable capabilities in Capabilities.Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSergienko committed Mar 11, 2019
1 parent b0e184a commit 7131402
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 53 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.exasol</groupId>
<artifactId>virtual-schema-common-java</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
<name>Common module of Exasol Virtual Schemas Adapters</name>
<description>This is one of the modules of Virtual Schemas Adapters. The libraries provided by this project are the foundation of the adapter development, i.e. adapters must be implemented on top of them.</description>
<url>https://github.com/exasol/virtual-schema-common-java</url>
Expand Down
105 changes: 88 additions & 17 deletions src/main/java/com/exasol/adapter/capabilities/Capabilities.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.exasol.adapter.capabilities;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.Set;

Expand Down Expand Up @@ -66,6 +67,31 @@ public Set<AggregateFunctionCapability> getAggregateFunctionCapabilities() {
return this.aggregateFunctionCapabilities;
}

/**
* Removes unsupported capabilities
*
* @param capabilitiesToExclude unsupported capabilities
* @return supported capabilities
*/
public Capabilities subtractCapabilities(final Capabilities capabilitiesToExclude) {
final Builder builder = builder();
final Set<MainCapability> mainCapabilitiesWithExclusions = this.mainCapabilities;
mainCapabilitiesWithExclusions.removeAll(capabilitiesToExclude.getMainCapabilities());
final Set<LiteralCapability> literalCapabilitiesWithExclusions = this.literalCapabilities;
literalCapabilitiesWithExclusions.removeAll(capabilitiesToExclude.getLiteralCapabilities());
final Set<PredicateCapability> predicateCapabilitiesWithExclusions = this.predicateCapabilities;
predicateCapabilitiesWithExclusions.removeAll(capabilitiesToExclude.getPredicateCapabilities());
final Set<ScalarFunctionCapability> scalarCapabilitiesWithExclusions = this.scalarFunctionCapabilities;
scalarCapabilitiesWithExclusions.removeAll(capabilitiesToExclude.getScalarFunctionCapabilities());
final Set<AggregateFunctionCapability> aggregateCapabilitiesWithExclusions = this.aggregateFunctionCapabilities;
aggregateCapabilitiesWithExclusions.removeAll(capabilitiesToExclude.getAggregateFunctionCapabilities());

builder.addMain(mainCapabilitiesWithExclusions).addPredicate(predicateCapabilitiesWithExclusions)
.addLiteral(literalCapabilitiesWithExclusions).addScalarFunction(scalarCapabilitiesWithExclusions)
.addAggregateFunction(aggregateCapabilitiesWithExclusions);
return builder.build();
}

/**
* Get a {@link Capabilities} builder
*
Expand All @@ -83,8 +109,8 @@ public static final class Builder {
final Set<LiteralCapability> literalCapabilities = EnumSet.noneOf(LiteralCapability.class);
final Set<PredicateCapability> predicateCapabilities = EnumSet.noneOf(PredicateCapability.class);
final Set<ScalarFunctionCapability> scalarFunctionCapabilities = EnumSet.noneOf(ScalarFunctionCapability.class);
final Set<AggregateFunctionCapability> aggregateFunctionCapabilities = EnumSet
.noneOf(AggregateFunctionCapability.class);
final Set<AggregateFunctionCapability> aggregateFunctionCapabilities =
EnumSet.noneOf(AggregateFunctionCapability.class);

/**
* Create new capability instance
Expand All @@ -102,9 +128,18 @@ public Capabilities build() {
* @return builder instance for fluent programming
*/
public Builder addMain(final MainCapability... capabilities) {
for (final MainCapability capability : capabilities) {
this.mainCapabilities.add(capability);
}
this.mainCapabilities.addAll(Arrays.asList(capabilities));
return this;
}

/**
* Add one or more main capabilities
*
* @param capabilities capabilities to be added
* @return builder instance for fluent programming
*/
public Builder addMain(final Set<MainCapability> capabilities) {
this.mainCapabilities.addAll(capabilities);
return this;
}

Expand All @@ -115,9 +150,18 @@ public Builder addMain(final MainCapability... capabilities) {
* @return builder instance for fluent programming
*/
public Builder addLiteral(final LiteralCapability... capabilities) {
for (final LiteralCapability capability : capabilities) {
this.literalCapabilities.add(capability);
}
this.literalCapabilities.addAll(Arrays.asList(capabilities));
return this;
}

/**
* Add one or more literal capabilities
*
* @param capabilities capabilities to be added
* @return builder instance for fluent programming
*/
public Builder addLiteral(final Set<LiteralCapability> capabilities) {
this.literalCapabilities.addAll(capabilities);
return this;
}

Expand All @@ -128,9 +172,18 @@ public Builder addLiteral(final LiteralCapability... capabilities) {
* @return builder instance for fluent programming
*/
public Builder addPredicate(final PredicateCapability... capabilities) {
for (final PredicateCapability capability : capabilities) {
this.predicateCapabilities.add(capability);
}
this.predicateCapabilities.addAll(Arrays.asList(capabilities));
return this;
}

/**
* Add one or more predicate capabilities
*
* @param capabilities capabilities to be added
* @return builder instance for fluent programming
*/
public Builder addPredicate(final Set<PredicateCapability> capabilities) {
this.predicateCapabilities.addAll(capabilities);
return this;
}

Expand All @@ -141,9 +194,18 @@ public Builder addPredicate(final PredicateCapability... capabilities) {
* @return builder instance for fluent programming
*/
public Builder addScalarFunction(final ScalarFunctionCapability... capabilities) {
for (final ScalarFunctionCapability capability : capabilities) {
this.scalarFunctionCapabilities.add(capability);
}
this.scalarFunctionCapabilities.addAll(Arrays.asList(capabilities));
return this;
}

/**
* Add one or more scalar function capabilities
*
* @param capabilities capabilities to be added
* @return builder instance for fluent programming
*/
public Builder addScalarFunction(final Set<ScalarFunctionCapability> capabilities) {
this.scalarFunctionCapabilities.addAll(capabilities);
return this;
}

Expand All @@ -154,9 +216,18 @@ public Builder addScalarFunction(final ScalarFunctionCapability... capabilities)
* @return builder instance for fluent programming
*/
public Builder addAggregateFunction(final AggregateFunctionCapability... capabilities) {
for (final AggregateFunctionCapability capability : capabilities) {
this.aggregateFunctionCapabilities.add(capability);
}
this.aggregateFunctionCapabilities.addAll(Arrays.asList(capabilities));
return this;
}

/**
* Add one or more aggregate function capabilities
*
* @param capabilities capabilities to be added
* @return builder instance for fluent programming
*/
public Builder addAggregateFunction(final Set<AggregateFunctionCapability> capabilities) {
this.aggregateFunctionCapabilities.addAll(capabilities);
return this;
}
}
Expand Down
122 changes: 87 additions & 35 deletions src/test/java/com/exasol/adapter/capabilities/CapabilitiesTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.exasol.adapter.capabilities;

import static com.exasol.adapter.capabilities.CapabilityAssertions.*;
import static org.junit.jupiter.api.Assertions.assertAll;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.exasol.adapter.capabilities.CapabilityAssertions.*;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.junit.jupiter.api.Assertions.assertAll;

class CapabilitiesTest {
private Capabilities.Builder builder;

Expand All @@ -18,67 +21,116 @@ void beforeEach() {
void testCreateEmptyCapabilities() {
final Capabilities capabilities = this.builder.build();
assertAll(() -> assertEmptyMainCapabilities(capabilities), //
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
}

@Test
void buildWithMainCapabilities() {
final MainCapability[] expectedCapabilities = { MainCapability.AGGREGATE_GROUP_BY_COLUMN,
MainCapability.AGGREGATE_GROUP_BY_EXPRESSION };
final MainCapability[] expectedCapabilities =
{MainCapability.AGGREGATE_GROUP_BY_COLUMN, MainCapability.AGGREGATE_GROUP_BY_EXPRESSION};
final Capabilities capabilities = this.builder.addMain(expectedCapabilities).build();
assertAll(() -> assertCapabilitesContainAllOf(capabilities, expectedCapabilities), //
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
}

@Test
void buildWithLiteralCapabilities() {
final LiteralCapability[] expectedCapabilities = { LiteralCapability.BOOL, LiteralCapability.DATE };
final LiteralCapability[] expectedCapabilities = {LiteralCapability.BOOL, LiteralCapability.DATE};
final Capabilities capabilities = this.builder.addLiteral(expectedCapabilities).build();
assertAll(() -> assertEmptyMainCapabilities(capabilities), //
() -> assertCapabilitesContainAllOf(capabilities, expectedCapabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
() -> assertCapabilitesContainAllOf(capabilities, expectedCapabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
}

@Test
void buildWithPredicateCapabilities() {
final PredicateCapability[] expectedCapabilities = { PredicateCapability.AND, PredicateCapability.BETWEEN };
final PredicateCapability[] expectedCapabilities = {PredicateCapability.AND, PredicateCapability.BETWEEN};
final Capabilities capabilities = this.builder.addPredicate(expectedCapabilities).build();
assertAll(() -> assertEmptyMainCapabilities(capabilities), //
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertCapabilitesContainAllOf(capabilities, expectedCapabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertCapabilitesContainAllOf(capabilities, expectedCapabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
}

@Test
void buildWithScalarFunctionCapabilities() {
final ScalarFunctionCapability[] expectedCapabilities = { ScalarFunctionCapability.ABS,
ScalarFunctionCapability.ACOS };
final ScalarFunctionCapability[] expectedCapabilities =
{ScalarFunctionCapability.ABS, ScalarFunctionCapability.ACOS};
final Capabilities capabilities = this.builder.addScalarFunction(expectedCapabilities).build();
assertAll(() -> assertEmptyMainCapabilities(capabilities), //
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertCapabilitesContainAllOf(capabilities, expectedCapabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertCapabilitesContainAllOf(capabilities, expectedCapabilities), //
() -> assertEmptyAggregateFunctionCapatilities(capabilities));
}

@Test
void buildWithAggregateFunctionCapabilities() {
final AggregateFunctionCapability[] expectedCapabilities = {
AggregateFunctionCapability.APPROXIMATE_COUNT_DISTINCT, AggregateFunctionCapability.AVG };
final AggregateFunctionCapability[] expectedCapabilities =
{AggregateFunctionCapability.APPROXIMATE_COUNT_DISTINCT, AggregateFunctionCapability.AVG};
final Capabilities capabilities = this.builder.addAggregateFunction(expectedCapabilities).build();
assertAll(() -> assertEmptyMainCapabilities(capabilities), //
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertCapabilitesContainAllOf(capabilities, expectedCapabilities));
() -> assertEmptyLiteralCapabilities(capabilities), //
() -> assertEmptyPredicateCapabilities(capabilities), //
() -> assertEmptyScalarFunctionCapabilities(capabilities), //
() -> assertCapabilitesContainAllOf(capabilities, expectedCapabilities));
}

@Test
void substractCapabilities() {
final MainCapability[] mainCapabilities =
{MainCapability.AGGREGATE_GROUP_BY_COLUMN, MainCapability.AGGREGATE_GROUP_BY_EXPRESSION};
final LiteralCapability[] literalCapabilities = {LiteralCapability.DATE, LiteralCapability.DOUBLE};
final PredicateCapability[] predicateCapabilities = {PredicateCapability.EQUAL, PredicateCapability.BETWEEN};
final ScalarFunctionCapability[] scalarFunctionCapabilities =
{ScalarFunctionCapability.ADD, ScalarFunctionCapability.ABS};
final AggregateFunctionCapability[] aggregateFunctionCapabilities =
{AggregateFunctionCapability.APPROXIMATE_COUNT_DISTINCT, AggregateFunctionCapability.AVG};
final Capabilities capabilities =
this.builder.addMain(mainCapabilities).addLiteral(literalCapabilities).addPredicate(predicateCapabilities)
.addScalarFunction(scalarFunctionCapabilities).addAggregateFunction(aggregateFunctionCapabilities)
.build();

this.builder = Capabilities.builder();
final MainCapability[] mainCapabilitiesToExclude = {MainCapability.AGGREGATE_GROUP_BY_COLUMN};
final LiteralCapability[] literalCapabilitiesToExclude = {LiteralCapability.DATE};
final PredicateCapability[] predicateCapabilitiesToExclude = {PredicateCapability.EQUAL};
final ScalarFunctionCapability[] scalarFunctionCapabilitiesToExclude = {ScalarFunctionCapability.ADD};
final AggregateFunctionCapability[] aggregateFunctionCapabilitiesToExclude =
{AggregateFunctionCapability.APPROXIMATE_COUNT_DISTINCT};
final Capabilities capabilitiesToExclude =
this.builder.addMain(mainCapabilitiesToExclude).addLiteral(literalCapabilitiesToExclude)
.addPredicate(predicateCapabilitiesToExclude).addScalarFunction(scalarFunctionCapabilitiesToExclude)
.addAggregateFunction(aggregateFunctionCapabilitiesToExclude).build();

final Capabilities capabilitiesWithExclusion = capabilities.subtractCapabilities(capabilitiesToExclude);
assertAll(() -> assertThat(capabilitiesWithExclusion.getMainCapabilities(),
contains(MainCapability.AGGREGATE_GROUP_BY_EXPRESSION)),
() -> assertThat(capabilitiesWithExclusion.getMainCapabilities(),
not(contains(MainCapability.AGGREGATE_GROUP_BY_COLUMN))),
() -> assertThat(capabilitiesWithExclusion.getLiteralCapabilities(), contains(LiteralCapability.DOUBLE)),
() -> assertThat(capabilitiesWithExclusion.getLiteralCapabilities(),
not(contains(LiteralCapability.DATE))),
() -> assertThat(capabilitiesWithExclusion.getPredicateCapabilities(),
contains(PredicateCapability.BETWEEN)),
() -> assertThat(capabilitiesWithExclusion.getPredicateCapabilities(),
not(contains(PredicateCapability.EQUAL))),
() -> assertThat(capabilitiesWithExclusion.getScalarFunctionCapabilities(),
contains(ScalarFunctionCapability.ABS)),
() -> assertThat(capabilitiesWithExclusion.getScalarFunctionCapabilities(),
not(contains(ScalarFunctionCapability.ADD))),
() -> assertThat(capabilitiesWithExclusion.getAggregateFunctionCapabilities(),
contains(AggregateFunctionCapability.AVG)),
() -> assertThat(capabilitiesWithExclusion.getAggregateFunctionCapabilities(),
not(contains(AggregateFunctionCapability.APPROXIMATE_COUNT_DISTINCT))));
}
}

0 comments on commit 7131402

Please sign in to comment.