Skip to content

Commit

Permalink
PI-17473: Added sourceType field in metered usage request
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmicciullo authored and naponce committed Feb 5, 2020
1 parent fba8da3 commit 49b1641
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class MeteredUsageRequest {
private String idempotencyKey;
private boolean billable;
private String sourceType;
private List<MeteredUsageItem> usages = new ArrayList<>();
}

Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ public interface MeteredUsageApiClientService {
*/
APIResult reportUsage(String baseUrl, String secretKey, String idempotentKey, List<MeteredUsageItem> meteredUsageItems, boolean billable);

/**
* Calls the Metered Usage API Endpoint to bill usages.
* This should be used instead of {@link AppmarketBillingClient} as this replaces the old API version.
*
* @param baseUrl from the request
* @param secretKey to sign the request
* @param idempotentKey to make unique calls
* @param meteredUsageItems list of usages to be reported
* @param billable specifies if the usage to be reported is billable or estimated
* @param sourceType specifies the type of source that generate the report (ex. aws, azure_csp, azure_modern)
* @return an {@link APIResult} instance representing the marketplace response
* <p>
* throws an {@link MeteredUsageApiException} to the client with an error code and a status:
*/
APIResult reportUsage(String baseUrl, String secretKey, String idempotentKey, List<MeteredUsageItem> meteredUsageItems, boolean billable, String sourceType);

/**
* Calls the Metered Usage API Endpoint to bill usages.
* This should be used instead of {@link AppmarketBillingClient} as this replaces the old API version.
Expand Down Expand Up @@ -96,6 +112,21 @@ public interface MeteredUsageApiClientService {
*/
APIResult reportUsage(String baseUrl, MeteredUsageItem meteredUsageItem, boolean billable, String secretKey, String secret);

/**
* Calls the Metered Usage API Endpoint to bill usages.
* This should be used instead of {@link AppmarketBillingClient} as this replaces the old API version.
*
* @param baseUrl from the request
* @param secretKey to sign the request
* @param meteredUsageItem usage instance to be reported
* @param billable specifies if the usage to be reported is billable or estimated
* @param sourceType specifies the type of source that generate the report (ex. aws, azure_csp, azure_modern)
* @return an {@link APIResult} instance representing the marketplace response
* <p>
* throws an {@link MeteredUsageApiException} to the client with an error code and a status:
*/
APIResult reportUsage(String baseUrl, MeteredUsageItem meteredUsageItem, boolean billable, String secretKey, String secret, String sourceType);

/**
* Calls the Metered Usage API Endpoint to bill usages.
* This should be used instead of {@link AppmarketBillingClient} as this replaces the old API version.
Expand All @@ -119,10 +150,25 @@ public interface MeteredUsageApiClientService {
* @param idempotentKey to make unique calls
* @param meteredUsageItems list of usages to be reported
* @param billable specifies if the usage to be reported is billable or estimated
* @param sourceType specifies the type of source that generate the report (ex. aws, azure_csp, azure_modern)
* @return an {@link APIResult} instance representing the marketplace response
* <p>
* throws an {@link MeteredUsageApiException} to the client with an error code and a status:
*/
APIResult reportUsage(String baseUrl, String idempotentKey, List<MeteredUsageItem> meteredUsageItems, boolean billable, String secretKey, String secret);
APIResult reportUsage(String baseUrl, String idempotentKey, List<MeteredUsageItem> meteredUsageItems, boolean billable, String secretKey, String secret, String sourceType);

/**
* Calls the Metered Usage API Endpoint to bill usages.
* This should be used instead of {@link AppmarketBillingClient} as this replaces the old API version.
*
* @param baseUrl from the request
* @param secretKey to sign the request
* @param meteredUsageItem usage instance to be reported
* @param billable specifies if the usage to be reported is billable or estimated
* @param sourceType specifies the type of source that generate the report (ex. aws, azure_csp, azure_modern)
* @return an {@link APIResult} instance representing the marketplace response
* <p>
* throws an {@link MeteredUsageApiException} to the client with an error code and a status:
*/
APIResult reportUsage(String baseUrl, String secretKey, MeteredUsageItem meteredUsageItem, boolean billable, String sourceType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,55 @@ public APIResult reportUsage(String baseUrl, String secretKey, MeteredUsageItem
return reportUsage(baseUrl, secretKey, UUID.randomUUID().toString(), Lists.newArrayList(meteredUsageItem), billable);
}

@Override
public APIResult reportUsage(String baseUrl, String secretKey, MeteredUsageItem meteredUsageItem, boolean billable, String sourceType) {
return reportUsage(baseUrl, secretKey, UUID.randomUUID().toString(), Lists.newArrayList(meteredUsageItem), billable, sourceType);
}

@Override
public APIResult reportUsage(String baseUrl, String secretKey, List<MeteredUsageItem> meteredUsageItems, boolean billable) {
return reportUsage(baseUrl, secretKey, UUID.randomUUID().toString(), meteredUsageItems, billable);
}

@Override
public APIResult reportUsage(String baseUrl, String secretKey, String idempotentKey, List<MeteredUsageItem> meteredUsageItems, boolean billable) {
return reportUsage(baseUrl, UUID.randomUUID().toString(), meteredUsageItems, billable, secretKey, credentialsSupplier.getConsumerCredentials(secretKey).developerSecret);
return reportUsage(baseUrl, UUID.randomUUID().toString(), meteredUsageItems, billable, secretKey, credentialsSupplier.getConsumerCredentials(secretKey).developerSecret, org.apache.commons.lang3.StringUtils.EMPTY);
}

@Override
public APIResult reportUsage(String baseUrl, String secretKey, String idempotentKey, List<MeteredUsageItem> meteredUsageItems, boolean billable, String sourceType) {
return reportUsage(baseUrl, UUID.randomUUID().toString(), meteredUsageItems, billable, secretKey, credentialsSupplier.getConsumerCredentials(secretKey).developerSecret, sourceType);
}

@Override
public APIResult reportUsage(String baseUrl, String idempotentKey, MeteredUsageItem meteredUsageItem, boolean billable, String secretKey, String secret) {
return reportUsage(baseUrl, idempotentKey, Lists.newArrayList(meteredUsageItem), billable, secretKey, secret);
return reportUsage(baseUrl, idempotentKey, Lists.newArrayList(meteredUsageItem), billable, secretKey, secret, org.apache.commons.lang3.StringUtils.EMPTY);
}

@Override
public APIResult reportUsage(String baseUrl, MeteredUsageItem meteredUsageItem, boolean billable, String secretKey, String secret) {
return reportUsage(baseUrl, UUID.randomUUID().toString(), Lists.newArrayList(meteredUsageItem), billable, secretKey, secret);
return reportUsage(baseUrl, UUID.randomUUID().toString(), Lists.newArrayList(meteredUsageItem), billable, secretKey, secret, org.apache.commons.lang3.StringUtils.EMPTY);
}

@Override
public APIResult reportUsage(String baseUrl, List<MeteredUsageItem> meteredUsageItems, boolean billable, String secretKey, String secret) {
return reportUsage(baseUrl, UUID.randomUUID().toString(), meteredUsageItems, billable, secretKey, secret);
return reportUsage(baseUrl, UUID.randomUUID().toString(), meteredUsageItems, billable, secretKey, secret, org.apache.commons.lang3.StringUtils.EMPTY);
}

@Override
public APIResult reportUsage(String baseUrl, MeteredUsageItem meteredUsageItem, boolean billable, String secretKey, String secret, String sourceType) {
return reportUsage(baseUrl, UUID.randomUUID().toString(), Lists.newArrayList(meteredUsageItem), billable, secretKey, secret, sourceType);
}

@Override
public APIResult reportUsage(String baseUrl, String idempotentKey, List<MeteredUsageItem> meteredUsageItems, boolean billable, String secretKey, String secret) {
public APIResult reportUsage(String baseUrl, String idempotentKey, List<MeteredUsageItem> meteredUsageItems, boolean billable, String secretKey, String secret, String sourceType) {
Preconditions.checkArgument(!StringUtils.isEmpty(baseUrl), "Base URL must not be empty");
Preconditions.checkArgument(!StringUtils.isEmpty(secretKey), "Secret Key must not be empty");
Preconditions.checkArgument(!StringUtils.isEmpty(idempotentKey), "IdempotentKey must not be empty");
Preconditions.checkArgument(!CollectionUtils.isEmpty(meteredUsageItems), "Usage data to report must not be empty");

// Create Request
MeteredUsageRequest meteredUsageRequest = createMeteredUsageRequest(idempotentKey, meteredUsageItems, billable);
MeteredUsageRequest meteredUsageRequest = createMeteredUsageRequest(idempotentKey, meteredUsageItems, billable, sourceType);

// Create API
MeteredUsageApi meteredUsageApi = createMeteredUsageApi(baseUrl, secretKey, secret);
Expand Down Expand Up @@ -111,11 +126,12 @@ private APIResult processResponse(Response<MeteredUsageResponse> response) {
return new APIResult(false, String.format("Failed to inform Usage with errorCode=%s, message=%s", response.code(), response.message()));
}

private MeteredUsageRequest createMeteredUsageRequest(String idempotentKey, List<MeteredUsageItem> meteredUsageItem, boolean billable) {
private MeteredUsageRequest createMeteredUsageRequest(String idempotentKey, List<MeteredUsageItem> meteredUsageItem, boolean billable, String sourceType) {
return MeteredUsageRequest.builder()
.idempotencyKey(idempotentKey)
.billable(billable)
.usages(meteredUsageItem)
.sourceType(sourceType)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,27 @@ public void testBillUsage_noErrors() {
doReturn(call).when(meteredUsageApi).meteredUsageCall(any());
doReturn(meteredUsageApi).when(meteredUsageApiClientService).createMeteredUsageApi(ConstantUtils.BASE_URL, ConstantUtils.CONSUMER_KEY, ConstantUtils.CONSUMER_SECRET);

APIResult result = meteredUsageApiClientService.reportUsage(ConstantUtils.BASE_URL, ConstantUtils.IDEMPOTENCY_KEY, items, ConstantUtils.BILLABLE, ConstantUtils.CONSUMER_KEY, ConstantUtils.CONSUMER_SECRET);
APIResult result = meteredUsageApiClientService.reportUsage(ConstantUtils.BASE_URL, ConstantUtils.IDEMPOTENCY_KEY, items, ConstantUtils.BILLABLE, ConstantUtils.CONSUMER_KEY, ConstantUtils.CONSUMER_SECRET, ConstantUtils.EMPTY_SOURCE_TYPE);

assertThat(result.isSuccess()).isTrue();
}

@Test
public void testSourceTypeUsage_noErrors() {

MeteredUsageResponse requestAccepted = new MeteredUsageResponse(ConstantUtils.REQUEST_ID, ConstantUtils.IDEMPOTENCY_KEY);

MeteredUsageItem meteredUsageItem = MeteredUsageItemMother.basic().build();

MeteredUsageApi meteredUsageApi = mock(MeteredUsageApi.class);
Response<MeteredUsageResponse> response = buildResponse(requestAccepted);
Call<MeteredUsageResponse> call = new RetrofitCallStub(response).getCall();
List<MeteredUsageItem> items = Lists.newArrayList(meteredUsageItem);

doReturn(call).when(meteredUsageApi).meteredUsageCall(any());
doReturn(meteredUsageApi).when(meteredUsageApiClientService).createMeteredUsageApi(ConstantUtils.BASE_URL, ConstantUtils.CONSUMER_KEY, ConstantUtils.CONSUMER_SECRET);

APIResult result = meteredUsageApiClientService.reportUsage(ConstantUtils.BASE_URL, ConstantUtils.IDEMPOTENCY_KEY, items, ConstantUtils.BILLABLE, ConstantUtils.CONSUMER_KEY, ConstantUtils.CONSUMER_SECRET, ConstantUtils.SOURCE_TYPE);

assertThat(result.isSuccess()).isTrue();
}
Expand All @@ -85,7 +105,7 @@ public void testBillUsage_unsuccessfulCall() {
doReturn(call).when(meteredUsageApi).meteredUsageCall(any());
doReturn(meteredUsageApi).when(meteredUsageApiClientService).createMeteredUsageApi(ConstantUtils.BASE_URL, ConstantUtils.CONSUMER_KEY, ConstantUtils.CONSUMER_SECRET);

APIResult result = meteredUsageApiClientService.reportUsage(ConstantUtils.BASE_URL, ConstantUtils.IDEMPOTENCY_KEY, items, ConstantUtils.BILLABLE, ConstantUtils.CONSUMER_KEY, ConstantUtils.CONSUMER_SECRET);
APIResult result = meteredUsageApiClientService.reportUsage(ConstantUtils.BASE_URL, ConstantUtils.IDEMPOTENCY_KEY, items, ConstantUtils.BILLABLE, ConstantUtils.CONSUMER_KEY, ConstantUtils.CONSUMER_SECRET, ConstantUtils.EMPTY_SOURCE_TYPE);

assertThat(result.isSuccess()).isFalse();
}
Expand All @@ -105,7 +125,7 @@ public void testBillUsageNullSecret_noErrors() {
doReturn(call).when(meteredUsageApi).meteredUsageCall(any());
doReturn(meteredUsageApi).when(meteredUsageApiClientService).createMeteredUsageApi(ConstantUtils.BASE_URL, ConstantUtils.CONSUMER_KEY, null);

APIResult result = meteredUsageApiClientService.reportUsage(ConstantUtils.BASE_URL, ConstantUtils.IDEMPOTENCY_KEY, items, ConstantUtils.BILLABLE, ConstantUtils.CONSUMER_KEY, null);
APIResult result = meteredUsageApiClientService.reportUsage(ConstantUtils.BASE_URL, ConstantUtils.IDEMPOTENCY_KEY, items, ConstantUtils.BILLABLE, ConstantUtils.CONSUMER_KEY, null, ConstantUtils.EMPTY_SOURCE_TYPE);

assertThat(result.isSuccess()).isTrue();
}
Expand All @@ -125,7 +145,7 @@ public void testBillUsageNullSecret_unsuccessfulCall() {
doReturn(call).when(meteredUsageApi).meteredUsageCall(any());
doReturn(meteredUsageApi).when(meteredUsageApiClientService).createMeteredUsageApi(ConstantUtils.BASE_URL, ConstantUtils.CONSUMER_KEY, null);

APIResult result = meteredUsageApiClientService.reportUsage(ConstantUtils.BASE_URL, ConstantUtils.IDEMPOTENCY_KEY, items, ConstantUtils.BILLABLE, ConstantUtils.CONSUMER_KEY, null);
APIResult result = meteredUsageApiClientService.reportUsage(ConstantUtils.BASE_URL, ConstantUtils.IDEMPOTENCY_KEY, items, ConstantUtils.BILLABLE, ConstantUtils.CONSUMER_KEY, null, ConstantUtils.EMPTY_SOURCE_TYPE);

assertThat(result.isSuccess()).isFalse();
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/appdirect/sdk/utils/ConstantUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.time.ZonedDateTime;

import org.apache.commons.lang3.StringUtils;

public class ConstantUtils {
public static final String BASE_URL = "https://localhost:8080/";
public static final String CONSUMER_KEY = "ConsumerKey";
Expand All @@ -23,4 +25,8 @@ public class ConstantUtils {

public static final String TEST_ENCRYPTION_DATA = "Test Encryption Data";
public static final String ENCRYPTED_DATA = "3k6xRbfgNTZwz76WFwoBdt/KDX3C0U/yonosIMSvbVg=";

public static final String EMPTY_SOURCE_TYPE = StringUtils.EMPTY;
public static final String SOURCE_TYPE = "test-source-type";

}

0 comments on commit 49b1641

Please sign in to comment.