Skip to content

Commit

Permalink
Merge pull request #147 from zelenyhleb/master
Browse files Browse the repository at this point in the history
Bug 560181 - [Passage] add billing core basic implementation
  • Loading branch information
eparovyshnaya authored Feb 26, 2020
2 parents 9608c4d + 0e2d604 commit 5f5ccad
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*******************************************************************************
* 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:
* Nikifor Fedorov <[email protected]> - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.internal.billing.core;

import java.util.Objects;

import org.eclipse.passage.lic.users.UserLicenseDescriptor;

/**
* Local data class designed to sort licenses by product and version.
*
* @since 0.1
*/
public final class ProductVersionLicense {

private final String product;
private final String version;

/**
* @param license license descriptor (must not be null)
*/
public ProductVersionLicense(UserLicenseDescriptor license) {
Objects.requireNonNull(license);
this.product = Objects.requireNonNull(license.getProductIdentifier());
this.version = Objects.requireNonNull(license.getProductVersion());
}

public String getProduct() {
return product;
}

public String getVersion() {
return version;
}

@Override
public boolean equals(Object obj) {
if (!this.getClass().isInstance(obj)) {
return false;
}
ProductVersionLicense license = (ProductVersionLicense) obj;
return Objects.equals(getProduct(), license.getProduct()) && Objects.equals(getVersion(), license.getVersion());
}

@Override
public int hashCode() {
return Objects.hash(getProduct(), getVersion());
}

@Override
public String toString() {
return getProduct() + ":" + getVersion(); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@
*******************************************************************************/
package org.eclipse.passage.loc.internal.billing.core;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.eclipse.passage.lic.users.UserDescriptor;
import org.eclipse.passage.lic.users.UserLicenseDescriptor;

public class UserLicenses {
/**
* Root class of the package. Contains methods that return license descriptors
* filtered by some parameters.
*
* @since 0.1
*/
public final class UserLicenses {

/**
* All users list
Expand Down Expand Up @@ -95,11 +103,22 @@ public boolean test(UserLicenseDescriptor descriptor) {
* @return Linked list of licenses
*/
private final List<UserLicenseDescriptor> getLicenses(Predicate<UserLicenseDescriptor> condition) {
return users.stream()
.map(UserDescriptor::getUserLicenses)
.flatMap(iterable -> StreamSupport.stream(iterable.spliterator(), false))
.filter(condition)
return users.stream().map(UserDescriptor::getUserLicenses)
.flatMap(iterable -> StreamSupport.stream(iterable.spliterator(), false)).filter(condition)
.collect(Collectors.toList());
}

/**
* Returns a number of issued licenses for each product-version
*
* @return Map where ProductVersionLicense (single license descriptor) is a key
* and number of licenses is a value
*/
public final Map<ProductVersionLicense, Integer> getLicensesNumbers() {
List<ProductVersionLicense> licenses = getAllLicenses().stream().map(ProductVersionLicense::new)
.collect(Collectors.toList());
return licenses.stream().collect(Collectors.toMap(license -> license,
license -> Collections.frequency(licenses, license), (l1, l2) -> l1));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.passage.loc.billing.core.tests;

import java.util.Date;
import java.util.Objects;

import org.eclipse.passage.lic.users.UserDescriptor;
import org.eclipse.passage.lic.users.UserLicenseDescriptor;
Expand All @@ -36,22 +37,38 @@ public FakeLicenseDescriptor(String identifier, Date validFrom, Date validUntil,
this.issueDate = issueDate;
}

public FakeLicenseDescriptor(String identifier, Date issueDate, String productIdentifier, String versionIdentifier) {
public FakeLicenseDescriptor(String identifier, Date issueDate, String productIdentifier,
String versionIdentifier) {
this(identifier, new Date(), new Date(), issueDate, productIdentifier, versionIdentifier);
}

public FakeLicenseDescriptor(String identifier, String productIdentifier, String versionIdentifier) {
this(identifier, new Date(), productIdentifier, versionIdentifier);
}

public FakeLicenseDescriptor(String productIdentifier) {
this("", productIdentifier, ""); //$NON-NLS-1$ //$NON-NLS-2$
}

public FakeLicenseDescriptor(String productIdentifier, String versionIdentifier) {
this("", productIdentifier, versionIdentifier); //$NON-NLS-1$
}

@Override
public boolean equals(Object obj) {
if (!this.getClass().isInstance(obj))
return false;
FakeLicenseDescriptor descriptor = (FakeLicenseDescriptor) obj;
return Objects.equals(planIdentifier, descriptor.planIdentifier) &&
Objects.equals(versionIdentifier, descriptor.versionIdentifier) &&
Objects.equals(productIdentifier, descriptor.productIdentifier);
}

@Override
public int hashCode() {
return Objects.hash(productIdentifier, versionIdentifier, planIdentifier);
}

@Override
public String getPlanIdentifier() {
return planIdentifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*******************************************************************************
* 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:
* Nikifor Fedorov <[email protected]> - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.billing.core.tests;

import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class StringsProvider {

private final List<String> strings = new LinkedList<String>();
private final Random random = new Random();

private boolean contains(String string) {
return strings.contains(string);
}

private boolean add(String string) {
return strings.add(string);
}

public final void clear() {
strings.clear();
}

public final String randomString() {
String string = new String(array(), Charset.forName("UTF-8")); //$NON-NLS-1$
if (contains(string)) {
return randomString();
}
add(string);
return string;
}

private byte[] array() {
byte[] array = new byte[5];
random.nextBytes(array);
return array;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,75 +14,95 @@

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.passage.lic.users.UserDescriptor;
import org.eclipse.passage.lic.users.UserLicenseDescriptor;
import org.eclipse.passage.loc.internal.billing.core.ProductVersionLicense;
import org.eclipse.passage.loc.internal.billing.core.UserLicenses;
import org.junit.Test;

public class UserLicensesTest {

private final StringsProvider provider = new StringsProvider();

@Test
public void allLicenses() {
List<UserLicenseDescriptor> licenses1 = new ArrayList<UserLicenseDescriptor>();
licenses1.add(new FakeLicenseDescriptor("foo")); //$NON-NLS-1$
licenses1.add(new FakeLicenseDescriptor("bar")); //$NON-NLS-1$

List<UserLicenseDescriptor> licenses2 = new ArrayList<UserLicenseDescriptor>();
licenses2.add(new FakeLicenseDescriptor("baz")); //$NON-NLS-1$
licenses2.add(new FakeLicenseDescriptor("foo")); //$NON-NLS-1$
List<UserLicenseDescriptor> licenses1 = licenses(license(provider.randomString()),
license(provider.randomString()));
List<UserLicenseDescriptor> licenses2 = licenses(license(provider.randomString()),
license(provider.randomString()));

UserLicenses service = new UserLicenses(
Arrays.asList(new FakeUser[] { new FakeUser(licenses1), new FakeUser(licenses2) }));
UserLicenses service = service(user(licenses1), user(licenses2));

assertEquals(Stream.concat(licenses1.stream(), licenses2.stream()).collect(Collectors.toList()),
service.getAllLicenses());
}

@Test
public void licensesForProduct() {
String product = "product"; //$NON-NLS-1$
FakeLicenseDescriptor license1 = new FakeLicenseDescriptor(product);
FakeLicenseDescriptor license2 = new FakeLicenseDescriptor(product);
String product = provider.randomString();
List<UserLicenseDescriptor> licenses1 = licenses(license(product), license(provider.randomString()));
List<UserLicenseDescriptor> licenses2 = licenses(license(product), license(provider.randomString()));

List<UserLicenseDescriptor> licenses1 = new ArrayList<UserLicenseDescriptor>();
licenses1.add(license1);
licenses1.add(new FakeLicenseDescriptor("bar")); //$NON-NLS-1$
UserLicenses service = service(user(licenses1), user(licenses2));

assertEquals(Arrays.asList(license(product), license(product)), service.getLicensesForProduct(product));
}

List<UserLicenseDescriptor> licenses2 = new ArrayList<UserLicenseDescriptor>();
licenses2.add(new FakeLicenseDescriptor("baz")); //$NON-NLS-1$
licenses2.add(license2);
@Test
public void licensesForProductVersion() {
String product = provider.randomString();
String version = provider.randomString();

UserLicenses service = new UserLicenses(
Arrays.asList(new FakeUser[] { new FakeUser(licenses1), new FakeUser(licenses2) }));
UserLicenses service = service(
user(licenses(license(product, version), license(provider.randomString()))),
user(licenses(license(product), license(provider.randomString()))));

assertEquals(Arrays.asList(license1, license2), service.getLicensesForProduct(product));
assertEquals(Arrays.asList(license(product, version)),
service.getLicensesForProductVersion(product, version));
}

@Test
public void licensesForProductVersion() {
String product = "product"; //$NON-NLS-1$
String version = "version"; //$NON-NLS-1$
public void countSameLicenses() {
UserLicenseDescriptor license1 = license(provider.randomString(), provider.randomString());
UserLicenseDescriptor license2 = license(provider.randomString(), provider.randomString());
UserLicenseDescriptor license3 = license(provider.randomString(), provider.randomString());

UserLicenses service = service(user(licenses(license1, license3)),
user(licenses(license2, license3)), user(licenses(license1, license2, license3)));

FakeLicenseDescriptor license1 = new FakeLicenseDescriptor(product, version);
FakeLicenseDescriptor license2 = new FakeLicenseDescriptor(product); // default version is empty string
assertEquals(Arrays.asList(2, 2, 3), numbers(service, license1, license2, license3));
}

private List<Integer> numbers(UserLicenses service, UserLicenseDescriptor... descriptors) {
return Arrays.asList(descriptors).stream()
.map(ProductVersionLicense::new)
.map(license -> service.getLicensesNumbers().get(license))
.collect(Collectors.toList());
}

private UserLicenseDescriptor license(String product) {
return new FakeLicenseDescriptor(product);
}

List<UserLicenseDescriptor> licenses1 = new ArrayList<UserLicenseDescriptor>();
licenses1.add(license1);
licenses1.add(new FakeLicenseDescriptor("bar")); //$NON-NLS-1$
private UserLicenseDescriptor license(String product, String version) {
return new FakeLicenseDescriptor(product, version);
}

List<UserLicenseDescriptor> licenses2 = new ArrayList<UserLicenseDescriptor>();
licenses2.add(new FakeLicenseDescriptor("baz")); //$NON-NLS-1$
licenses2.add(license2);
private List<UserLicenseDescriptor> licenses(UserLicenseDescriptor... fakeLicenseDescriptors) {
return Arrays.asList(fakeLicenseDescriptors);
}

UserLicenses service = new UserLicenses(
Arrays.asList(new FakeUser[] { new FakeUser(licenses1), new FakeUser(licenses2) }));
private UserDescriptor user(List<UserLicenseDescriptor> licenses) {
return new FakeUser(licenses);
}

assertEquals(Arrays.asList(license1), service.getLicensesForProductVersion(product, version));
private UserLicenses service(UserDescriptor... users) {
return new UserLicenses(Arrays.asList(users));
}

}

0 comments on commit 5f5ccad

Please sign in to comment.