/*
 * Copyright (c) 2020 - Manifold Systems LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package manifold.api.json;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import abc.Dummy;

import static org.junit.Assert.assertEquals;

public class RequesterTest
{
  @BeforeClass
  public static void init() {
    TestServer.main(new String[0]);
  }

  @AfterClass
  public static void destroy() {
    TestServer.stop();
  }

  @Test
  public void httpPostRequestWithParams()
  {
    Requester<Dummy> req = Dummy.request( "http://localhost:4567/" )
      .withParam( "foo", "bar" )
      .withParam( "abc", "8" );
    Object queryString = req.postOne( "testPost_QueryString", Dummy.create(), Requester.Format.Text );
    assertEquals( "foo=bar&abc=8", queryString );
  }

  @Test
  public void httpGetRequestWithParams()
  {
    Requester<Dummy> req = Dummy.request( "http://localhost:4567/" )
      .withParam( "foo", "bar" )
      .withParam( "abc", "8" );
    Object queryString = req.getOne( "testGet_QueryString", Dummy.create(), Requester.Format.Text );
    assertEquals( "foo=bar&abc=8", queryString );
  }

  @Test
  public void httpPostRequestWithParameterizedUrlSuffixWithParams()
  {
    Requester<Dummy> req = Dummy.request( "http://localhost:4567/" )
      .withParam( "foo", "bar" )
      .withParam( "abc", "8" );
    Object queryString = req.postOne( "testPost_QueryString?firstParam=firstValue", Dummy.create(), Requester.Format.Text );
    assertEquals( "firstParam=firstValue&foo=bar&abc=8", queryString );
  }

  @Test
  public void httpGetRequestWithParameterizedUrlSuffixWithParams()
  {
    Requester<Dummy> req = Dummy.request( "http://localhost:4567/" )
      .withParam( "foo", "bar" )
      .withParam( "abc", "8" );
    Object queryString = req.getOne( "testGet_QueryString?firstParam=firstValue", Dummy.create(), Requester.Format.Text );
    assertEquals( "firstParam=firstValue&foo=bar&abc=8", queryString );
  }
}

@@ -734,16 +734,36 @@
 c.Dummy;
+%0Aimport spark.Spark;
 %0A%0Aimport
@@ -873,16 +873,18 @@
 d init()
+%0A 
  %7B%0A    T
@@ -914,16 +914,49 @@
 ng%5B0%5D);%0A
+    Spark.awaitInitialization();%0A
   %7D%0A%0A  @

fix test breaks on CircleCI, wait for Spark server to initialize before tests run