@@ -695,171 +695,177 @@ In `consumer/src/test/java/au/com/dius/pactworkshop/consumer/ProductConsumerPact
695
695
@ExtendWith (PactConsumerTestExt . class)
696
696
public class ProductConsumerPactTest {
697
697
698
- @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
699
- RequestResponsePact getAllProducts (PactDslWithProvider builder ) {
700
- return builder. given(" products exist" )
701
- .uponReceiving(" get all products" )
702
- .method(" GET" )
703
- .path(" /products" )
704
- .matchHeader(" Authorization" , " Bearer (19|20)\\ d\\ d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]" )
705
- .willRespondWith()
706
- .status(200 )
707
- .headers(Map . of( " Content-Type " , " application/json; charset=utf-8 " ))
708
- .body(newJsonArrayMinLike(2 , array - >
709
- array. object(object - > {
710
- object. stringType(" id" , " 09" );
711
- object. stringType(" type" , " CREDIT_CARD" );
712
- object. stringType(" name" , " Gem Visa" );
713
- })
714
- ). build())
715
- .toPact();
716
- }
698
+ @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
699
+ RequestResponsePact getAllProducts (PactDslWithProvider builder ) {
700
+ return builder. given(" products exist" )
701
+ .uponReceiving(" get all products" )
702
+ .method(" GET" )
703
+ .path(" /products" )
704
+ .matchHeader(" Authorization" , " Bearer (19|20)\\ d\\ d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]" )
705
+ .willRespondWith()
706
+ .status(200 )
707
+ .headers(headers( ))
708
+ .body(newJsonArrayMinLike(2 , array - >
709
+ array. object(object - > {
710
+ object. stringType(" id" , " 09" );
711
+ object. stringType(" type" , " CREDIT_CARD" );
712
+ object. stringType(" name" , " Gem Visa" );
713
+ })
714
+ ). build())
715
+ .toPact();
716
+ }
717
717
718
- @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
719
- RequestResponsePact noProductsExist (PactDslWithProvider builder ) {
720
- return builder. given(" no products exist" )
721
- .uponReceiving(" get all products" )
722
- .method(" GET" )
723
- .path(" /products" )
724
- .matchHeader(" Authorization" , " Bearer (19|20)\\ d\\ d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]" )
725
- .willRespondWith()
726
- .status(200 )
727
- .headers(Map . of( " Content-Type " , " application/json; charset=utf-8 " ))
728
- .body(" []" )
729
- .toPact();
730
- }
718
+ @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
719
+ RequestResponsePact noProductsExist (PactDslWithProvider builder ) {
720
+ return builder. given(" no products exist" )
721
+ .uponReceiving(" get all products" )
722
+ .method(" GET" )
723
+ .path(" /products" )
724
+ .matchHeader(" Authorization" , " Bearer (19|20)\\ d\\ d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]" )
725
+ .willRespondWith()
726
+ .status(200 )
727
+ .headers(headers( ))
728
+ .body(" []" )
729
+ .toPact();
730
+ }
731
731
732
- @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
733
- RequestResponsePact allProductsNoAuthToken (PactDslWithProvider builder ) {
734
- return builder. given(" products exist" )
735
- .uponReceiving(" get all products with no auth token" )
736
- .method(" GET" )
737
- .path(" /products" )
738
- .willRespondWith()
739
- .status(401 )
740
- .toPact();
741
- }
732
+ @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
733
+ RequestResponsePact allProductsNoAuthToken (PactDslWithProvider builder ) {
734
+ return builder. given(" products exist" )
735
+ .uponReceiving(" get all products with no auth token" )
736
+ .method(" GET" )
737
+ .path(" /products" )
738
+ .willRespondWith()
739
+ .status(401 )
740
+ .toPact();
741
+ }
742
742
743
- @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
744
- RequestResponsePact getOneProduct (PactDslWithProvider builder ) {
745
- return builder. given(" product with ID 10 exists" )
746
- .uponReceiving(" get product with ID 10" )
747
- .method(" GET" )
748
- .path(" /product/10" )
749
- .matchHeader(" Authorization" , " Bearer (19|20)\\ d\\ d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]" )
750
- .willRespondWith()
751
- .status(200 )
752
- .headers(Map . of( " Content-Type " , " application/json; charset=utf-8 " ))
753
- .body(newJsonBody(object - > {
754
- object. stringType(" id" , " 10" );
755
- object. stringType(" type" , " CREDIT_CARD" );
756
- object. stringType(" name" , " 28 Degrees" );
757
- }). build())
758
- .toPact();
759
- }
743
+ @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
744
+ RequestResponsePact getOneProduct (PactDslWithProvider builder ) {
745
+ return builder. given(" product with ID 10 exists" )
746
+ .uponReceiving(" get product with ID 10" )
747
+ .method(" GET" )
748
+ .path(" /product/10" )
749
+ .matchHeader(" Authorization" , " Bearer (19|20)\\ d\\ d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]" )
750
+ .willRespondWith()
751
+ .status(200 )
752
+ .headers(headers( ))
753
+ .body(newJsonBody(object - > {
754
+ object. stringType(" id" , " 10" );
755
+ object. stringType(" type" , " CREDIT_CARD" );
756
+ object. stringType(" name" , " 28 Degrees" );
757
+ }). build())
758
+ .toPact();
759
+ }
760
760
761
- @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
762
- RequestResponsePact productDoesNotExist (PactDslWithProvider builder ) {
763
- return builder. given(" product with ID 11 does not exist" )
764
- .uponReceiving(" get product with ID 11" )
765
- .method(" GET" )
766
- .path(" /product/11" )
767
- .matchHeader(" Authorization" , " Bearer (19|20)\\ d\\ d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]" )
768
- .willRespondWith()
769
- .status(404 )
770
- .toPact();
771
- }
761
+ @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
762
+ RequestResponsePact productDoesNotExist (PactDslWithProvider builder ) {
763
+ return builder. given(" product with ID 11 does not exist" )
764
+ .uponReceiving(" get product with ID 11" )
765
+ .method(" GET" )
766
+ .path(" /product/11" )
767
+ .matchHeader(" Authorization" , " Bearer (19|20)\\ d\\ d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]" )
768
+ .willRespondWith()
769
+ .status(404 )
770
+ .toPact();
771
+ }
772
772
773
- @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
774
- RequestResponsePact singleProductnoAuthToken (PactDslWithProvider builder ) {
775
- return builder. given(" product with ID 10 exists" )
776
- .uponReceiving(" get product by ID 10 with no auth token" )
777
- .method(" GET" )
778
- .path(" /product/10" )
779
- .willRespondWith()
780
- .status(401 )
781
- .toPact();
782
- }
773
+ @Pact (consumer = " FrontendApplication" , provider = " ProductService" )
774
+ RequestResponsePact singleProductnoAuthToken (PactDslWithProvider builder ) {
775
+ return builder. given(" product with ID 10 exists" )
776
+ .uponReceiving(" get product by ID 10 with no auth token" )
777
+ .method(" GET" )
778
+ .path(" /product/10" )
779
+ .willRespondWith()
780
+ .status(401 )
781
+ .toPact();
782
+ }
783
783
784
- @Test
785
- @PactTestFor (pactMethod = " getAllProducts" )
786
- void getAllProducts_whenProductsExist (MockServer mockServer ) {
787
- Product product = new Product ();
788
- product. setId(" 09" );
789
- product. setType(" CREDIT_CARD" );
790
- product. setName(" Gem Visa" );
791
- List<Product > expected = List . of (product, product);
784
+ @Test
785
+ @PactTestFor (pactMethod = " getAllProducts" )
786
+ void getAllProducts_whenProductsExist (MockServer mockServer ) {
787
+ Product product = new Product ();
788
+ product. setId(" 09" );
789
+ product. setType(" CREDIT_CARD" );
790
+ product. setName(" Gem Visa" );
791
+ List<Product > expected = Arrays . asList (product, product);
792
792
793
- RestTemplate restTemplate = new RestTemplateBuilder ()
794
- .rootUri(mockServer. getUrl())
795
- .build();
796
- List<Product > products = new ProductService (restTemplate). getAllProducts();
793
+ RestTemplate restTemplate = new RestTemplateBuilder ()
794
+ .rootUri(mockServer. getUrl())
795
+ .build();
796
+ List<Product > products = new ProductService (restTemplate). getAllProducts();
797
797
798
- assertEquals(expected, products);
799
- }
798
+ assertEquals(expected, products);
799
+ }
800
800
801
- @Test
802
- @PactTestFor (pactMethod = " noProductsExist" )
803
- void getAllProducts_whenNoProductsExist (MockServer mockServer ) {
804
- RestTemplate restTemplate = new RestTemplateBuilder ()
805
- .rootUri(mockServer. getUrl())
806
- .build();
807
- List<Product > products = new ProductService (restTemplate). getAllProducts();
801
+ @Test
802
+ @PactTestFor (pactMethod = " noProductsExist" )
803
+ void getAllProducts_whenNoProductsExist (MockServer mockServer ) {
804
+ RestTemplate restTemplate = new RestTemplateBuilder ()
805
+ .rootUri(mockServer. getUrl())
806
+ .build();
807
+ List<Product > products = new ProductService (restTemplate). getAllProducts();
808
808
809
- assertEquals(Collections . emptyList(), products);
810
- }
809
+ assertEquals(Collections . emptyList(), products);
810
+ }
811
811
812
- @Test
813
- @PactTestFor (pactMethod = " allProductsNoAuthToken" )
814
- void getAllProducts_whenNoAuth (MockServer mockServer ) {
815
- RestTemplate restTemplate = new RestTemplateBuilder ()
816
- .rootUri(mockServer. getUrl())
817
- .build();
812
+ @Test
813
+ @PactTestFor (pactMethod = " allProductsNoAuthToken" )
814
+ void getAllProducts_whenNoAuth (MockServer mockServer ) {
815
+ RestTemplate restTemplate = new RestTemplateBuilder ()
816
+ .rootUri(mockServer. getUrl())
817
+ .build();
818
818
819
- HttpClientErrorException e = assertThrows(HttpClientErrorException . class,
820
- () - > new ProductService (restTemplate). getAllProducts());
821
- assertEquals(401 , e. getRawStatusCode());
822
- }
819
+ HttpClientErrorException e = assertThrows(HttpClientErrorException . class,
820
+ () - > new ProductService (restTemplate). getAllProducts());
821
+ assertEquals(401 , e. getRawStatusCode());
822
+ }
823
823
824
- @Test
825
- @PactTestFor (pactMethod = " getOneProduct" )
826
- void getProductById_whenProductWithId10Exists (MockServer mockServer ) {
827
- Product expected = new Product ();
828
- expected. setId(" 10" );
829
- expected. setType(" CREDIT_CARD" );
830
- expected. setName(" 28 Degrees" );
824
+ @Test
825
+ @PactTestFor (pactMethod = " getOneProduct" )
826
+ void getProductById_whenProductWithId10Exists (MockServer mockServer ) {
827
+ Product expected = new Product ();
828
+ expected. setId(" 10" );
829
+ expected. setType(" CREDIT_CARD" );
830
+ expected. setName(" 28 Degrees" );
831
831
832
- RestTemplate restTemplate = new RestTemplateBuilder ()
833
- .rootUri(mockServer. getUrl())
834
- .build();
835
- Product product = new ProductService (restTemplate). getProduct(" 10" );
832
+ RestTemplate restTemplate = new RestTemplateBuilder ()
833
+ .rootUri(mockServer. getUrl())
834
+ .build();
835
+ Product product = new ProductService (restTemplate). getProduct(" 10" );
836
836
837
- assertEquals(expected, product);
838
- }
837
+ assertEquals(expected, product);
838
+ }
839
839
840
- @Test
841
- @PactTestFor (pactMethod = " productDoesNotExist" )
842
- void getProductById_whenProductWithId11DoesNotExist (MockServer mockServer ) {
843
- RestTemplate restTemplate = new RestTemplateBuilder ()
844
- .rootUri(mockServer. getUrl())
845
- .build();
840
+ @Test
841
+ @PactTestFor (pactMethod = " productDoesNotExist" )
842
+ void getProductById_whenProductWithId11DoesNotExist (MockServer mockServer ) {
843
+ RestTemplate restTemplate = new RestTemplateBuilder ()
844
+ .rootUri(mockServer. getUrl())
845
+ .build();
846
846
847
- HttpClientErrorException e = assertThrows(HttpClientErrorException . class,
848
- () - > new ProductService (restTemplate). getProduct(" 11" ));
849
- assertEquals(404 , e. getRawStatusCode());
850
- }
847
+ HttpClientErrorException e = assertThrows(HttpClientErrorException . class,
848
+ () - > new ProductService (restTemplate). getProduct(" 11" ));
849
+ assertEquals(404 , e. getRawStatusCode());
850
+ }
851
851
852
- @Test
853
- @PactTestFor (pactMethod = " singleProductnoAuthToken" )
854
- void getProductById_whenNoAuth (MockServer mockServer ) {
855
- RestTemplate restTemplate = new RestTemplateBuilder ()
856
- .rootUri(mockServer. getUrl())
857
- .build();
852
+ @Test
853
+ @PactTestFor (pactMethod = " singleProductnoAuthToken" )
854
+ void getProductById_whenNoAuth (MockServer mockServer ) {
855
+ RestTemplate restTemplate = new RestTemplateBuilder ()
856
+ .rootUri(mockServer. getUrl())
857
+ .build();
858
858
859
- HttpClientErrorException e = assertThrows(HttpClientErrorException . class,
860
- () - > new ProductService (restTemplate). getProduct(" 10" ));
861
- assertEquals(401 , e. getRawStatusCode());
862
- }
859
+ HttpClientErrorException e = assertThrows(HttpClientErrorException . class,
860
+ () - > new ProductService (restTemplate). getProduct(" 10" ));
861
+ assertEquals(401 , e. getRawStatusCode());
862
+ }
863
+
864
+ private Map<String , String > headers () {
865
+ Map<String , String > headers = new HashMap<> ();
866
+ headers. put(" Content-Type" , " application/json; charset=utf-8" );
867
+ return headers;
868
+ }
863
869
}
864
870
```
865
871
0 commit comments