/**
 *
 * Copyright 2015 Florian Schmaus
 *
 * 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 org.jivesoftware.smack;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import org.igniterealtime.smack.inttest.AbstractSmackLowLevelIntegrationTest;
import org.igniterealtime.smack.inttest.Configuration;
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.TestNotPossibleException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.junit.AfterClass;
import org.junit.BeforeClass;

public class StreamManagementTest extends AbstractSmackLowLevelIntegrationTest {

    public StreamManagementTest(Configuration configuration, String testRunId)
                    throws Exception {
        super(configuration, testRunId);
        performCheck(new ConnectionCallback() {
            @Override
            public void connectionCallback(XMPPTCPConnection connection) throws Exception {
                if (!connection.isSmAvailable()) {
                    throw new TestNotPossibleException("XEP-198: Stream Mangement not supported by service");
                }
            }
        });
    }

    @BeforeClass
    public static void before() {
        // TODO remove this once stream mangement is enabled per default
        XMPPTCPConnection.setUseStreamManagementDefault(true);
    }

    @AfterClass
    public static void after() {
        XMPPTCPConnection.setUseStreamManagementDefault(false);
    }

    @SmackIntegrationTest
    public void testStreamManagement(XMPPTCPConnection conOne, XMPPTCPConnection conTwo) throws InterruptedException, KeyManagementException,
                    NoSuchAlgorithmException, SmackException, IOException, XMPPException,
                    TestNotPossibleException {
        send("Hi, what's up?", conOne, conTwo);

        conOne.instantShutdown();

        send("Hi, what's up? I've been just instantly shutdown", conOne, conTwo);

        // Reconnect with xep198
        conOne.connect();

        send("Hi, what's up? I've been just resumed", conOne, conTwo);
        // TODO check that all messages where received
    }

    private static void send(String messageString, XMPPConnection from, XMPPConnection to)
                    throws NotConnectedException, InterruptedException {
        Message message = new Message(to.getUser());
        message.setBody(messageString);
        from.sendStanza(message);
    }
}

@@ -629,16 +629,108 @@
 smack;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertNotNull;%0A%0A
 import j
@@ -1163,32 +1163,198 @@
 ectedException;%0A
+import org.jivesoftware.smack.filter.AndFilter;%0Aimport org.jivesoftware.smack.filter.FromMatchesFilter;%0Aimport org.jivesoftware.smack.filter.MessageWithBodiesFilter;%0A
 import org.jives
@@ -2742,147 +2742,633 @@
     
-send(%22Hi, what's up?%22, conOne, conTwo);%0A%0A        conOne.
+final String body1 = %22Hi, what's up? %22 + testRunId;%0A        final String body2 = %22Hi, what's up? I've been just 
 instant
-S
+ly s
 hutdown
-();%0A%0A        send(%22Hi, what's up? I've been just instantly shutdown%22
+%22 + testRunId;%0A        final String body3 = %22Hi, what's up? I've been just resumed%22 + testRunId;%0A%0A        final PacketCollector collector = conTwo.createPacketCollector(new AndFilter(%0A                        MessageWithBodiesFilter.INSTANCE,%0A                        FromMatchesFilter.createFull(conOne.getUser())));%0A%0A        try %7B%0A            send(body1, conOne, conTwo);%0A            assertMessageWithBodyReceived(body1, collector);%0A%0A            conOne.instantShutdown();%0A%0A            send(body2
 , co
@@ -3391,16 +3391,20 @@
         
+    
 // Recon
@@ -3420,16 +3420,20 @@
  xep198%0A
+    
         
@@ -3452,136 +3452,251 @@
 ct()
-;%0A%0A        send(%22Hi, what's up? I've been just resumed%22, conOne, conTwo);%0A        // TODO check that all messages where received
+.login();%0A            assertMessageWithBodyReceived(body2, collector);%0A%0A            send(body3, conOne, conTwo);%0A            assertMessageWithBodyReceived(body3, collector);%0A        %7D%0A        finally %7B%0A            collector.cancel();%0A        %7D
 %0A   
@@ -3995,11 +3995,271 @@
 );%0A    %7D
+%0A%0A    private static void assertMessageWithBodyReceived(String body, PacketCollector collector) throws InterruptedException %7B%0A        Message message = collector.nextResult();%0A        assertNotNull(message);%0A        assertEquals(body, message.getBody());%0A    %7D
 %0A%7D%0A

Fix StreamManagementTest in smack-integration