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
|
|
|
|
22
|
|
|
@RequiredArgsConstructor |
23
|
|
|
public class RepresentationExtractor { |
24
|
|
|
|
25
|
|
|
private final VauntSerializer serializer; |
26
|
|
|
|
27
|
|
|
public Service extractServiceRepresentation(String packageRoot, String serviceName) throws JsonMappingException { |
28
|
|
|
return new Service(serviceName, extractCapabilities(packageRoot), extractExpectations(packageRoot)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private Capabilities extractCapabilities(String packageRoot) { |
32
|
|
|
List<Contract> providerContracts = new ArrayList<>(); |
33
|
|
|
|
34
|
|
|
new Reflections(packageRoot).getTypesAnnotatedWith(Providers.class) |
35
|
|
|
.forEach(providerMessage -> Arrays.stream(providerMessage.getAnnotation(Providers.class).value()) |
36
|
|
|
.forEach(providerAnnotation -> providerContracts.add( |
37
|
|
|
extractProviderContract(providerMessage, providerAnnotation)))); |
38
|
|
|
|
39
|
|
|
new Reflections(packageRoot).getTypesAnnotatedWith(Provider.class) |
40
|
|
|
.forEach(providerMessage -> providerContracts.add( |
41
|
|
|
extractProviderContract(providerMessage, providerMessage.getAnnotation(Provider.class)))); |
42
|
|
|
|
43
|
|
|
return new Capabilities(providerContracts); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private Expectations extractExpectations(String packageRoot) { |
47
|
|
|
Multimap<String, Contract> providerNameToContracts = ArrayListMultimap.create(); |
48
|
|
|
|
49
|
|
|
new Reflections(packageRoot).getTypesAnnotatedWith(Consumers.class) |
50
|
|
|
.forEach(consumerMessage -> Arrays.stream(consumerMessage.getAnnotation(Consumers.class).value()) |
51
|
|
|
.forEach(consumerAnnotation -> providerNameToContracts.put( |
52
|
|
|
consumerAnnotation.providerName(), |
53
|
|
|
extractConsumerContract(consumerMessage, consumerAnnotation)))); |
54
|
|
|
|
55
|
|
|
new Reflections(packageRoot).getTypesAnnotatedWith(Consumer.class) |
56
|
|
|
.forEach(consumerMessage -> providerNameToContracts.put( |
57
|
|
|
consumerMessage.getAnnotation(Consumer.class).providerName(), |
58
|
|
|
extractConsumerContract(consumerMessage, consumerMessage.getAnnotation(Consumer.class)))); |
59
|
|
|
|
60
|
|
|
return new Expectations(providerNameToContracts); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private Contract extractProviderContract(Class<?> providerMessage, Provider providerAnnotation) { |
64
|
|
|
try { |
65
|
|
|
return new Contract( |
66
|
|
|
providerAnnotation.destinationType(), |
67
|
|
|
providerAnnotation.destinationName(), |
68
|
|
|
serializer.generateSchema(providerMessage)); |
69
|
|
|
} catch (JsonMappingException ex) { |
70
|
|
|
throw new RuntimeException("Unable to extract contract for given provider", ex); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private Contract extractConsumerContract(Class<?> consumerMessage, Consumer consumerAnnotation) { |
75
|
|
|
try { |
76
|
|
|
return new Contract( |
77
|
|
|
consumerAnnotation.destinationType(), |
78
|
|
|
consumerAnnotation.destinationName(), |
79
|
|
|
serializer.generateSchema(consumerMessage)); |
80
|
|
|
} catch (JsonMappingException ex) { |
81
|
|
|
throw new RuntimeException("Unable to extract contract for given consumer", ex); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|