package com.github.valdr;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;

/**
 * Tests ValidationRulesServlet.
 */
public class ValidationRulesServletTest {

  private final ValidationRulesServlet servlet = new ValidationRulesServlet();
  private ServletConfig servletConfig;

  @Before
  public void setUp() throws Exception {
    servletConfig = mock(ServletConfig.class);
  }

  /**
   * See method name.
   */
  @Test
  public void shouldThrowExceptionIfNoConfigFileExists  () throws ServletException {
    // given
    given(servletConfig.getInitParameter("configFile")).willReturn("");

    // when
    try {
      servlet.init(servletConfig);
      fail("Should throw IllegalArgumentException if no config file exists.");
    } catch (IllegalArgumentException e) {
      // expected
    }
  }

  /**
   * See method name.
   */
  @Test
  public void shouldUseCustomConfigFileIfProvided() throws IOException, ServletException {
    // given
    String configFile = createTempFile("{\n" +
      "  \"modelPackages\": [\"com.github.valdr.model.b\"],\n" +
      "  \"corsAllowOriginPattern\": \"abc.com\"\n" +
      "}");

    given(servletConfig.getInitParameter("configFile")).willReturn(configFile);

    // when
    servlet.init(servletConfig);

    // then
    assertThat(ReflectionTestUtils.getField(servlet, "correctlyConfigured").toString(), is("true"));
    assertThat(ReflectionTestUtils.getField(servlet, "corsAllowOriginPattern").toString(), is("abc.com"));
  }

  private String createTempFile(String string) throws IOException {
    File tempFile = File.createTempFile("valdr", "json");
    FileWriter writer = new FileWriter(tempFile);
    IOUtils.write(string, writer);
    IOUtils.closeQuietly(writer);
    return tempFile.getAbsolutePath();
  }
}

@@ -106,16 +106,137 @@
 t.Test;%0A
+import org.springframework.mock.web.MockHttpServletRequest;%0Aimport org.springframework.mock.web.MockHttpServletResponse;%0A
 import o
@@ -438,16 +438,68 @@
 ption;%0A%0A
+import static org.hamcrest.Matchers.containsString;%0A
 import s
@@ -695,16 +695,61 @@
 to.mock;
+%0Aimport static org.mockito.Matchers.contains;
 %0A%0A/**%0A *
@@ -943,16 +943,102 @@
 tConfig;
+%0A  private MockHttpServletResponse response;%0A  private MockHttpServletRequest request;
 %0A%0A  @Bef
@@ -1129,16 +1129,106 @@
 class);%0A
+    response = new MockHttpServletResponse();%0A    request = new MockHttpServletRequest();%0A
   %7D%0A%0A  /
@@ -1320,18 +1320,16 @@
 leExists
-  
 () throw
@@ -1797,42 +1797,30 @@
     
-String configFile = createTempFile
+givenThisConfiguration
 (%22%7B%5C
@@ -1960,89 +1960,8 @@
 );%0A%0A
-    given(servletConfig.getInitParameter(%22configFile%22)).willReturn(configFile);%0A%0A
     
@@ -2227,16 +2227,1207 @@
 );%0A  %7D%0A%0A
+  /**%0A   * See method name.%0A   */%0A  @Test%0A  public void shouldSendErrorIfNotCorrectlyConfigured() throws IOException, ServletException %7B%0A    // given mandatory 'modelPackages' is missing%0A    givenThisConfiguration(%22%7B%5Cn%22 +%0A      %22  %5C%22corsAllowOriginPattern%5C%22: %5C%22abc.com%5C%22%5Cn%22 +%0A      %22%7D%22);%0A    servlet.init(servletConfig);%0A%0A    // when%0A    servlet.doGet(request, response);%0A%0A    // then%0A    assertThat(response.getStatus(), is(500));%0A    assertThat(response.getErrorMessage(), containsString(%22Invalid configuration%22));%0A  %7D%0A%0A  /**%0A   * See method name.%0A   */%0A  @Test%0A  public void shouldSendHttp200IfAllIsWell() throws IOException, ServletException %7B%0A    // given%0A    givenThisConfiguration(%22%7B%5Cn%22 +%0A      %22  %5C%22modelPackages%5C%22: %5B%5C%22com.github.valdr.model.b%5C%22%5D,%5Cn%22 +%0A      %22  %5C%22corsAllowOriginPattern%5C%22: %5C%22abc.com%5C%22%5Cn%22 +%0A      %22%7D%22);%0A    servlet.init(servletConfig);%0A%0A    // when%0A    servlet.doGet(request, response);%0A%0A    // then%0A    assertThat(response.getStatus(), is(200));%0A  %7D%0A%0A  private void givenThisConfiguration(String jsonString) throws IOException %7B%0A    String configFile = createTempFile(jsonString);%0A    given(servletConfig.getInitParameter(%22configFile%22)).willReturn(configFile);%0A  %7D%0A%0A
   privat

Add more tests for Servlet.