com.conwet.silbops.model.AdvertiseTest.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.model.AdvertiseTest.java

Source

package com.conwet.silbops.model;

/*
 * #%L
 * SilboPS API
 * %%
 * 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.when;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.json.simple.JSONArray;
import org.junit.Before;
import org.junit.Test;

import com.conwet.silbops.model.basic.Attribute;
import com.conwet.silbops.model.basic.Type;

/**
 * Test advertise behavior
 * 
 * @author sergio
 */
public class AdvertiseTest {

    private Advertise advertise;

    @Before
    public void setUp() {

        advertise = new Advertise().attribute("attr1", Type.DOUBLE).attribute("attr2", Type.LONG).attribute("attr3",
                Type.STRING);
    }

    @Test
    public void shouldAddAttributes() {

        Attribute other = new Attribute("attr4", Type.STRING);

        advertise.attribute(other);

        assertThat(advertise.getAttributes()).contains(other);
    }

    @Test(expected = NullPointerException.class)
    public void shouldntAddNullAttribute() {

        advertise.attribute(null);
    }

    @Test(expected = NullPointerException.class)
    public void shouldntAcceptNullName() {

        advertise.attribute(null, Type.DOUBLE);
    }

    @Test(expected = NullPointerException.class)
    public void shouldntAcceptNullType() {

        advertise.attribute("anything", null);
    }

    @Test(expected = NullPointerException.class)
    public void shouldntAcceptNullNameAndType() {

        advertise.attribute(null, null);
    }

    @Test
    public void shouldCreateAdvertiseFromAttributeIterable() {

        IterableAttribute<?> iter = mock(IterableAttribute.class);
        when(iter.getAttributes()).thenReturn(advertise.getAttributes());

        assertThat(Advertise.asAdvertise(iter)).isEqualTo(advertise).isNotSameAs(advertise);
    }

    @Test
    public void shouldReturnAllAttributes() {

        Set<Attribute> expected = new HashSet<>();
        expected.add(new Attribute("attr1", Type.DOUBLE));
        expected.add(new Attribute("attr2", Type.LONG));
        expected.add(new Attribute("attr3", Type.STRING));

        assertThat(advertise.getAttributes()).isEqualTo(expected);
    }

    @Test
    public void shouldReturnAdvertiseSize() {

        assertThat(advertise.size()).isEqualTo(3);
    }

    @Test
    public void shouldExternalize() throws Exception {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutput output = new ObjectOutputStream(baos);

        output.writeObject(advertise);
        output.close();

        ObjectInput input = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));

        assertThat((Advertise) input.readObject()).isEqualTo(advertise);
    }

    @Test
    @SuppressWarnings({ "unchecked" })
    public void testJSONExport() {

        JSONArray json = new JSONArray();
        json.add("attr1:double");
        json.add("attr2:long");
        json.add("attr3:str");

        JSONArray newJSON = advertise.toJSON();
        Collections.sort(newJSON);
        Collections.sort(json);

        assertThat(newJSON).isEqualTo(json);
        assertThat(Advertise.fromJSON(json)).isEqualTo(advertise);
    }

    @Test
    public void shouldToJSONString() {

        // any permutation of ["attr1:double","attr2:long","attr3:str"]
        String jsonString = advertise.toJSONString();
        assertThat(jsonString.split(",")).hasSize(advertise.size());
        assertThat(jsonString).startsWith("[").endsWith("]").contains("\"attr1:double\"").contains("\"attr2:long\"")
                .contains("\"attr3:str\"");
    }
}