1
|
|
|
package com.hltech.vaunt.generator.domain.representation; |
2
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException; |
4
|
|
|
import com.google.common.collect.ArrayListMultimap; |
5
|
|
|
import com.google.common.collect.Multimap; |
6
|
|
|
import com.hltech.vaunt.core.VauntSerializer; |
7
|
|
|
import com.hltech.vaunt.core.domain.model.Capabilities; |
8
|
|
|
import com.hltech.vaunt.core.domain.model.Contract; |
9
|
|
|
import com.hltech.vaunt.core.domain.model.Expectations; |
10
|
|
|
import com.hltech.vaunt.core.domain.model.Service; |
11
|
|
|
import com.hltech.vaunt.generator.domain.representation.annotation.Consumer; |
12
|
|
|
import com.hltech.vaunt.generator.domain.representation.annotation.Consumers; |
13
|
|
|
import com.hltech.vaunt.generator.domain.representation.annotation.Provider; |
14
|
|
|
import com.hltech.vaunt.generator.domain.representation.annotation.Providers; |
15
|
|
|
import lombok.RequiredArgsConstructor; |
16
|
|
|
import org.reflections.Reflections; |
17
|
|
|
|
18
|
|
|
import java.util.ArrayList; |
19
|
|
|
import java.util.Arrays; |
20
|
|
|
import java.util.List; |
21
|
|
|
import java.util.Properties; |
22
|
|
|
|
23
|
|
|
@RequiredArgsConstructor |
24
|
|
|
public class RepresentationExtractor { |
25
|
|
|
|
26
|
|
|
private final VauntSerializer serializer; |
27
|
|
|
|
28
|
|
|
public Service extractServiceRepresentation(String packageRoot, String serviceName, Properties props) |
29
|
|
|
throws JsonMappingException { |
30
|
|
|
return new Service(serviceName, |
31
|
|
|
extractCapabilities(packageRoot, props), |
32
|
|
|
extractExpectations(packageRoot, props)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private Capabilities extractCapabilities(String packageRoot, Properties props) { |
36
|
|
|
List<Contract> providerContracts = new ArrayList<>(); |
37
|
|
|
|
38
|
|
|
new Reflections(packageRoot).getTypesAnnotatedWith(Providers.class) |
39
|
|
|
.forEach(providerMessage -> Arrays.stream(providerMessage.getAnnotation(Providers.class).value()) |
40
|
|
|
.forEach(providerAnnotation -> providerContracts.add( |
41
|
|
|
extractProviderContract(providerMessage, providerAnnotation, props)))); |
42
|
|
|
|
43
|
|
|
new Reflections(packageRoot).getTypesAnnotatedWith(Provider.class) |
44
|
|
|
.forEach(providerMessage -> providerContracts.add(extractProviderContract( |
45
|
|
|
providerMessage, providerMessage.getAnnotation(Provider.class), props))); |
46
|
|
|
|
47
|
|
|
return new Capabilities(providerContracts); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private Expectations extractExpectations(String packageRoot, Properties props) { |
51
|
|
|
Multimap<String, Contract> providerNameToContracts = ArrayListMultimap.create(); |
52
|
|
|
|
53
|
|
|
new Reflections(packageRoot).getTypesAnnotatedWith(Consumers.class) |
54
|
|
|
.forEach(consumerMessage -> Arrays.stream(consumerMessage.getAnnotation(Consumers.class).value()) |
55
|
|
|
.forEach(consumerAnnotation -> providerNameToContracts.put( |
56
|
|
|
consumerAnnotation.providerName(), |
57
|
|
|
extractConsumerContract(consumerMessage, consumerAnnotation, props)))); |
58
|
|
|
|
59
|
|
|
new Reflections(packageRoot).getTypesAnnotatedWith(Consumer.class) |
60
|
|
|
.forEach(consumerMessage -> providerNameToContracts.put( |
61
|
|
|
consumerMessage.getAnnotation(Consumer.class).providerName(), extractConsumerContract( |
62
|
|
|
consumerMessage, consumerMessage.getAnnotation(Consumer.class), props))); |
63
|
|
|
|
64
|
|
|
return new Expectations(providerNameToContracts); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private Contract extractProviderContract(Class<?> providerMessage, Provider providerAnnotation, Properties props) { |
68
|
|
|
try { |
69
|
|
|
return new Contract( |
70
|
|
|
providerAnnotation.destinationType(), |
71
|
|
|
props.getProperty(providerAnnotation.destinationName(), providerAnnotation.destinationName()), |
72
|
|
|
serializer.generateSchema(providerMessage)); |
73
|
|
|
} catch (JsonMappingException ex) { |
74
|
|
|
throw new RuntimeException("Unable to extract contract for given provider", ex); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private Contract extractConsumerContract(Class<?> consumerMessage, Consumer consumerAnnotation, Properties props) { |
79
|
|
|
try { |
80
|
|
|
return new Contract( |
81
|
|
|
consumerAnnotation.destinationType(), |
82
|
|
|
props.getProperty(consumerAnnotation.destinationName(), consumerAnnotation.destinationName()), |
83
|
|
|
serializer.generateSchema(consumerMessage)); |
84
|
|
|
} catch (JsonMappingException ex) { |
85
|
|
|
throw new RuntimeException("Unable to extract contract for given consumer", ex); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|