Skip to content

Commit

Permalink
Merge pull request #182 from eclipse-passage/561435-2
Browse files Browse the repository at this point in the history
Bug 561435 API revision: requirements resolution: test coverage
  • Loading branch information
eparovyshnaya authored Apr 5, 2020
2 parents ca94996 + 6e0e6b8 commit 86c1919
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ public class EquinoxMessages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.passage.lic.internal.equinox.i18n.EquinoxMessages"; //$NON-NLS-1$

public static String ApplicationConfigurations_contact_defaults;

public static String EquinoxPaths_uri_retrieval_error;
public static String EquinoxRestrictions_feature_is_licensed;
public static String EquinoxRestrictions_feature_is_not_licensed;
public static String EquinoxRestrictions_title;

public static String ComponentConfigurationResolver_error_invalid_bundle_context;
public static String ComponentConfigurationResolver_error_invalid_component_rt;

public static String ComponentRequirements_error_no_resource;
public static String ComponentRequirements_requirement_for_resource;
public static String BundleRequirements_error_bundle_context;

public static String BundleRequirements_no_context;

public static String BundleVendor_unknown_vendor;
public static String EquinoxRestrictionExecutorRegistry_error_name;
public static String EquinoxRestrictionExecutorRegistry_error_title;
public static String EquinoxRestrictionExecutorRegistry_fatal_name;
Expand All @@ -42,9 +40,7 @@ public class EquinoxMessages extends NLS {
public static String EquinoxRestrictionExecutorRegistry_info_title;
public static String EquinoxRestrictionExecutorRegistry_warning_name;
public static String EquinoxRestrictionExecutorRegistry_warning_title;

public static String RequirementsFromCapability_no_attributes;

public static String RequirementsFromCapability_no_feature_id;

public static String RequirementsFromComponent_no_feature_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ EquinoxRestrictions_title=Issues with licensing

ComponentConfigurationResolver_error_invalid_bundle_context=Unable to extract configuration requirements: invalid BundleContext
ComponentConfigurationResolver_error_invalid_component_rt=Unable to extract configuration requirements: invalid ServiceComponentRuntime
ComponentRequirements_error_no_resource=Unable to extract configuration requirements: invalid {1}
ComponentRequirements_requirement_for_resource={1} for {2} OSGi-component
BundleRequirements_no_context=Bundle context for {1} OSGi-component
ComponentRequirements_error_no_resource=Unable to extract configuration requirements: invalid {0}
ComponentRequirements_requirement_for_resource={0} for {1} OSGi-component
BundleRequirements_no_context=Bundle context for {0} OSGi-component
BundleRequirements_error_bundle_context=Unable to extract configuration requirements: invalid BundleContext
BundleVendor_unknown_vendor=Unknown vendor
EquinoxRestrictionExecutorRegistry_error_name=Error
EquinoxRestrictionExecutorRegistry_error_title=Interrupt the execution for the restricted functionality with error, but allow to use other scenarios
EquinoxRestrictionExecutorRegistry_fatal_name=Fatal
Expand All @@ -32,7 +33,7 @@ EquinoxRestrictionExecutorRegistry_info_name=Info
EquinoxRestrictionExecutorRegistry_info_title=Inform about functionality restriction without pausing the execution flow
EquinoxRestrictionExecutorRegistry_warning_name=Warning
EquinoxRestrictionExecutorRegistry_warning_title=Pause the execution flow with warning, but allow to proceed without functionality blocking
RequirementsFromCapability_no_attributes=Attributes for capability {1} in bundle {2} is required
RequirementsFromCapability_no_feature_id=Configuration of a feature identifier in capability {1} of bundle {2} is required
RequirementsFromComponent_no_feature_id=Feature identifier in properties of OSGi component {1}
RequirementsFromComponent_no_properties=Properties for OSGi-component {1}
RequirementsFromCapability_no_attributes=Attributes for capability {0} in bundle {1} is required
RequirementsFromCapability_no_feature_id=Configuration of a feature identifier in capability {0} of bundle {1} is required
RequirementsFromComponent_no_feature_id=Feature identifier in properties of OSGi component {0}
RequirementsFromComponent_no_properties=Properties for OSGi-component {0}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
package org.eclipse.passage.lic.internal.equinox.requirements;

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

import org.eclipse.passage.lic.internal.equinox.i18n.EquinoxMessages;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;

Expand All @@ -33,7 +35,8 @@ protected BundleVendor(Bundle bundle) {

@Override
public String get() {
return bundle.getHeaders().get(Constants.BUNDLE_VENDOR);
return Optional.ofNullable(bundle.getHeaders().get(Constants.BUNDLE_VENDOR))//
.orElse(EquinoxMessages.BundleVendor_unknown_vendor);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ Bundle-Name: Data for Passage LIC Equinox requirements tests
Bundle-Vendor: Eclipse Passage
Bundle-Copyright: %Bundle-Copyright
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"
licensing.feature;licensing.feature="E";version="2.71.82";name="Euler number";level="info";provider="Euler",
licensing.feature;licensing.feature="Incomplete",
licensing.feature

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

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 @@ -39,17 +38,15 @@ public void providedAsResolvedRequirementsImpl() throws InvalidSyntaxException {
}

@Test
@Ignore
public void allRequirements() throws InvalidSyntaxException {
Collection<Requirement> list = service().all();
assertTrue(list.stream() //
Collection<Requirement> requirements = service().all();
assertTrue(requirements.stream() //
.collect(Collectors.toSet())//
.containsAll(//
new DataBundle().requirements()));
new DataBundle().validRequirements()));
}

@Test
@Ignore
public void requirementsForFeature() throws InvalidSyntaxException {
Collection<Requirement> list = new ResolvedRequirements.Smart(service()).forFeature("PI"); //$NON-NLS-1$
assertEquals(//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ protected StringNamedData infoSupplier(Map<String, Object> attributes) {
protected Set<String> expectations() {
return new HashSet<String>(Arrays.asList(//
"PI", //$NON-NLS-1$
"E" //$NON-NLS-1$
"E", //$NON-NLS-1$
"Incomplete" //$NON-NLS-1$
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public Bundle get() {
return bundle.get();
}

Set<Requirement> requirements() {
return new HashSet<Requirement>(Arrays.asList(pi(), e()));
Set<Requirement> validRequirements() {
return new HashSet<Requirement>(Arrays.asList(pi(), e(), incomplete()));
}

BaseRequirement e() {
Expand All @@ -65,8 +65,28 @@ BaseRequirement pi() {
id);
}

BaseRequirement incomplete() {
return new BaseRequirement(//
new BaseFeature(//
"Incomplete", //$NON-NLS-1$
"0.0.0", //$NON-NLS-1$
"Incomplete", //$NON-NLS-1$
"Eclipse Passage"), //$NON-NLS-1$
new RestrictionLevel.Warning(), //
id);
}

List<BundleCapability> capabilities() {
return get().adapt(BundleWiring.class).getCapabilities(//
new LicensingFeatureCapabilitiesFromBundle(get()).key());
}
// BaseRequirement [feature=BaseFeature [id=PI, version=3.14.15, name=PI of version PI, provider=Eclipse Passage], restriction=error, source=org.eclipse.passage.lic.equinox.tests.data.requirements],
// BaseRequirement [feature=BaseFeature [id=Incomplete, version=0.0.0, name=Incomplete, provider=Eclipse Passage], restriction=warn, source=org.eclipse.passage.lic.equinox.tests.data.requirements],
// BaseRequirement [feature=BaseFeature [id=E, version=2.71.82, name=Euler number, provider=Euler], restriction=info, source=org.eclipse.passage.lic.equinox.tests.data.requirements]]> but was:<[

// BaseRequirement [feature=BaseFeature [id=PI, version=3.14.15, name=PI of version PI, provider=Eclipse Passage], restriction=error, source=org.eclipse.passage.lic.equinox.tests.data.requirements],
// BaseRequirement [feature=BaseFeature [id=17149af8056, version=0.0.0, name=Configuration of a feature identifier in capability Data for Passage LIC Equinox requirements tests of bundle <missing argument> is required, provider=Passage License Management], restriction=error, source=org.eclipse.passage.lic.equinox.tests.data.requirements_0.1.0.qualifier [461]],
// BaseRequirement [feature=BaseFeature [id=Incomplete, version=0.0.0, name=Incomplete, provider=Eclipse Passage], restriction=warn, source=org.eclipse.passage.lic.equinox.tests.data.requirements],
// BaseRequirement [feature=BaseFeature [id=E, version=2.71.82, name=Euler number, provider=Euler], restriction=info, source=org.eclipse.passage.lic.equinox.tests.data.requirements

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

Expand All @@ -32,13 +33,23 @@ public void read() {
Optional<List<BundleCapability>> capabilities = new LicensingFeatureCapabilitiesFromBundle(//
new DataBundle().get()).get();
assertTrue(capabilities.isPresent());
assertEquals(4, capabilities.get().size());
assertExpectedContent(capabilities);
}

private void assertExpectedContent(Optional<List<BundleCapability>> capabilities) {
String key = new CapabilityLicFeatureId("").key(); //$NON-NLS-1$
assertEquals(//
new HashSet<String>(Arrays.asList(//
"Incomplete", //$NON-NLS-1$
"PI", //$NON-NLS-1$
"E")), //$NON-NLS-1$
capabilities.get().stream() //
.map(c -> c.getAttributes().get(key))//
.filter(c -> Optional.ofNullable(c.getAttributes()).isPresent()) //
.map(c -> c.getAttributes())//
.filter(attributes -> attributes.containsKey(key))//
.map(attributes -> attributes.get(key))//
.filter(Objects::nonNull) //
.map(Object::toString) //
.collect(Collectors.toSet()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,42 @@
package org.eclipse.passage.lic.internal.equinox.requirements;

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

import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.passage.lic.internal.api.requirements.Requirement;
import org.junit.Test;

@SuppressWarnings("restriction")
public final class RequirementsFromCapabilityTest {

@Test
public void read() {
DataBundle data = new DataBundle();
Set<Requirement> found = data.capabilities().stream() //
.map(c -> new RequirementFromCapability(data.get(), c)) //
.map(RequirementFromCapability::get) //
.collect(Collectors.toSet());
assertContainsUnsatisfiableRequirement(found);
assertContainsAllExpectedRequirements(data, found);
}

private void assertContainsUnsatisfiableRequirement(Set<Requirement> requirements) {
assertTrue(//
requirements.stream()//
.filter(new Unsatisfiable())//
.findAny() //
.isPresent()//
);
}

private void assertContainsAllExpectedRequirements(DataBundle data, Set<Requirement> requirements) {
assertEquals(//
data.requirements(), //
data.capabilities().stream() //
.map(c -> new RequirementFromCapability(data.get(), c)) //
.map(RequirementFromCapability::get) //
data.validRequirements(), //
requirements.stream()//
.filter(r -> !new Unsatisfiable().test(r)) //
.collect(Collectors.toSet())//
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* 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.function.Predicate;

import org.eclipse.passage.lic.internal.api.requirements.Requirement;

@SuppressWarnings("restriction")
final class Unsatisfiable implements Predicate<Requirement> {

@Override
public boolean test(Requirement requirement) {
String identifier = requirement.feature().identifier();
if (identifier.length() < 4) {
return false;
}
return Long.toHexString(System.currentTimeMillis())//
.startsWith(identifier.substring(0, 4));
}

}

0 comments on commit 86c1919

Please sign in to comment.