1
|
|
|
package com.hltech.vaunt.generator.domain.representation; |
2
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException; |
4
|
|
|
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; |
5
|
|
|
import com.google.common.collect.ArrayListMultimap; |
6
|
|
|
import com.google.common.collect.Multimap; |
7
|
|
|
import com.hltech.vaunt.generator.domain.representation.annotation.Consumer; |
8
|
|
|
import com.hltech.vaunt.generator.domain.representation.annotation.Provider; |
9
|
|
|
import com.hltech.vaunt.generator.domain.representation.model.Capabilities; |
10
|
|
|
import com.hltech.vaunt.generator.domain.representation.model.Contract; |
11
|
|
|
import com.hltech.vaunt.generator.domain.representation.model.Expectations; |
12
|
|
|
import com.hltech.vaunt.generator.domain.representation.model.Service; |
13
|
|
|
import lombok.RequiredArgsConstructor; |
14
|
|
|
import org.reflections.Reflections; |
15
|
|
|
|
16
|
|
|
import java.util.ArrayList; |
17
|
|
|
import java.util.List; |
18
|
|
|
|
19
|
|
|
@RequiredArgsConstructor |
20
|
|
|
public class RepresentationExtractor { |
21
|
|
|
|
22
|
|
|
private final JsonSchemaGenerator jsonSchemaGenerator; |
23
|
|
|
|
24
|
|
|
public Service extractServiceRepresentation(String packageRoot, String serviceName) throws JsonMappingException { |
25
|
|
|
return new Service(serviceName, extractCapabilities(packageRoot), extractExpectations(packageRoot)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private Capabilities extractCapabilities(String packageRoot) throws JsonMappingException { |
29
|
|
|
List<Contract> providerContracts = new ArrayList<>(); |
30
|
|
|
for (Class<?> providerMessage : new Reflections(packageRoot).getTypesAnnotatedWith(Provider.class)) { |
31
|
|
|
providerContracts.add(new Contract( |
32
|
|
|
providerMessage.getAnnotation(Provider.class).destinationType(), |
33
|
|
|
providerMessage.getAnnotation(Provider.class).destinationName(), |
34
|
|
|
jsonSchemaGenerator.generateSchema(providerMessage))); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return new Capabilities(providerContracts); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private Expectations extractExpectations(String packageRoot) throws JsonMappingException { |
41
|
|
|
Multimap<String, Contract> providerNameToContracts = ArrayListMultimap.create(); |
42
|
|
|
for (Class<?> consumerMessage : new Reflections(packageRoot).getTypesAnnotatedWith(Consumer.class)) { |
43
|
|
|
Contract contract = new Contract( |
44
|
|
|
consumerMessage.getAnnotation(Consumer.class).destinationType(), |
45
|
|
|
consumerMessage.getAnnotation(Consumer.class).destinationName(), |
46
|
|
|
jsonSchemaGenerator.generateSchema(consumerMessage)); |
47
|
|
|
|
48
|
|
|
providerNameToContracts.put(consumerMessage.getAnnotation(Consumer.class).providerName(), contract); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return new Expectations(providerNameToContracts); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|