package org.ocpsoft.rewrite.annotation.query;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import org.apache.http.client.methods.HttpGet;
import org.hamcrest.Matchers;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ocpsoft.rewrite.annotation.RewriteAnnotationTest;
import org.ocpsoft.rewrite.test.HttpAction;
import org.ocpsoft.rewrite.test.RewriteTest;
import org.ocpsoft.rewrite.test.RewriteTestBase;

@RunWith(Arquillian.class)
public class QueryParameterTest extends RewriteTestBase
{

   @Deployment(testable = false)
   public static WebArchive getDeployment()
   {
      return RewriteTest.getDeploymentWithCDI()
               .addAsLibrary(RewriteAnnotationTest.getRewriteAnnotationArchive())
               .addAsLibrary(RewriteAnnotationTest.getRewriteCdiArchive())
               .addClass(QueryParameterBean.class)
               .addAsWebResource(new StringAsset(
                        "Log: [${queryParameterBean.value}]"),
                        "query.jsp");
   }

   @Test
   public void testQueryParameter() throws Exception
   {
      HttpAction<HttpGet> action = get("/query?q=foo");
      assertEquals(200, action.getStatusCode());
      assertThat(action.getResponseContent(), Matchers.containsString("Log: [foo]"));
   }

   @Test
   public void shouldFindCorrectQueryParameterIfOthersExist() throws Exception
   {
      HttpAction<HttpGet> action = get("/query?a=b&q=foo&c=d");
      assertEquals(200, action.getStatusCode());
      assertThat(action.getResponseContent(), Matchers.containsString("Log: [foo]"));
   }

}

@@ -1204,12 +1204,88 @@
 ue%7D%5D
+%5Cn%22%0A                        + %22IsNull: %5B$%7BqueryParameterBean.value == null%7D%5D
 %22),%0A
-
     
@@ -1578,24 +1578,115 @@
 : %5Bfoo%5D%22));%0A
+      assertThat(action.getResponseContent(), Matchers.containsString(%22IsNull: %5Bfalse%5D%22));%0A
    %7D%0A%0A   @Te
@@ -1877,32 +1877,32 @@
 tStatusCode());%0A
-
       assertThat
@@ -1967,20 +1967,472 @@
 : %5Bfoo%5D%22));%0A
+      assertThat(action.getResponseContent(), Matchers.containsString(%22IsNull: %5Bfalse%5D%22));%0A   %7D%0A%0A   @Test%0A   public void shouldSupportMissingQueryParameter() throws Exception%0A   %7B%0A      HttpAction%3CHttpGet%3E action = get(%22/query%22);%0A      assertEquals(200, action.getStatusCode());%0A      assertThat(action.getResponseContent(), Matchers.containsString(%22Log: %5B%5D%22));%0A      assertThat(action.getResponseContent(), Matchers.containsString(%22IsNull: %5Btrue%5D%22));%0A
    %7D%0A%0A%7D%0A

Verify that @Parameter rules work even if parameter is missing (#192)