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