Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/p-pact-contract-test' into p-pac…
Browse files Browse the repository at this point in the history
…t-contract-test

# Conflicts:
#	services/Promoter/src/test/java/com/bottega/promoter/concert/api/rest/ConcertRestController_discountConcert_restApiTest.java
  • Loading branch information
yacekmm committed Nov 28, 2024
2 parents 90d8886 + 412f010 commit c968d88
Show file tree
Hide file tree
Showing 24 changed files with 742 additions and 84 deletions.
107 changes: 107 additions & 0 deletions .github/workflows/ci-pricing-liveCoding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: LiveCoding Pricing CI
run-name: LiveCoding Pricing CI

on:
push:
paths:
- 'services/Pricing/**'
- 'libs/SharedLib/**'
- '.github/workflows/ci-pricing-liveCoding.yml'
- '.github/workflows/ci-shared-lib.yml'
workflow_dispatch:
repository_dispatch:
types: [pricing-ci-webhook]

env:
PACT_BROKER_BASE_URL: ${{ vars.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
PACTICIPANT: "LiveCoding.Tickets.Pricing"

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

# Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies.
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0

- name: Publish Promoter Spring Cloud Contract to local maven repo
run: >-
cd services/Promoter &&
./gradlew publishToMavenLocal
- name: Test
run: >-
cd services/Pricing &&
./gradlew
-Dpact.verifier.publishResults=true
-Dpact.provider.version=$PACTICIPANT-${{ github.sha }}
-Dpact.provider.branchName=${{ github.ref_name }}
-Dpact.provider.tag=${{ github.ref_name }}
-Dpact.rootDir=build/pacts
test publishToMavenLocal
dev-deployment:
needs: build
runs-on: ubuntu-latest
environment: dev
steps:
- uses: actions/checkout@v4

- name: Can I deploy to dev?
run: >-
docker run --rm \
-e PACT_BROKER_BASE_URL=$PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN=$PACT_BROKER_TOKEN \
pactfoundation/pact-cli:1.1.0.0 \
broker can-i-deploy --pacticipant=$PACTICIPANT --to-environment=dev-liveCoding --version=$PACTICIPANT-${{ github.sha }}
- name: Deploy to Dev
run: echo "Deploying to dev"

- name: Record Pact deployment to dev
run: >-
docker run --rm \
-e PACT_BROKER_BASE_URL=$PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN=$PACT_BROKER_TOKEN \
pactfoundation/pact-cli:1.1.0.0 \
broker record-deployment --pacticipant=$PACTICIPANT --environment=dev-liveCoding --version=$PACTICIPANT-${{ github.sha }}
prod-deployment:
needs: dev-deployment
runs-on: ubuntu-latest
environment: prod
steps:
- uses: actions/checkout@v4

- name: Can I deploy to prod?
run: >-
docker run --rm \
-e PACT_BROKER_BASE_URL=$PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN=$PACT_BROKER_TOKEN \
pactfoundation/pact-cli:1.1.0.0 \
broker can-i-deploy --pacticipant=$PACTICIPANT --to-environment=prod-liveCoding --version=$PACTICIPANT-${{ github.sha }} --ignore="Tickets.Promoter"
- name: Deploy to prod
run: echo "Deploying to prod"

- name: Record Pact deployment to prod
run: >-
docker run --rm \
-e PACT_BROKER_BASE_URL=$PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN=$PACT_BROKER_TOKEN \
pactfoundation/pact-cli:1.1.0.0 \
broker record-deployment --pacticipant=$PACTICIPANT --environment=prod-liveCoding --version=$PACTICIPANT-${{ github.sha }}
113 changes: 113 additions & 0 deletions .github/workflows/ci-promoter-liveCoding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
name: LiveCoding Promoter CI
run-name: LiveCoding Promoter CI

on:
push:
paths:
- 'services/Promoter/**'
- 'libs/SharedLib/**'
- '.github/workflows/ci-promoter.yml'
- '.github/workflows/ci-promoter-liveCoding.yml'
- '.github/workflows/ci-shared-lib.yml'
workflow_dispatch:
repository_dispatch:
types: [promoter-ci-webhook]

env:
PACT_BROKER_BASE_URL: ${{ vars.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
PACTICIPANT: "LiveCoding.Tickets.Promoter"

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

# Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies.
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0

- name: Publish Pricing Spring Cloud Contract to local maven repo
run: >-
cd services/Pricing &&
./gradlew publishToMavenLocal
- name: Test
run: >-
cd services/Promoter &&
./gradlew
-Dpact.rootDir=build/pacts
test publishToMavenLocal
- name: Publish Promoter Consumer Pacts
run: >-
docker run --rm \
-w ${PWD} \
-v ${PWD}:${PWD} \
-e PACT_BROKER_BASE_URL=$PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN=$PACT_BROKER_TOKEN \
pactfoundation/pact-cli:1.1.0.0 \
broker publish ${PWD}/services/Promoter/build/pacts --consumer-app-version=$PACTICIPANT-${{ github.sha }} --branch=${{github.ref_name}} --tag-with-git-branch
dev-deployment:
needs: build
runs-on: ubuntu-latest
environment: dev
steps:
- uses: actions/checkout@v4

- name: Can I deploy to dev?
run: >-
docker run --rm \
-e PACT_BROKER_BASE_URL=$PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN=$PACT_BROKER_TOKEN \
pactfoundation/pact-cli:1.1.0.0 \
broker can-i-deploy --pacticipant=$PACTICIPANT --to-environment=dev-liveCoding --version=$PACTICIPANT-${{ github.sha }}
- name: Deploy to Dev
run: echo "Deploying to dev"

- name: Record Pact deployment to dev
run: >-
docker run --rm \
-e PACT_BROKER_BASE_URL=$PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN=$PACT_BROKER_TOKEN \
pactfoundation/pact-cli:1.1.0.0 \
broker record-deployment --pacticipant=$PACTICIPANT --environment=dev-liveCoding --version=$PACTICIPANT-${{ github.sha }}
prod-deployment:
needs: dev-deployment
runs-on: ubuntu-latest
environment: prod
steps:
- uses: actions/checkout@v4

- name: Can I deploy to prod?
run: >-
docker run --rm \
-e PACT_BROKER_BASE_URL=$PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN=$PACT_BROKER_TOKEN \
pactfoundation/pact-cli:1.1.0.0 \
broker can-i-deploy --pacticipant=$PACTICIPANT --to-environment=prod-liveCoding --version=$PACTICIPANT-${{ github.sha }}
- name: Deploy to prod
run: echo "Deploying to prod"

- name: Record Pact deployment to prod
run: >-
docker run --rm \
-e PACT_BROKER_BASE_URL=$PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN=$PACT_BROKER_TOKEN \
pactfoundation/pact-cli:1.1.0.0 \
broker record-deployment --pacticipant=$PACTICIPANT --environment=prod-liveCoding --version=$PACTICIPANT-${{ github.sha }}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import org.apache.groovy.util.Maps;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner;
import org.springframework.test.context.*;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static com.bottega.sharedlib.config.CdcStubs.CDC_STUB_ID_PROMOTER;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import static org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties.StubsMode.LOCAL;
Expand Down Expand Up @@ -58,6 +62,7 @@ static void kafkaProperties(DynamicPropertyRegistry registry) {

@BeforeEach
public void beforeEach() {
System.setProperty("pact.rootDir", "build/pacts");
kafkaContainerFixtures.beforeEach();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.bottega.pricing.price.api.rest;

import au.com.dius.pact.provider.junit5.HttpTestTarget;
import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify;
import au.com.dius.pact.provider.junitsupport.Provider;
import au.com.dius.pact.provider.junitsupport.State;
import au.com.dius.pact.provider.junitsupport.loader.PactBroker;
import au.com.dius.pact.provider.junitsupport.loader.PactBrokerAuth;
import com.bottega.pricing.fixtures.FrameworkTestBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Value;

@Provider("LiveCoding.Tickets.Pricing")
@PactBroker(url = "https://jacek.pactflow.io", authentication = @PactBrokerAuth(token = "${PACT_BROKER_TOKEN}"))
@IgnoreNoPactsToVerify
public class Pricing_providesApiContract_liveCoding_pactApiTest extends FrameworkTestBase {

@Value("${server.port}")
int port;

@BeforeEach
public void setupTestTarget(PactVerificationContext context) {
super.beforeEach();
if (context != null) {
context.setTarget(new HttpTestTarget("localhost", port));
}
System.setProperty("pact.verifier.publishResults", "true");
}

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
public void pricingPactProviderVerification(PactVerificationContext context) {
if (context != null) {
context.verifyInteraction();
}
}

@State("price for item exists")
public void priceForItemDiscount() {
builders.aPrice().priceForItem(100_00, "123").inDb();
}


}
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package com.bottega.promoter.concert;

import java.util.List;

import com.bottega.sharedlib.vo.Money;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

@EqualsAndHashCode
@AllArgsConstructor
@ToString
@Getter
public class Price {

private String id;
private String itemId;
private Money price;
private List<PriceFactor> factors;

public Price applyFactor(PriceFactor factor) {
this.price = price.subtract(price.percentage(factor.value()));
this.factors.add(factor);
return this;
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package com.bottega.promoter.concert.api.app;

import java.util.List;

import com.bottega.promoter.agreements.*;
import com.bottega.promoter.agreements.PromoterAgreement;
import com.bottega.promoter.agreements.PromoterService;
import com.bottega.promoter.concert.Price;
import com.bottega.promoter.concert.domain.*;
import com.bottega.promoter.concert.infra.repo.ConcertRepo;
import com.bottega.promoter.infra.client.pricing.PricingClient;
import com.bottega.promoter.pricing.api.app.PricingService;
import com.bottega.sharedlib.ddd.ApplicationService;
import com.bottega.sharedlib.event.EventPublisher;
import com.bottega.sharedlib.vo.error.ErrorResult;
import io.vavr.control.Either;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import java.util.List;

import static com.bottega.promoter.concert.domain.PromoterEventFactory.concertCreated;
import static com.bottega.sharedlib.vo.error.ErrorResult.notFound;
import static com.bottega.sharedlib.vo.error.GenericErrorCode.not_found;
import static io.vavr.control.Option.ofOptional;

@ApplicationService
@AllArgsConstructor
@RequiredArgsConstructor
public class ConcertService {

private final ConcertFactory concertFactory;
Expand All @@ -28,6 +32,8 @@ public class ConcertService {
private final TagService tagService;
private final CategoryService categoryService;
private final PromoterService promoterService;
@Setter
private PricingService pricingService;

public Either<ErrorResult, Concert> createConcert(String title, String dateTime, String promoterIdString) {
PromoterAgreement promoterAgreement = promoterService.getPromoterAgreement(promoterIdString);
Expand All @@ -45,4 +51,10 @@ public Either<ErrorResult, List<Price>> discountConcert(String concertId, int pe
.toEither(notFound(not_found, "Concert with given ID does not exist. ID: " + concertId))
.flatMap(concert -> pricingClient.applyPercentageDiscount(concert.getId(), percentage));
}

public Either<ErrorResult, List<Price>> discountConcertLiveCoding(String concertId, int percentage) {
return ofOptional(concertRepo.findById(new ConcertId(concertId)))
.toEither(notFound(not_found, "Concert with given ID does not exist. ID: " + concertId))
.flatMap(concert -> pricingService.applyPercentageDiscount(concert.getId(), percentage));
}
}
Loading

0 comments on commit c968d88

Please sign in to comment.