package org.motechproject.sms.it;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.motechproject.sms.audit.SmsRecordsDataService;
import org.motechproject.sms.service.OutgoingSms;
import org.motechproject.testing.osgi.BasePaxIT;
import org.motechproject.testing.utils.TestContext;
import org.motechproject.testing.osgi.container.MotechNativeTestContainerFactory;
import org.motechproject.testing.osgi.http.SimpleHttpClient;
import org.ops4j.pax.exam.ExamFactory;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerSuite;

import javax.inject.Inject;
import java.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.assertTrue;

/**
 * Verify SendController present & functional.
 */
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerSuite.class)
@ExamFactory(MotechNativeTestContainerFactory.class)
public class SendControllerBundleIT extends BasePaxIT {
    @Inject
    private SmsRecordsDataService smsRecordsDataService;


    @Before
    public void waitForBeans() {
        // To prevent the annoying "BeanFactory not initialized or already closed" errors
        try { Thread.sleep(SmsIntegrationTests.BUNDLE_MS_WAIT_TIME); } catch (InterruptedException e) {  }
    }

    @Before
    public void createUser() {
        try {
            createAdminUser();
        } catch (IOException|InterruptedException e) {
            getLogger().error("Unable to create the admin user needed for this test: {}", e.getMessage());
        }
    }

    @Test
    public void verifyFunctional() throws Exception {
        OutgoingSms outgoingSms = new OutgoingSms("foo", Arrays.asList("12065551212"), "hello, world");
        ObjectMapper mapper = new ObjectMapper();
        String outgoingSmsJson = mapper.writeValueAsString(outgoingSms);

        URIBuilder builder = new URIBuilder();
        builder.setScheme("http").setHost("localhost").setPort(TestContext.getJettyPort())
                .setPath("/sms/send");

        HttpPost httpPost = new HttpPost(builder.build());
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setEntity(new StringEntity(outgoingSmsJson));

        // We're specifying a nonexistent config so the controller should respond with a 404
        assertTrue(SimpleHttpClient.execHttpRequest(httpPost, HttpStatus.SC_NOT_FOUND, MOTECH_ADMIN_USERNAME,
                MOTECH_ADMIN_PASSWORD));

        //TODO: figure out how to create configs an then use them to "pretend send" using an SimpleHttpServer that
        //TODO: responds the way an SMS provider would.
    }
}

@@ -55,21 +55,154 @@
 ttp.Http
-Statu
+Response;%0Aimport org.apache.http.HttpStatus;%0Aimport org.apache.http.auth.AuthScope;%0Aimport org.apache.http.auth.UsernamePasswordCredential
 s;%0Aimpor
@@ -624,60 +624,8 @@
 IT;%0A
-import org.motechproject.testing.utils.TestContext;%0A
 impo
@@ -739,33 +739,24 @@
 ing.
-osgi.http.SimpleHttpClien
+utils.TestContex
 t;%0Ai
@@ -1000,23 +1000,31 @@
 eption;%0A
+%0A
 import 
+static 
 java.uti
@@ -1029,17 +1029,80 @@
 til.
-Array
+Collections.singletonList;%0Aimport static org.junit.Assert.assertEqual
 s;%0A
-%0A
 impo
@@ -1134,20 +1134,23 @@
 t.assert
-True
+NotNull
 ;%0A%0A/**%0A 
@@ -2081,17 +2081,17 @@
 o%22, 
-Arrays.as
+singleton
 List
@@ -2718,25 +2718,210 @@
     
-assertTrue(Simple
+getHttpClient().getCredentialsProvider().setCredentials(AuthScope.ANY,%0A                new UsernamePasswordCredentials(MOTECH_ADMIN_USERNAME, MOTECH_ADMIN_PASSWORD));%0A        HttpResponse response = get
 Http
@@ -2930,24 +2930,18 @@
 ient
+()
 .exec
-HttpRequest
+ute
 (htt
@@ -2974,70 +2974,130 @@
 OUND
-, MOTECH_ADMIN_USERNAME,%0A                MOTECH_ADMIN_PASSWORD
+);%0A%0A        assertNotNull(response);%0A        assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusLine().getStatusCode(
 ));%0A

Fix SendControllerBundleIT