Passed
Pull Request — master (#26)
by
unknown
02:03
created

com.hltech.vaunt.generator.VauntGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 20
c 1
b 0
f 0
dl 0
loc 30
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A writeVauntFile(String,String,String) 0 2 1
A write(Service,File) 0 5 2
A serviceRepresentation(String,String) 0 8 2
A targetFile(String,String) 0 2 1
1
package com.hltech.vaunt.generator;
2
3
import com.fasterxml.jackson.databind.JsonMappingException;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.hltech.vaunt.generator.domain.representation.RepresentationExtractor;
6
import com.hltech.vaunt.generator.domain.representation.model.Service;
7
import lombok.RequiredArgsConstructor;
8
9
import java.io.File;
10
import java.io.IOException;
11
import java.nio.file.Paths;
12
13
@RequiredArgsConstructor
14
public class VauntGenerator {
15
16
    private final RepresentationExtractor extractor;
17
    private final ObjectMapper mapper;
18
19
    public void writeVauntFile(String packageRoot, String serviceName, String targetDirectory) {
20
        write(serviceRepresentation(packageRoot, serviceName), targetFile(serviceName, targetDirectory));
21
    }
22
23
    private File targetFile(String serviceName, String targetDirectory) {
24
        return Paths.get(targetDirectory).resolve(serviceName.concat(".json")).toFile();
25
    }
26
27
    private Service serviceRepresentation(String packageRoot, String serviceName) {
28
        try {
29
            return extractor.extractServiceRepresentation(packageRoot, serviceName);
30
        } catch (JsonMappingException ex) {
31
            throw new RuntimeException(
32
                    String.format(
33
                            "Error when trying to extract service representation: package=%s, service name=%s",
34
                            packageRoot, serviceName), ex);
35
        }
36
    }
37
38
    private void write(Service service, File targetFile) {
39
        try {
40
            mapper.writeValue(targetFile, service);
41
        } catch (IOException ex) {
42
            throw new RuntimeException("Error when trying to write vaunt file: " + targetFile.getAbsolutePath(), ex);
43
        }
44
    }
45
}
46