Skip to content

Commit

Permalink
Merge pull request #81 from paulsmelser/bug/serialization_for_dates_n…
Browse files Browse the repository at this point in the history
…ot_working

Bug: Serialization for dates not working
  • Loading branch information
paulsmelser authored Oct 19, 2017
2 parents d27d04b + f5d3311 commit ac05523
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
Expand All @@ -17,6 +18,7 @@ public class PricingMeter {
private String region;
private String unit;
private Double includedQuantity;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssX")
private ZonedDateTime effectiveDate;

public String getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import org.springframework.social.partnercenter.api.order.BillingType;
import org.springframework.social.partnercenter.api.order.ContractType;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Subscription {
Expand All @@ -18,16 +20,22 @@ public class Subscription {
private String friendlyName;
private int quantity;
private String unitType;
private boolean hasPurchasableAddons;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX")
private ZonedDateTime creationDate;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssX")
private ZonedDateTime effectiveStartDate;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssX")
private ZonedDateTime commitmentEndDate;
private SubscriptionStatus status;
private boolean autoRenewEnabled;
@JsonProperty("isTrial")
private boolean trial;
private BillingType billingType;
private BillingCycle billingCycle;
private ContractType contractType;
private boolean autoRenewEnabled;
private String orderId;
private SubscriptionLinks links;
private String orderId;
private ResourceAttributes attributes;

public String getId() {
Expand Down Expand Up @@ -196,6 +204,22 @@ public static SubscriptionBuilder builder(){
return new SubscriptionBuilder();
}

public boolean isTrial() {
return trial;
}

public void setIsTrial(boolean trial) {
this.trial = trial;
}

public boolean isHasPurchasableAddons() {
return hasPurchasableAddons;
}

public void setHasPurchasableAddons(boolean hasPurchasableAddons) {
this.hasPurchasableAddons = hasPurchasableAddons;
}

public static class SubscriptionBuilder{
private String id;
private String offerId;
Expand All @@ -204,6 +228,8 @@ public static class SubscriptionBuilder{
private String friendlyName;
private int quantity;
private String unitType;
private boolean hasPurchasableAddons;
private boolean isTrial;
private ZonedDateTime creationDate;
private ZonedDateTime effectiveStartDate;
private ZonedDateTime commitmentEndDate;
Expand Down Expand Up @@ -296,6 +322,11 @@ public SubscriptionBuilder orderId(String orderId) {
return this;
}

public SubscriptionBuilder trial(boolean trial) {
isTrial = trial;
return this;
}

public SubscriptionBuilder links(SubscriptionLinks links) {
this.links = links;
return this;
Expand All @@ -306,6 +337,11 @@ public SubscriptionBuilder attributes(ResourceAttributes attributes) {
return this;
}

public SubscriptionBuilder hasPurchasableAddons(boolean hasPurchasableAddons) {
this.hasPurchasableAddons = hasPurchasableAddons;
return this;
}

public Subscription build(){
Subscription subscription = new Subscription();
subscription.setId(id);
Expand All @@ -315,6 +351,8 @@ public Subscription build(){
subscription.setAttributes(attributes);
subscription.setOfferId(offerId);
subscription.setOrderId(orderId);
subscription.setIsTrial(isTrial);
subscription.setHasPurchasableAddons(hasPurchasableAddons);
subscription.setAutoRenewEnabled(autoRenewEnabled);
subscription.setBillingCycle(billingCycle);
subscription.setBillingType(billingType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.springframework.social.partnercenter.api.billing.pricing;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.springframework.social.partnercenter.test.stubs.PricingOperationsStubs.given_getAzurePricing_200_OK;

import java.net.URI;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Locale;
Expand All @@ -13,18 +13,19 @@
import org.springframework.http.ResponseEntity;
import org.springframework.social.partnercenter.api.billing.pricing.impl.PricingTemplate;
import org.springframework.social.partnercenter.http.client.RestClient;
import org.springframework.social.partnercenter.test.stubs.StubURI;
import org.springframework.social.partnercenter.test.stubs.TestRestTemplateFactory;

import com.github.tomakehurst.wiremock.junit.WireMockRule;

public class PricingOperationsTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule();
public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort());

@Test
public void testGetAzurePricing_whenCalled_thenResponseIsParsedCorrectly() {
given_getAzurePricing_200_OK();
final PricingTemplate pricingTemplate = new PricingTemplate(new RestClient(TestRestTemplateFactory.createRestTemplate(), URI.create("http://localhost:8080/v1/ratecards/azure")), true);
final PricingTemplate pricingTemplate = new PricingTemplate(new RestClient(TestRestTemplateFactory.createRestTemplate(), StubURI.baseURI(wireMockRule.port(), "v1", "ratecards", "azure")), true);
final ResponseEntity<AzureResourcePricing> azurePricing = pricingTemplate.getAzurePricing();

SoftAssertions.assertSoftly(softly -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
package org.springframework.social.partnercenter.api.order.subscription;

import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.patchRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.springframework.social.partnercenter.test.stubs.StubURI.baseURI;
import static org.springframework.social.partnercenter.test.stubs.SubscriptionOperationStubs.given_getById_200_OK;
import static org.springframework.social.partnercenter.test.stubs.SubscriptionOperationStubs.given_patch_200_OK;
import static org.springframework.social.partnercenter.test.stubs.TestRestTemplateFactory.createRestTemplate;

import java.net.URI;
import java.time.format.DateTimeFormatter;

import org.assertj.core.api.SoftAssertions;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.social.partnercenter.api.order.subscription.impl.SubscriptionTemplate;
import org.springframework.social.partnercenter.http.client.RestClient;
import org.springframework.social.partnercenter.test.stubs.TestRestTemplateFactory;
import org.springframework.social.partnercenter.test.Resource;

import com.github.tomakehurst.wiremock.junit.WireMockRule;

public class SubscriptionOperationsTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule();
public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort());

@Test
public void testById_whenCalled_thenResponseIsParsedCorrectly() {
given_getById_200_OK();

final SubscriptionOperations subscriptionOperations = new SubscriptionTemplate(
new RestClient(TestRestTemplateFactory.createRestTemplate(),URI.create("http://localhost:8080/v1/customers")),
new RestClient(createRestTemplate(), baseURI(wireMockRule.port(), "v1", "customers")),
true);

final Subscription subscription = subscriptionOperations.getById(
Expand All @@ -37,4 +44,20 @@ public void testById_whenCalled_thenResponseIsParsedCorrectly() {
});
}

@Test
public void testUpdateSubscription_validateRequestSerializedCorrectly() {
given_patch_200_OK();

final SubscriptionOperations subscriptionOperations = new SubscriptionTemplate(
new RestClient(createRestTemplate(), baseURI(wireMockRule.port(), "v1", "customers")),
true);

final Subscription subscription = Resource.parseFile("data/subscription/ok.json").getJsonAsObject(Subscription.class);
subscriptionOperations.updateSubscription("hello", "world",
subscription);

verify(patchRequestedFor(urlPathEqualTo("/v1/customers/hello/subscriptions/world"))
.withRequestBody(equalToJson(Resource.parseFile("data/subscription/ok.json").getAsString(), true, true)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.springframework.social.partnercenter.test.stubs;

import java.net.URI;

import org.springframework.web.util.UriComponentsBuilder;

public class StubURI {

private static final String HOST_URI = "http://localhost";

public static URI baseURI(int port) {
return UriComponentsBuilder
.fromUriString(HOST_URI)
.port(port)
.build()
.toUri();
}

public static URI baseURI(int port, String... pathSegments) {
return UriComponentsBuilder.fromUriString(HOST_URI)
.port(port)
.pathSegment(pathSegments)
.build()
.toUri();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.patch;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import org.springframework.social.partnercenter.test.Resource;

Expand All @@ -15,4 +18,13 @@ public static void given_getById_200_OK() {
.withHeader("Content-Type", "application/json")
.withBody(Resource.parseFile("data/subscription/ok.json").getAsString())));
}

public static void given_patch_200_OK() {
stubFor(patch(anyUrl())
.willReturn(aResponse()
.withStatus(200)
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.withBody(Resource.parseFile("data/subscription/ok.json").getAsStringFlattenedString()))
);
}
}
2 changes: 1 addition & 1 deletion src/test/resources/data/subscription/ok.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"id":"6C36999D-F573-408D-A463-ED9EAC6319E1","offerId":"MS-AZR-0146P","offerName":"Microsoft Azure","friendlyName":"Pay As You Go","quantity":20,"unitType":"Usage-based","hasPurchasableAddons":false,"creationDate":"2017-10-04T09:40:28.773Z","effectiveStartDate":"2017-10-04T00:00:00Z","commitmentEndDate":"9999-12-04T00:00:00Z","status":"active","autoRenewEnabled":false,"isTrial":false,"billingType":"usage","billingCycle":"monthly","actions":["Cancel","Edit"],"contractType":"subscription","links":{"offer":{"uri":"/offers/MS-AZR-0146P?country=US","method":"GET","headers":[]},"self":{"uri":"/customers/cec7381b-58d1-455d-b516-134caf447ffa/subscriptions/6C36999D-F573-408D-A463-ED9EAC6319E1","method":"GET","headers":[]}},"orderId":"7CB5E7EA-F17F-4C77-BD7C-6417B7A90CCC","attributes":{"etag":"eyJpZCI6IjZjMzY5OTlkLWY1NzMtNDA4ZC1hNDYzLWVkOWVhYzYzMTllMSIsInZlcnNpb24iOjJ9","objectType":"Subscription"}}
{"id":"6C36999D-F573-408D-A463-ED9EAC6319E1","offerId":"MS-AZR-0146P","offerName":"Microsoft Azure","friendlyName":"Pay As You Go","quantity":20,"unitType":"Usage-based","hasPurchasableAddons":false,"creationDate":"2017-10-04T09:40:28.773Z","effectiveStartDate":"2017-10-04T00:00:00Z","commitmentEndDate":"9999-12-04T00:00:00Z","status":"active","autoRenewEnabled":false,"isTrial":false,"billingType":"usage","billingCycle":"monthly","contractType":"subscription","links":{"offer":{"uri":"/offers/MS-AZR-0146P?country=US","method":"GET","headers":[]},"self":{"uri":"/customers/cec7381b-58d1-455d-b516-134caf447ffa/subscriptions/6C36999D-F573-408D-A463-ED9EAC6319E1","method":"GET","headers":[]}},"orderId":"7CB5E7EA-F17F-4C77-BD7C-6417B7A90CCC","attributes":{"etag":"eyJpZCI6IjZjMzY5OTlkLWY1NzMtNDA4ZC1hNDYzLWVkOWVhYzYzMTllMSIsInZlcnNpb24iOjJ9","objectType":"Subscription"}}

0 comments on commit ac05523

Please sign in to comment.