// Path: src/main/java/net/eusashead/iot/mqtt/paho/DisconnectFactory.java
// static final class DisconnectActionListener
//         extends CompletableEmitterMqttActionListener {
// 
//     public DisconnectActionListener(final CompletableEmitter emitter) {
//         super(emitter);
//     }
// 
//     @Override
//     public void onSuccess(final IMqttToken asyncActionToken) {
//         this.emitter.onComplete();
//     }
// }


import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Matchers;
import org.mockito.Mockito;
import io.reactivex.Completable;
import io.reactivex.CompletableEmitter;
import net.eusashead.iot.mqtt.paho.DisconnectFactory.DisconnectActionListener;
import static org.hamcrest.Matchers.isA;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
package net.eusashead.iot.mqtt.paho;

/*
 * #[license]
 * rxmqtt
 * %%
 * Copyright (C) 2013 - 2016 Eusa's Head
 * %%
 * 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.
 * %[license]
 */




@RunWith(JUnit4.class)
public class DisconnectFactoryTest {

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Test
    public void whenCreateIsCalledThenAnObservableIsReturned()
            throws Exception {
        // Given
        final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
        final DisconnectFactory factory = new DisconnectFactory(client);
        final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
                .forClass(IMqttActionListener.class);

        // When
        final Completable obs = factory.create();

        // Then
        Assert.assertNotNull(obs);
        obs.subscribe();
        Mockito.verify(client).disconnect(Matchers.isNull(),
                actionListener.capture());
        Assert.assertTrue(actionListener
// Path: src/main/java/net/eusashead/iot/mqtt/paho/DisconnectFactory.java
// static final class DisconnectActionListener
//         extends CompletableEmitterMqttActionListener {
// 
//     public DisconnectActionListener(final CompletableEmitter emitter) {
//         super(emitter);
//     }
// 
//     @Override
//     public void onSuccess(final IMqttToken asyncActionToken) {
//         this.emitter.onComplete();
//     }
// }


// Path: src/test/java/net/eusashead/iot/mqtt/paho/DisconnectFactoryTest.java
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Matchers;
import org.mockito.Mockito;
import io.reactivex.Completable;
import io.reactivex.CompletableEmitter;
import net.eusashead.iot.mqtt.paho.DisconnectFactory.DisconnectActionListener;
import static org.hamcrest.Matchers.isA;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

package net.eusashead.iot.mqtt.paho;

/*
 * #[license]
 * rxmqtt
 * %%
 * Copyright (C) 2013 - 2016 Eusa's Head
 * %%
 * 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.
 * %[license]
 */




@RunWith(JUnit4.class)
public class DisconnectFactoryTest {

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Test
    public void whenCreateIsCalledThenAnObservableIsReturned()
            throws Exception {
        // Given
        final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
        final DisconnectFactory factory = new DisconnectFactory(client);
        final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
                .forClass(IMqttActionListener.class);

        // When
        final Completable obs = factory.create();

        // Then
        Assert.assertNotNull(obs);
        obs.subscribe();
        Mockito.verify(client).disconnect(Matchers.isNull(),
                actionListener.capture());
        Assert.assertTrue(actionListener
                .getValue() instanceof DisconnectFactory.DisconnectActionListener);