Converted the storer's interface to byte[]

This commit is contained in:
Marcin Grzejszczak
2018-11-12 22:23:14 +01:00
parent 72f171ed6c
commit a0aaee7578
6 changed files with 18 additions and 18 deletions

View File

@@ -4,7 +4,7 @@ import java.util.HashMap;
import java.util.Map;
/**
* Defines how to store converted contracts to a String representation
* Defines how to store converted contracts to a byte array representation
* that can be stored to drive
*
* @author Marcin Grzejszczak
@@ -15,11 +15,11 @@ public interface ContractStorer<T> {
* Stores the contracts as a map of filename and String
*
* @param contracts - to convert
* @return mapping of filename to converted String representation of the contract
* @return mapping of filename to converted byte array representation of the contract
*/
default Map<String, String> storeAsString(T contracts) {
Map<String, String> map = new HashMap<>();
map.put(String.valueOf(Math.abs(hashCode())), contracts.toString());
default Map<String, byte[]> store(T contracts) {
Map<String, byte[]> map = new HashMap<>();
map.put(String.valueOf(Math.abs(hashCode())), contracts.toString().getBytes());
return map;
}
}

View File

@@ -77,9 +77,9 @@ class PactContractConverter implements ContractConverter<Collection<Pact>> {
}
@Override
Map<String, String> storeAsString(Collection<Pact> contracts) {
Map<String, byte[]> store(Collection<Pact> contracts) {
return contracts.collectEntries {
return [(name(it)) : JsonOutput.prettyPrint(JsonOutput.toJson(it.toMap(PactSpecVersion.V3)))]
return [(name(it)) : JsonOutput.prettyPrint(JsonOutput.toJson(it.toMap(PactSpecVersion.V3))).bytes]
}
}

View File

@@ -424,14 +424,14 @@ class PactContractConverterSpec extends Specification {
and:
Collection<Pact> pacts = converter.convertTo(contracts)
when:
Map<String, String> strings = converter.storeAsString(pacts)
Map<String, byte[]> strings = converter.store(pacts)
then:
strings.size() == 1
strings.keySet().first().startsWith("10-04-pact-consumer_10-05-pact-producer_")
strings.keySet().first().endsWith(".json")
JSONAssert.assertEquals(
new File("src/test/resources/contracts/grouped/shouldWorkWithBeer.json").text,
strings.values().first(), false)
new String(strings.values().first()), false)
}
def "should convert from pact v2 to two SC contracts"() {

View File

@@ -52,10 +52,10 @@ class YamlContractConverter implements ContractConverter<List<YamlContract>> {
}
@Override
Map<String, String> storeAsString(List<YamlContract> contracts) {
Map<String, byte[]> store(List<YamlContract> contracts) {
return contracts.collectEntries {
return [(name(it)) :
this.mapper.writeValueAsString(it)]
this.mapper.writeValueAsString(it).bytes]
}
}

View File

@@ -75,16 +75,16 @@ final class ToFileContractsTransformer {
ContractConverter<Collection> contractConverter = (ContractConverter) name.newInstance()
Collection converted = contractConverter.convertTo(contracts)
log.info("Successfully converted contracts definitions")
Map<String, String> stored = contractConverter.storeAsString(converted)
Map<String, byte[]> stored = contractConverter.store(converted)
File outputFolder = new File(outputPath)
outputFolder.mkdirs()
int i = 1
Set<Map.Entry<String, String>> entries = stored.entrySet()
Set<Map.Entry<String, byte[]>> entries = stored.entrySet()
log.info("Will convert [" + entries.size() + "] contracts")
List<File> files = new ArrayList<>()
for (Map.Entry<String, String> entry : entries) {
for (Map.Entry<String, byte[]> entry : entries) {
File outputFile = new File(outputFolder, entry.getKey())
Files.write(outputFile.toPath(), entry.getValue().getBytes())
Files.write(outputFile.toPath(), entry.getValue())
log.info("[" + i + "/" + entries.size() + "] Successfully stored [" + outputFile.getName() + "]")
files.add(outputFile)
}

View File

@@ -616,7 +616,7 @@ priority: null
ignored: false
'''
when:
Map<String, String> strings = converter.storeAsString([
Map<String, byte[]> strings = converter.store([
new YamlContract(
name: "post1",
request: new YamlContract.Request(method: "POST", url: "/users/1"),
@@ -629,8 +629,8 @@ ignored: false
])
then:
strings.size() == 2
strings["post1.yml"].trim() == expectedYaml1.trim()
strings["post2.yml"].trim() == expectedYaml2.trim()
new String(strings["post1.yml"]).trim() == expectedYaml1.trim()
new String(strings["post2.yml"]).trim() == expectedYaml2.trim()
}
def "should parse messaging contract for [#file]"() {