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

targetFile(String,String)   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 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