blob: 0b1819d2870e90a108a3a0457b6ecabf537d38a2 [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.template.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import io.cucumber.java8.En;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.fineract.template.domain.Template;
import org.apache.fineract.template.domain.TemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
public class TemplateServiceStepDefinitions implements En {
@Autowired
private TemplateMergeService tms;
private String template;
private Map<String, Object> data;
private String result;
public TemplateServiceStepDefinitions() {
Given("/^A mustache template file (.*)$/", (String file) -> {
template = IOUtils.resourceToString("templates/" + file, StandardCharsets.UTF_8,
TemplateServiceStepDefinitions.class.getClassLoader());
});
Given("/^A JSON data file (.*)$/", (String file) -> {
data = parse(IOUtils.resourceToString("templates/" + file, StandardCharsets.UTF_8,
TemplateServiceStepDefinitions.class.getClassLoader()));
});
When("The user merges the template with data", () -> {
result = compile(template, data);
});
Then("/^The result should match the content of file (.*)$/", (String file) -> {
String expected = IOUtils.resourceToString("results/" + file, StandardCharsets.UTF_8,
TemplateServiceStepDefinitions.class.getClassLoader());
assertEquals(expected, result);
});
}
private String compile(String templateText, Map<String, Object> scope) throws IOException {
List<TemplateMapper> mappers = new ArrayList<>();
Template template = new Template("TemplateName", templateText, null, null, mappers);
return tms.compile(template, scope);
}
private Map<String, Object> parse(String data) {
Gson gson = new Gson();
Type ssMap = new TypeToken<Map<String, Object>>() {}.getType();
JsonElement json = JsonParser.parseString(data);
return gson.fromJson(json, ssMap);
}
}