Skip to content

Commit

Permalink
code cleanup (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
palfi authored Oct 23, 2023
1 parent f423b58 commit d1701bb
Show file tree
Hide file tree
Showing 38 changed files with 292 additions and 283 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package no.nav.foreldrepenger.dokgen.test.handlebarshelpers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -26,7 +25,7 @@ public class ArrayHelper implements Helper<Object> {
* ```
*/
@Override
public Object apply(Object verdi, Options options) throws IOException {
public Object apply(Object verdi, Options options) {
List<Object> verdier = new ArrayList<>();
if (options.hash.isEmpty()) {
verdier.add(verdi);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class CaseHelper implements Helper<Object> {

@Override
public Object apply(Object caseKonstant, Options options) throws IOException {
Object konstant = options.hash.isEmpty() ? caseKonstant : options.hash;
Map<String, Object> model = (Map<String, Object>) options.context.model();
Object condition_variable = model.get(CONDITION_VARIABLE);
var konstant = options.hash.isEmpty() ? caseKonstant : options.hash;
var model = (Map<String, Object>) options.context.model();
var condition_variable = model.get(CONDITION_VARIABLE);
if (caseKonstant instanceof Iterable) {
if (((List) caseKonstant).contains(condition_variable)) {
incrementConditionFulfilledCounter(model);
Expand All @@ -30,7 +30,7 @@ public Object apply(Object caseKonstant, Options options) throws IOException {
}

private void incrementConditionFulfilledCounter(final Map<String, Object> model) {
Integer antall = (Integer) model.get(CONDITION_FULFILLED);
var antall = (Integer) model.get(CONDITION_FULFILLED);
model.put(CONDITION_FULFILLED, ++antall);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DivideHelper implements Helper<Integer> {
@Override
public Object apply(final Integer antall, final Options options) {
Integer beløp = options.param(0);
BigDecimal value = BigDecimal.valueOf(beløp).divide(BigDecimal.valueOf(antall), 0, RoundingMode.HALF_UP);
var value = BigDecimal.valueOf(beløp).divide(BigDecimal.valueOf(antall), 0, RoundingMode.HALF_UP);

return value.intValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
public class FormatKronerHelper implements Helper<Integer> {
@Override
public Object apply(Integer kroner, final Options options) {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
var formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
var symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class NorwegianDatetimeHelper implements Helper<String> {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");

@Override
public Object apply(String s, Options options) throws IOException {
public Object apply(String s, Options options) {
return formatter.format((DateTimeFormatter.ISO_DATE_TIME.parse(s)));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package no.nav.foreldrepenger.dokgen.test.handlebarshelpers;

import java.io.IOException;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;

public class SizeHelper implements Helper<Object> {

public Object apply(Object o, Options options) throws IOException {
public Object apply(Object o, Options options) {
if (o instanceof ArrayNode) {
return ((ArrayNode) o).size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public Object apply(Object variable, Options options) throws IOException {
variabelVerdier.add(0);
variabelNavn.add("__condition_variable");
variabelVerdier.add(options.hash.isEmpty() ? variable : options.hash);
Context ctx = Context.newBlockParamContext(options.context, variabelNavn, variabelVerdier);
String resultat = options.fn.apply(ctx);
var ctx = Context.newBlockParamContext(options.context, variabelNavn, variabelVerdier);
var resultat = options.fn.apply(ctx);

Integer antall = (Integer) ctx.get("__condition_fulfilled");
var antall = (Integer) ctx.get("__condition_fulfilled");
if (Integer.valueOf(1).equals(antall)) {
return resultat;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import java.util.Set;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.JsonNodeValueResolver;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.context.FieldValueResolver;
import com.github.jknack.handlebars.context.JavaBeanValueResolver;
import com.github.jknack.handlebars.context.MapValueResolver;
Expand All @@ -33,83 +33,95 @@
import no.nav.foreldrepenger.dokgen.test.handlebarshelpers.ThousandSeperatorHelper;
import no.nav.foreldrepenger.dokgen.test.handlebarshelpers.TrimDecimalHelper;

public class TemplateTestService {
public final class TemplateTestService {

private static final Handlebars handlebars;
private static final Handlebars HANDLEBARS;

static {
handlebars = Files.exists(FileStructureUtil.getTemplateRootPath()) ? new Handlebars(
HANDLEBARS = Files.exists(FileStructureUtil.getTemplateRootPath()) ? new Handlebars(
new FileTemplateLoader(FileStructureUtil.getTemplateRootPath().toFile())) : new Handlebars();
handlebars.registerHelper("eq", ConditionalHelpers.eq);
handlebars.registerHelper("neq", ConditionalHelpers.neq);
handlebars.registerHelper("gt", ConditionalHelpers.gt);
handlebars.registerHelper("gte", ConditionalHelpers.gte);
handlebars.registerHelper("lt", ConditionalHelpers.lt);
handlebars.registerHelper("lte", ConditionalHelpers.lte);
handlebars.registerHelper("and", ConditionalHelpers.and);
handlebars.registerHelper("or", ConditionalHelpers.or);
handlebars.registerHelper("not", ConditionalHelpers.not);
handlebars.registerHelper("switch", new SwitchHelper());
handlebars.registerHelper("case", new CaseHelper());
handlebars.registerHelper("add", new AdditionHelper());
handlebars.registerHelper("array", new ArrayHelper());
handlebars.registerHelper("in-array", new InArrayHelper());
handlebars.registerHelper("divide", new DivideHelper());
handlebars.registerHelper("format-kroner", new FormatKronerHelper());
handlebars.registerHelper("thousand-seperator", new ThousandSeperatorHelper());
handlebars.registerHelper("trim-decimal", new TrimDecimalHelper());
handlebars.registerHelper("size", new SizeHelper());
handlebars.registerHelper("norwegian-datetime", new NorwegianDatetimeHelper());
handlebars.registerHelpers(StringHelpers.class);
HANDLEBARS.registerHelper("eq", ConditionalHelpers.eq);
HANDLEBARS.registerHelper("neq", ConditionalHelpers.neq);
HANDLEBARS.registerHelper("gt", ConditionalHelpers.gt);
HANDLEBARS.registerHelper("gte", ConditionalHelpers.gte);
HANDLEBARS.registerHelper("lt", ConditionalHelpers.lt);
HANDLEBARS.registerHelper("lte", ConditionalHelpers.lte);
HANDLEBARS.registerHelper("and", ConditionalHelpers.and);
HANDLEBARS.registerHelper("or", ConditionalHelpers.or);
HANDLEBARS.registerHelper("not", ConditionalHelpers.not);
HANDLEBARS.registerHelper("switch", new SwitchHelper());
HANDLEBARS.registerHelper("case", new CaseHelper());
HANDLEBARS.registerHelper("add", new AdditionHelper());
HANDLEBARS.registerHelper("array", new ArrayHelper());
HANDLEBARS.registerHelper("in-array", new InArrayHelper());
HANDLEBARS.registerHelper("divide", new DivideHelper());
HANDLEBARS.registerHelper("format-kroner", new FormatKronerHelper());
HANDLEBARS.registerHelper("thousand-seperator", new ThousandSeperatorHelper());
HANDLEBARS.registerHelper("trim-decimal", new TrimDecimalHelper());
HANDLEBARS.registerHelper("size", new SizeHelper());
HANDLEBARS.registerHelper("norwegian-datetime", new NorwegianDatetimeHelper());
HANDLEBARS.registerHelpers(StringHelpers.class);
}

public TemplateTestService() {
private TemplateTestService() {
}

public static String compileContent(Brevmal brevmal, String undermal, Språk språk, String testDataFilename) throws Exception {
String templateContent = readFile(FileStructureUtil.getTemplatePath(brevmal, undermal, språk));
String mergeFieldsJsonString = readFile(FileStructureUtil.getTestDataPath(brevmal, undermal, testDataFilename));
public static String compileContent(Brevmal brevmal, String undermal, Språk språk, String testDataFilename) {
var templateContent = readFile(FileStructureUtil.getTemplatePath(brevmal, undermal, språk));
var mergeFieldsJsonString = readFile(FileStructureUtil.getTestDataPath(brevmal, undermal, testDataFilename));
return produceContent(mergeFieldsJsonString, templateContent);
}

public static String compileContent(Brevmal brevmal, Språk språk, String testDataFilename) throws Exception {
String templateContent = readFile(FileStructureUtil.getTemplatePath(brevmal, språk));
String mergeFieldsJsonString = readFile(FileStructureUtil.getTestDataPath(brevmal, testDataFilename));
public static String compileContent(Brevmal brevmal, Språk språk, String testDataFilename) {
var templateContent = readFile(FileStructureUtil.getTemplatePath(brevmal, språk));
var mergeFieldsJsonString = readFile(FileStructureUtil.getTestDataPath(brevmal, testDataFilename));
return produceContent(mergeFieldsJsonString, templateContent);
}

public static String compileContent(Brevmal brevmal, Språk språk, JsonNode testData) throws Exception {
String templateContent = readFile(FileStructureUtil.getTemplatePath(brevmal, språk));
public static String compileContent(Brevmal brevmal, Språk språk, JsonNode testData) {
var templateContent = readFile(FileStructureUtil.getTemplatePath(brevmal, språk));
return produceContent(testData, templateContent);
}

private static String produceContent(final String mergeFieldsJsonString, final String templateContent) throws Exception {
JsonNode mergeFields = getJsonFromString(mergeFieldsJsonString);
private static String produceContent(String mergeFieldsJsonString, String templateContent) {
var mergeFields = getJsonFromString(mergeFieldsJsonString);
return produceContent(mergeFields, templateContent);
}

private static String produceContent(final JsonNode mergeFields, final String templateContent) throws Exception {
Template template = handlebars.compileInline(templateContent);
return removeNewLines(template.apply(with(mergeFields)));
private static String produceContent(JsonNode mergeFields, String templateContent) {
try {
var template = HANDLEBARS.compileInline(templateContent);
return removeNewLines(template.apply(with(mergeFields)));
} catch (IOException e) {
throw new RuntimeException("Feil ved lesing av fil", e);
}
}

public static String getExpected(Brevmal brevmal, String expectedFileName) throws Exception {
Path expectedPath = FileStructureUtil.getExpectedPath(brevmal, expectedFileName);
public static String getExpected(Brevmal brevmal, String expectedFileName) {
var expectedPath = FileStructureUtil.getExpectedPath(brevmal, expectedFileName);
return readFile(expectedPath);
}

public static String getExpected(Brevmal brevmal, String undermal, String expectedFileName) throws Exception {
Path expectedPath = FileStructureUtil.getExpectedPath(brevmal, undermal, expectedFileName);
public static String getExpected(Brevmal brevmal, String undermal, String expectedFileName) {
var expectedPath = FileStructureUtil.getExpectedPath(brevmal, undermal, expectedFileName);
return readFile(expectedPath);
}

private static String readFile(Path file) throws Exception {
return removeNewLines(Files.readString(file));
private static String readFile(Path file) {
try {
return removeNewLines(Files.readString(file));
} catch (IOException e) {
throw new RuntimeException("Feil ved lesing av fil", e);
}
}

private static JsonNode getJsonFromString(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(json);
private static JsonNode getJsonFromString(String json) {
var mapper = new ObjectMapper();
try {
return mapper.readTree(json);
} catch (JsonProcessingException e) {
throw new RuntimeException("Feil med konvertering til jsonnode", e);
}
}

private static Context with(JsonNode model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BfhrMorUførTest {
* "disponibleDagerMedAktivitetskrav": 35,
* "utenMinsterett": false,
*/
void med_og_uten_aktkrav_etter_wlb() throws Exception {
void med_og_uten_aktkrav_etter_wlb() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "forstegangsbehandling/bfhr_mor_ufør_med_og_uten_aktkrav");
var expected = getExpected(BREVMAL, "forstegangsbehandling/bfhr_mor_ufør_med_og_uten_aktkrav.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
Expand All @@ -36,7 +36,7 @@ void med_og_uten_aktkrav_etter_wlb() throws Exception {
* "disponibleDagerMedAktivitetskrav": 35,
* "utenMinsterett": true,
*/
void med_og_uten_aktkrav_før_wlb() throws Exception {
void med_og_uten_aktkrav_før_wlb() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "forstegangsbehandling/bfhr_mor_ufør_med_og_uten_aktkrav_uten_minsterett");
var expected = getExpected(BREVMAL, "forstegangsbehandling/bfhr_mor_ufør_med_aktkrav_uten_minsterett.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
Expand All @@ -50,7 +50,7 @@ void med_og_uten_aktkrav_etter_wlb() throws Exception {
* "disponibleDagerMedAktivitetskrav": 0,
* "utenMinsterett": false,
*/
void uten_aktkrav_etter_wlb() throws Exception {
void uten_aktkrav_etter_wlb() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "forstegangsbehandling/bfhr_mor_ufør_uten_aktkrav");
var expected = getExpected(BREVMAL, "forstegangsbehandling/bfhr_mor_ufør_uten_aktkrav.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
Expand All @@ -64,7 +64,7 @@ void uten_aktkrav_etter_wlb() throws Exception {
* "disponibleDagerMedAktivitetskrav": 0,
* "utenMinsterett": true,
*/
void uten_aktkrav_før_wlb() throws Exception {
void uten_aktkrav_før_wlb() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "forstegangsbehandling/bfhr_mor_ufør_uten_aktkrav_uten_minsterett");
var expected = getExpected(BREVMAL, "forstegangsbehandling/bfhr_mor_ufør_uten_aktkrav_uten_minsterett.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
Expand All @@ -78,7 +78,7 @@ void uten_aktkrav_etter_wlb() throws Exception {
* "disponibleDagerMedAktivitetskrav": 75,
* "utenMinsterett": false,
*/
void med_aktkrav_etter_wlb() throws Exception {
void med_aktkrav_etter_wlb() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "forstegangsbehandling/bfhr_mor_ufør_med_aktkrav");
var expected = getExpected(BREVMAL, "forstegangsbehandling/bfhr_mor_ufør_med_aktkrav.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
Expand All @@ -92,7 +92,7 @@ void med_aktkrav_etter_wlb() throws Exception {
* "disponibleDagerMedAktivitetskrav": 75,
* "utenMinsterett": true,
*/
void med_aktkrav_før_wlb() throws Exception {
void med_aktkrav_før_wlb() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "forstegangsbehandling/bfhr_mor_ufør_med_aktkrav_uten_minsterett");
var expected = getExpected(BREVMAL, "forstegangsbehandling/bfhr_mor_ufør_med_aktkrav.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class EngangsstønadAvslagTest {
private static final Brevmal BREVMAL = Brevmal.ENGANGSSTØNAD_AVSLAG;

@Test
void avslagsbrev_nb() throws Exception {
void avslagsbrev_nb() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "test_avslag_fb");
var expected = getExpected(BREVMAL, "engangsstonad-avslag-fb_nb.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
}

@Test
void avslagsbrev_revurdering_nb() throws Exception {
void avslagsbrev_revurdering_nb() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "test_avslag_rv");
var expected = getExpected(BREVMAL, "engangsstonad-avslag-rv_nb.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,43 @@ class EngangsstønadInnvilgelseTest {
private static final Brevmal BREVMAL = Brevmal.ENGANGSSTØNAD_INNVILGELSE;

@Test
void skal_generere_brev_for_innvilgelse_ESførstegang_på_bokmål() throws Exception {
void skal_generere_brev_for_innvilgelse_ESførstegang_på_bokmål() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "testInnvFB");
var expected = getExpected(BREVMAL, "engangsstonad-innvilgelse_nb.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
}

@Test
void skal_generere_brev_for_innvilgelse_ESførstegang_på_nynorsk() throws Exception {
void skal_generere_brev_for_innvilgelse_ESførstegang_på_nynorsk() {
var content = compileContent(BREVMAL, Språk.NYNORSK, "testInnvFB");
var expected = getExpected(BREVMAL, "engangsstonad-innvilgelse_nn.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
}

@Test
void skal_generere_brev_for_innvilgelse_på_engelsk() throws Exception {
void skal_generere_brev_for_innvilgelse_på_engelsk() {
var content = compileContent(BREVMAL, Språk.ENGELSK, "testInnvFB");
var expected = getExpected(BREVMAL, "engangsstonad-innvilgelse_en.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
}

//Varianter av samme mal
@Test
void skal_generere_brev_for_død_på_bokmål() throws Exception {
void skal_generere_brev_for_død_på_bokmål() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "testDød");
var expected = getExpected(BREVMAL, "engangsstonad-innvilgelse-død_nb.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
}

@Test
void skal_generere_brev_for_medhold_på_bokmål() throws Exception {
void skal_generere_brev_for_medhold_på_bokmål() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "testMedhold");
var expected = getExpected(BREVMAL, "engangsstonad-innvilgelse-medhold_nb.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
}

@Test
void skal_generere_brev_for_endret_sats_på_bokmål() throws Exception {
void skal_generere_brev_for_endret_sats_på_bokmål() {
var content = compileContent(BREVMAL, Språk.BOKMÅL, "testEndretSats");
var expected = getExpected(BREVMAL, "engangsstonad-innvilgelse-endretSats_nb.txt");
assertThat(content).isEqualToIgnoringNewLines(expected);
Expand Down
Loading

0 comments on commit d1701bb

Please sign in to comment.