Skip to content

Commit

Permalink
Simplified unit testing of templates
Browse files Browse the repository at this point in the history
  • Loading branch information
cpoder committed Jul 1, 2024
1 parent 4d6a6af commit be2f128
Showing 1 changed file with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
package lora.ns.objenious;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.support.StaticApplicationContext;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;

import io.restassured.path.json.JsonPath;
import lora.rest.LoraContextService;

@SpringBootTest
@ExtendWith(MockitoExtension.class)
public class ObjeniousIntegrationServiceTest {
private final Logger logger = LoggerFactory.getLogger(ObjeniousIntegrationServiceTest.class);

@Autowired
@Mock
protected LoraContextService loraContextService;

@InjectMocks
private ObjeniousIntegrationService is;

@Spy
private SpringTemplateEngine mMessageTemplateEngine = createTemplateEngine();

public static final String DEVEUI = "0123456789ABCDEF";
public static final String DEVICE_NAME = "My device";
public static final String PAYLOAD = "0123456";
Expand All @@ -37,4 +52,18 @@ void testPayload() {
assertThat(payloadJsonPath.getString("device_properties.external_id"), is(DEVICE_NAME));
assertThat(payloadJsonPath.getString("payload_cleartext"), is(PAYLOAD));
}

private SpringTemplateEngine createTemplateEngine() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(new StaticApplicationContext());
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".xml");
templateResolver.setTemplateMode(TemplateMode.HTML);

var templateEngine = new SpringTemplateEngine();
templateEngine.setEnableSpringELCompiler(true);
templateEngine.setTemplateResolver(templateResolver);

return templateEngine;
}
}

0 comments on commit be2f128

Please sign in to comment.