Skip to content

Commit

Permalink
Merge pull request #180 from eclipse-passage/561435-2
Browse files Browse the repository at this point in the history
561435 API revision: requirements resolution: test coverage
  • Loading branch information
eparovyshnaya authored Apr 5, 2020
2 parents ea76aaa + 2cf1033 commit ca94996
Show file tree
Hide file tree
Showing 18 changed files with 399 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
package org.eclipse.passage.lic.internal.api.requirements;

/**
* Describes a <i> feature under licensing</i> in context of access cycle
* <p>
* Describes a <i> feature under licensing</i> in context of access cycle.
* </p>
* <p>
* Intended to be implemented as a <i>data-class</i>.
* </p>
*
* @see Requirement
* @see org.eclipse.passage.lic.api
Expand All @@ -35,6 +40,9 @@ public interface Feature {
*/
String identifier();

/**
* Freely named provider of the feature
*/
String provider();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
import java.util.Objects;

/**
* <p>
* Severity of a licensing requirement and, accordingly, restriction verdict.
* </p>
* <p>
* Designed to be a <i>data-class</i>.
* </p>
*/
public abstract class RestrictionLevel {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.eclipse.passage.lic.internal.base;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

Expand All @@ -15,6 +16,7 @@ public abstract class BaseNamedData<T> implements NamedData<T> {
private final Function<String, T> retrieve;

protected BaseNamedData(Function<String, T> retrieve) {
Objects.requireNonNull(retrieve, "Retriever cannot be null"); //$NON-NLS-1$
this.retrieve = retrieve;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.passage.lic.internal.base;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
Expand All @@ -32,7 +33,9 @@ protected StringNamedData(Function<String, String> retrieve) {
}

protected StringNamedData(Map<String, Object> container) {
super(key -> String.valueOf(container.get(key)));
super(key -> Optional.ofNullable(container.get(key))//
.map(String::valueOf) //
.orElse(null));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.eclipse.passage.lic.internal.api.requirements.Feature;

/**
* Base <i>data driven</i> implementation of a {@linkplain Feature} descriptor
* Base <i>data driven</i> implementation of a {@linkplain Feature} descriptor.
*/
@SuppressWarnings("restriction")
public final class BaseFeature implements Feature {
Expand Down Expand Up @@ -58,6 +58,23 @@ public String provider() {
return provider;
}

@Override
public boolean equals(Object another) {
if (!getClass().isInstance(another)) {
return false;
}
Feature feature = (Feature) another;
return id.equals(feature.identifier()) //
&& name.equals(feature.name()) //
&& version.equals(feature.version()) //
&& provider.equals(feature.provider()); //
}

@Override
public int hashCode() {
return Objects.hash(name, version, id, provider);
}

@Override
public String toString() {
return "BaseFeature [id=" + id + // //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.eclipse.passage.lic.internal.api.requirements.Requirement;
import org.eclipse.passage.lic.internal.api.restrictions.RestrictionLevel;

/**
* Base <i>data-driven</i> implementation of a {@linkplain Requirement}.
*/
@SuppressWarnings("restriction")
public final class BaseRequirement implements Requirement {

Expand Down Expand Up @@ -50,4 +53,27 @@ public Object source() {
return source;
}

@Override
public boolean equals(Object another) {
if (!getClass().isInstance(another)) {
return false;
}
Requirement requirement = (Requirement) another;
return feature.equals(requirement.feature()) //
&& restriction.equals(requirement.restrictionLevel()) //
&& source.equals(requirement.source());
}

@Override
public int hashCode() {
return Objects.hash(feature, restriction, source);
}

@Override
public String toString() {
return "BaseRequirement [feature=" + feature + // //$NON-NLS-1$
", restriction=" + restriction + // //$NON-NLS-1$
", source=" + source + "]"; //$NON-NLS-1$ //$NON-NLS-2$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
@SuppressWarnings("restriction")
public final class CapabilityLicFeatureProvider extends StringNamedData {

public CapabilityLicFeatureProvider(String version) {
super(version);
public CapabilityLicFeatureProvider(String provider) {
super(provider);
}

public CapabilityLicFeatureProvider(Map<String, Object> container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private Requirement requirementFromAttributes(String feature, Map<String, Object
BaseRequirement requirement = new BaseRequirement(//
new BaseFeature(feature, version, name, provider), //
level, //
capability.getResource());
capability.getResource().getBundle().getSymbolicName());
return requirement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bundle-SymbolicName: org.eclipse.passage.lic.equinox.tests.data.requirements;sin
Bundle-Version: 0.1.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Name: Data for Passage LIC Equinox requirements tests
Bundle-Vendor: Eclipse Passage
Bundle-Vendor: Eclipse Passage
Bundle-Copyright: %Bundle-Copyright
Provide-Capability: licensing.feature;licensing.feature="PI";licensing.verion="3.14.15",
licensing.feature;licensing.feature="E";licensing.verion="2.71.82"
Provide-Capability: licensing.feature;licensing.feature="PI";version="3.14.15";name="PI of version PI";level="error",
licensing.feature;licensing.feature="E";version="2.71.82";name="Euler number";level="info";provider="Euler"
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.equinox.requirements;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.passage.lic.internal.api.requirements.Requirement;
import org.eclipse.passage.lic.internal.api.requirements.ResolvedRequirements;
import org.junit.Ignore;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
Expand All @@ -34,9 +39,22 @@ public void providedAsResolvedRequirementsImpl() throws InvalidSyntaxException {
}

@Test
@Ignore
public void allRequirements() throws InvalidSyntaxException {
Collection<Requirement> list = service().all();
assertTrue(list.size() >= 2); // at least two are declared in our test data bundle
assertTrue(list.stream() //
.collect(Collectors.toSet())//
.containsAll(//
new DataBundle().requirements()));
}

@Test
@Ignore
public void requirementsForFeature() throws InvalidSyntaxException {
Collection<Requirement> list = new ResolvedRequirements.Smart(service()).forFeature("PI"); //$NON-NLS-1$
assertEquals(//
Collections.singleton(new DataBundle().pi()), //
new HashSet<Requirement>(list));
}

private Optional<ResolvedRequirements> mayBeService() throws InvalidSyntaxException {
Expand All @@ -52,4 +70,5 @@ private ResolvedRequirements service() throws InvalidSyntaxException {
assumeTrue(service.isPresent());
return service.get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.equinox.requirements;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.passage.lic.internal.base.StringNamedData;

@SuppressWarnings("restriction")
public final class CapabilityLicFeatureIdTest extends CapabilityLicFeatureInfoTest {

@Override
protected StringNamedData infoSupplier(Map<String, Object> attributes) {
return new CapabilityLicFeatureId(attributes);
}

@Override
protected Set<String> expectations() {
return new HashSet<String>(Arrays.asList(//
"PI", //$NON-NLS-1$
"E" //$NON-NLS-1$
));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.equinox.requirements;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.passage.lic.internal.base.StringNamedData;
import org.junit.Test;
import org.osgi.framework.wiring.BundleCapability;

@SuppressWarnings("restriction")
abstract class CapabilityLicFeatureInfoTest {

@Test
public void read() {
DataBundle data = new DataBundle();
assertEquals(//
expectations(), // $NON-NLS-1$
data.capabilities().stream()//
.map(BundleCapability::getAttributes) //
.map(this::infoSupplier) //
.map(StringNamedData::get) //
.filter(Optional::isPresent)//
.map(Optional::get) //
.collect(Collectors.toSet())//
);
}

@Test(expected = NullPointerException.class)
public void nullInputMap() {
infoSupplier(null).get();
}

@Test
public void nullInputValue() {
assertFalse(infoSupplier(Collections.emptyMap()).get().isPresent());
}

protected abstract StringNamedData infoSupplier(Map<String, Object> attributes);

protected abstract Set<String> expectations();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.equinox.requirements;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.passage.lic.internal.base.StringNamedData;

@SuppressWarnings("restriction")
public final class CapabilityLicFeatureNameTest extends CapabilityLicFeatureInfoTest {

@Override
protected StringNamedData infoSupplier(Map<String, Object> attributes) {
return new CapabilityLicFeatureName(attributes);
}

@Override
protected Set<String> expectations() {
return new HashSet<String>(Arrays.asList(//
"PI of version PI", //$NON-NLS-1$
"Euler number" //$NON-NLS-1$
));
}

}
Loading

0 comments on commit ca94996

Please sign in to comment.