com.conwet.silbops.api.impl.ConnectionFactoryImplTest.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.api.impl.ConnectionFactoryImplTest.java

Source

package com.conwet.silbops.api.impl;

/*
 * #%L
 * SilboPS API - XJSP binding
 * %%
 * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOError;
import java.net.URI;

import org.json.simple.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.conwet.silbops.api.AdvertiseListener;
import com.conwet.silbops.api.IConnectionFactory;
import com.conwet.silbops.api.SubEndpoint;
import com.conwet.silbops.model.Advertise;
import com.conwet.silbops.model.MessageType;
import com.conwet.xjsp.features.FeatureHandler;
import com.conwet.xjsp.features.Message;

public class ConnectionFactoryImplTest {

    private IConnectionFactory factory;
    private MockServer server;

    @Before
    public void setUp() throws Exception {

        server = new MockServer();
        server.start();
        factory = new ConnectionFactoryImpl(URI.create("silbops://localhost:" + server.getServerPort()));
    }

    @After
    public void teadDown() {

        server.stop();
    }

    @Test
    public void shouldCreateFactoryWithDefaultPort() {

        factory = new ConnectionFactoryImpl(URI.create("silbops://localhost"));
        assertThat(factory).isNotNull();
    }

    private void checkEndPoint(Object endpoint, String featureName) {

        assertThat(endpoint).isNotNull();
        assertThat(server.getRequest()).contains("ps=silbops-" + featureName).contains("xjsp=xjsp");
    }

    @Test
    public void shouldCreatePublisher() throws Exception {

        checkEndPoint(factory.openPubEndpoint(), "publisher");
    }

    @Test
    public void shouldCreateSubscriber() {

        checkEndPoint(factory.openSubEndpoint(), "subscriber");
    }

    @Test
    @SuppressWarnings("unchecked")
    public void shouldCreateSubscriberWithAdvertiseListerner() throws Exception {

        Advertise advertise = new Advertise();
        JSONObject adv = new JSONObject();
        adv.put("advertise", advertise.toJSON());
        AdvertiseListener listener = mock(AdvertiseListener.class);
        SubEndpoint endpoint = factory.openSubEndpoint(listener);

        checkEndPoint(endpoint, "subscriber");

        Message message = mock(Message.class);
        when(message.getMessageType()).thenReturn(MessageType.ADVERTISE.name());
        when(message.getPayload()).thenReturn(adv);

        // we know we can do this safely
        FeatureHandler handler = (FeatureHandler) endpoint;
        handler.handleMessage(message, null);

        verify(listener).receiveAdvertise(advertise);
    }

    @Test(expected = IOError.class)
    public void shouldntConnectPublisher() {

        server.stop();
        factory.openPubEndpoint();
    }

    @Test(expected = IOError.class)
    public void shouldntConnectSubscriber() {

        server.stop();
        factory.openSubEndpoint();
    }
}