Skip to content

Commit 94207c8

Browse files
committed
Add Service [rp[erty map
1 parent e3f4657 commit 94207c8

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.acidmanic.pactdoc.dcoumentstructure.namextractors;
7+
8+
import com.acidmanic.pact.helpers.NameExtractor;
9+
import com.acidmanic.pact.helpers.Normalizer;
10+
import com.acidmanic.pact.helpers.RequestPathBuilder;
11+
import com.acidmanic.pactmodels.Interaction;
12+
import com.acidmanic.pactmodels.Request;
13+
14+
/**
15+
*
16+
* @author diego
17+
*/
18+
public class EndpointNameExtractor implements NameExtractor<Interaction> ,Normalizer<String>{
19+
20+
@Override
21+
public String extract(Interaction sourceData) {
22+
23+
Request request = sourceData.getRequest();
24+
25+
if (request != null && request.getPath() != null) {
26+
27+
String pathString = request.getPath();
28+
29+
String endPointUri = new RequestPathBuilder().stripParameters(pathString);
30+
31+
return endPointUri;
32+
}
33+
return "";
34+
}
35+
36+
@Override
37+
public String normalize(String value) {
38+
39+
return value;
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.acidmanic.pactdoc.dcoumentstructure.namextractors;
7+
8+
import com.acidmanic.pact.helpers.NameExtractor;
9+
import com.acidmanic.pact.helpers.Normalizer;
10+
import com.acidmanic.pact.models.EndPoint;
11+
import com.acidmanic.pactmodels.Interaction;
12+
import com.acidmanic.pactmodels.Request;
13+
14+
/**
15+
*
16+
* @author diego
17+
*/
18+
public class ServiceNameExtractor implements NameExtractor<EndPoint>, Normalizer<String> {
19+
20+
@Override
21+
public String extract(EndPoint sourceData) {
22+
23+
if (sourceData.getInteractions() != null && !sourceData.getInteractions().isEmpty()) {
24+
25+
Interaction interaction = sourceData.getInteractions().get(0);
26+
27+
if (interaction != null && interaction.getRequest() != null) {
28+
29+
Request request = interaction.getRequest();
30+
31+
String path = request.getPath();
32+
33+
if (path != null && path.length() > 0) {
34+
35+
String[] uriSegments = path.split("/");
36+
37+
if (uriSegments.length > 0) {
38+
39+
return uriSegments[0];
40+
}
41+
}
42+
}
43+
}
44+
return "";
45+
}
46+
47+
@Override
48+
public String normalize(String value) {
49+
50+
return value;
51+
}
52+
53+
}

src/com/acidmanic/pactdoc/dcoumentstructure/propertymappers/EndpointPropertyMapper.java

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public List<String> keySegmentValues(Object parent) {
6666
}
6767
return segments;
6868
}
69+
70+
6971

7072
@Override
7173
public String keySegmentName() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.acidmanic.pactdoc.dcoumentstructure.propertymappers;
7+
8+
import com.acidmanic.document.structure.propertymapped.PropertyMapper;
9+
import com.acidmanic.pact.helpers.PactClassifier;
10+
import com.acidmanic.pact.models.EndPoint;
11+
import com.acidmanic.pact.models.Service;
12+
import com.acidmanic.pactdoc.dcoumentstructure.namextractors.EndpointNameExtractor;
13+
import com.acidmanic.pactdoc.dcoumentstructure.namextractors.ServiceNameExtractor;
14+
import com.acidmanic.pactmodels.Contract;
15+
import com.acidmanic.pactmodels.Interaction;
16+
import com.acidmanic.pactmodels.Request;
17+
import java.util.ArrayList;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
21+
/**
22+
*
23+
* @author diego
24+
*/
25+
public class ServicePropertyMapper implements PropertyMapper {
26+
27+
@Override
28+
public List<String> keySegmentValues(Object parent) {
29+
30+
List<String> segments = new ArrayList<>();
31+
32+
if (parent instanceof Contract) {
33+
34+
Contract contract = (Contract) parent;
35+
36+
for (Interaction interaction : contract.getInteractions()) {
37+
38+
String serviceName = getServiceName(interaction);
39+
40+
if (serviceName != null) {
41+
42+
if (!containesIgnoreCase(segments, serviceName)) {
43+
44+
segments.add(serviceName);
45+
}
46+
}
47+
48+
}
49+
}
50+
return segments;
51+
}
52+
53+
private boolean containesIgnoreCase(List<String> list, String value) {
54+
55+
for (String item : list) {
56+
57+
if (item.equalsIgnoreCase(value)) {
58+
59+
return true;
60+
}
61+
}
62+
return false;
63+
}
64+
65+
/**
66+
* This method defines service as first segment of the request path
67+
*
68+
* @param interaction
69+
* @return
70+
*/
71+
public static String getServiceName(Interaction interaction) {
72+
73+
if (interaction.getRequest() != null) {
74+
75+
Request request = interaction.getRequest();
76+
77+
String path = request.getPath();
78+
79+
if (path != null && path.length() > 0) {
80+
81+
String[] uriSegments = path.split("/");
82+
83+
if (uriSegments.length > 0) {
84+
return uriSegments[0];
85+
}
86+
}
87+
}
88+
return "";
89+
}
90+
91+
public static String getServiceName(EndPoint endpoint) {
92+
93+
if (endpoint.getInteractions() != null && !endpoint.getInteractions().isEmpty()) {
94+
95+
return getServiceName(endpoint.getInteractions().get(0));
96+
}
97+
return "";
98+
}
99+
100+
public static String getServiceName(Service service) {
101+
102+
if (service.getEndpoints() != null && !service.getEndpoints().isEmpty()) {
103+
104+
return getServiceName(service.getEndpoints().get(0));
105+
}
106+
return "";
107+
}
108+
109+
@Override
110+
public String keySegmentName() {
111+
return "Service";
112+
}
113+
114+
@Override
115+
public Class parentType() {
116+
return Contract.class;
117+
}
118+
119+
@Override
120+
public Object propertyValue(Object parent, String keySegmentValue) {
121+
122+
if (parent instanceof Contract) {
123+
124+
Contract contract = (Contract) parent;
125+
126+
PactClassifier classifier = new PactClassifier();
127+
128+
List<EndPoint> endPoints = classifier.splitByEndpoint(
129+
contract.getInteractions(),
130+
new EndpointNameExtractor());
131+
132+
List<Service> services = classifier.splitByService(
133+
endPoints, new ServiceNameExtractor());
134+
135+
if (keySegmentValue != null) {
136+
137+
for (Service service : services) {
138+
139+
if (keySegmentValue.equalsIgnoreCase(getServiceName(service))) {
140+
141+
return service;
142+
}
143+
}
144+
}
145+
146+
}
147+
return null;
148+
}
149+
150+
}

0 commit comments

Comments
 (0)