com.hltech.pact.gen.domain.pact.PactJsonGenerator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A writePactFile(File,Pact) 0 11 4
A writePactFiles(File,Collection) 0 2 1
1
package com.hltech.pact.gen.domain.pact;
2
3
import com.fasterxml.jackson.annotation.JsonInclude;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.hltech.pact.gen.PactGenerationException;
6
import com.hltech.pact.gen.domain.pact.model.Pact;
7
import lombok.extern.slf4j.Slf4j;
8
9
import java.io.File;
10
import java.io.IOException;
11
import java.util.Collection;
12
13
@Slf4j
14
public class PactJsonGenerator {
15
16
    private ObjectMapper objectMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
17
18
    public void writePactFiles(File destinationDir, Collection<Pact> pacts) {
19
        pacts.forEach(pact -> writePactFile(destinationDir, pact));
20
    }
21
22
    private void writePactFile(File destinationDir, Pact pact) {
23
        if (destinationDir != null && !destinationDir.exists()) {
24
            destinationDir.mkdirs();
25
        }
26
27
        final String pactFileName = pact.getConsumer().getName() + "-" + pact.getProvider().getName() + ".json";
28
        try {
29
            objectMapper.writeValue(new File(destinationDir, pactFileName), pact);
30
        } catch (IOException ex) {
31
            log.error("Unable to write {} to json file", pact);
32
            throw new PactGenerationException("Unable to write pact to json file", ex);
33
        }
34
    }
35
}
36