(chore) more test coverage

This commit is contained in:
Andreas Penski (init) 2020-08-14 08:56:45 +02:00
parent aad08768dd
commit bcbb0de09c
6 changed files with 95 additions and 6 deletions

View file

@ -28,7 +28,6 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.config.ConfigurationLoader;
import de.kosit.validationtool.impl.ContentRepository;
import de.kosit.validationtool.impl.Scenario;
@ -55,7 +54,7 @@ public class CheckConfiguration implements Configuration {
*/
private URI scenarioRepository;
private ConfigurationLoader loader;
private Configuration delegate;

View file

@ -15,13 +15,13 @@ abstract class BaseHandler implements HttpHandler {
protected static final String APPLICATION_XML = "application/xml";
protected static final int OK = 200;
static final int OK = 200;
protected static void write(final HttpExchange exchange, final byte[] content, final String contentType) throws IOException {
write(exchange, contentType, os -> os.write(content));
}
protected static void write(final HttpExchange exchange, final String contentType, Write write) throws IOException {
protected static void write(final HttpExchange exchange, final String contentType, final Write write) throws IOException {
exchange.getResponseHeaders().add("Content-Type", contentType);
exchange.sendResponseHeaders(OK, 0);
final OutputStream os = exchange.getResponseBody();
@ -41,6 +41,6 @@ abstract class BaseHandler implements HttpHandler {
@FunctionalInterface
protected interface Write {
public void write(OutputStream out) throws IOException;
void write(OutputStream out) throws IOException;
}
}

View file

@ -0,0 +1,29 @@
package de.kosit.validationtool.api;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import de.kosit.validationtool.impl.Helper.Simple;
/**
* Test {@link CheckConfiguration }.
*
* @author Andreas Penski
*/
@Deprecated
public class CheckConfigurationTest {
@Test
public void testDelegation() {
final CheckConfiguration config = new CheckConfiguration(Simple.SCENARIOS);
config.setScenarioRepository(Simple.REPOSITORY_URI);
assertThat(config.getScenarios()).isNotEmpty();
assertThat(config.getContentRepository()).isNotNull();
assertThat(config.getFallbackScenario()).isNotNull();
assertThat(config.getAuthor()).isNotEmpty();
assertThat(config.getDate()).isNotEmpty();
assertThat(config.getName()).isNotEmpty();
assertThat(config.getScenarioRepository()).isNotNull();
}
}

View file

@ -17,7 +17,7 @@ import de.kosit.validationtool.impl.ResolvingMode;
*/
public class TestScenarioFactory {
static ConfigurationBuilder createSimpleConfiguration() {
public static ConfigurationBuilder createSimpleConfiguration() {
return Configuration.create().name("Simple-API").author("me").description("test desc").date(new Date())
.with(createScenario().description("awesome scenario")).with(fallback().name("default").source("report.xsl"))

View file

@ -0,0 +1,60 @@
package de.kosit.validationtool.daemon;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.OutputStream;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import de.kosit.validationtool.api.Configuration;
import de.kosit.validationtool.config.TestScenarioFactory;
import de.kosit.validationtool.impl.ConversionService;
/**
* @author Andreas Penski
*/
public class ConfigHandlerTest {
@Test
public void testApiConfiguration() throws IOException {
final HttpExchange exchange = mock(HttpExchange.class);
final Headers headers = mock(Headers.class);
final OutputStream stream = mock(OutputStream.class);
when(exchange.getResponseHeaders()).thenReturn(headers);
when(exchange.getResponseBody()).thenReturn(stream);
final Configuration config = TestScenarioFactory.createSimpleConfiguration().build();
final ConfigHandler handler = new ConfigHandler(config, new ConversionService());
handler.handle(exchange);
verify(exchange, times(1)).sendResponseHeaders(ConfigHandler.OK, 0);
verify(stream, atLeast(1)).write(any());
}
@Test
public void testError() throws IOException {
final HttpExchange exchange = mock(HttpExchange.class);
final Headers headers = mock(Headers.class);
final OutputStream stream = mock(OutputStream.class);
when(exchange.getResponseHeaders()).thenReturn(headers);
when(exchange.getResponseBody()).thenReturn(stream);
final ArgumentCaptor<Integer> valueCapture = ArgumentCaptor.forClass(Integer.class);
doNothing().when(exchange).sendResponseHeaders(valueCapture.capture(), anyLong());
final ConfigHandler handler = new ConfigHandler(null/* will produce npe */, new ConversionService());
handler.handle(exchange);
verify(headers, times(1)).add(any(), any());
verify(stream, atLeast(1)).write(any());
assertThat(valueCapture.getValue()).isEqualTo(500);
}
}

View file

@ -20,6 +20,7 @@
<scenarios xmlns="http://www.xoev.de/de/validator/framework/1/scenarios" frameworkVersion="1.1.2">
<name>HTML-TestSuite</name>
<author>QA</author>
<date>2017-08-08</date>
<description>
<p>Szenario für Tests</p>