|
| 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