Package org.apache.tuscany.sca.binding.atom

Source Code of org.apache.tuscany.sca.binding.atom.AtomFeedNonCollectionTest

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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.apache.tuscany.sca.binding.atom;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import junit.framework.Assert;

import org.apache.abdera.model.Link;
import org.apache.tuscany.sca.binding.atom.collection.Collection;
import org.apache.tuscany.sca.data.collection.Entry;
import org.apache.tuscany.sca.data.collection.Item;
import org.apache.tuscany.sca.node.Contribution;
import org.apache.tuscany.sca.node.ContributionLocationHelper;
import org.apache.tuscany.sca.node.Node;
import org.apache.tuscany.sca.node.NodeFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Test cases for using an Atom feed that does not implement
* the Collections interface but does have a getAll() method.
*
* @version $Rev: 951651 $ $Date: 2010-06-05 06:17:47 +0100 (Sat, 05 Jun 2010) $
*/
public class AtomFeedNonCollectionTest {
    /**
     * Used to generate unique IDs for the feed entries.
     */
    private static final AtomicInteger ID_GEN = new AtomicInteger();

    /**
     * Data used for creating test feed entries.
     */
    private static final String[] FEED_ENTRY_TITLES = {"Apache Tuscany", "Apache"};

    /**
     * Data used for creating test feed entries.
     */
    private static final String[] FEED_ENTRY_CONTENT = {"This is Apache Tuscany", "This is Apache"};

    /**
     * Data used for creating test feed entries.
     */
    private static final String[] FEED_ENTRY_LINK = {"http://tuscany.apache.org", "http://www.apache.org"};

    /**
     * The number of test feed entries.
     */
    private static final int FEED_ENTRY_COUNT = FEED_ENTRY_TITLES.length;

    protected static Node scaProviderNode;
    protected static Node scaConsumerNode;

    private static CustomerClient testService;

    @BeforeClass
    public static void init() throws Exception {
        try {
            //System.out.println(">>>AtomFeedNonCollectionTest.init entry");

            String contribution = ContributionLocationHelper.getContributionLocation(AtomFeedNonCollectionTest.class);

            scaProviderNode = NodeFactory.newInstance().createNode("org/apache/tuscany/sca/binding/atom/ProviderNonCollection.composite", new Contribution("provider", contribution));
            scaProviderNode.start();

            scaConsumerNode = NodeFactory.newInstance().createNode("org/apache/tuscany/sca/binding/atom/Consumer.composite", new Contribution("consumer", contribution));
            scaConsumerNode.start();

            testService = scaConsumerNode.getService(CustomerClient.class, "CustomerClient");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterClass
    public static void destroy() throws Exception {
        if (scaConsumerNode != null) {
            scaConsumerNode.stop();
        }
        if (scaProviderNode != null) {
            scaProviderNode.stop();
        }
    }

    /**
     * Make sure everything has been initialized correctly.
     */
    @SuppressWarnings("unchecked")
    @Before
    public void testPrelim() {
        Assert.assertNotNull(scaConsumerNode);
        Assert.assertNotNull(scaProviderNode);
        Assert.assertNotNull(testService);

        // Add some entries to the Atom feed
        final Entry<Object, Object>[] testEntries = new Entry[FEED_ENTRY_COUNT];
        for (int i = 0; i < FEED_ENTRY_COUNT; i++) {
            testEntries[i] = createFeedEntry(FEED_ENTRY_TITLES[i], FEED_ENTRY_CONTENT[i], FEED_ENTRY_LINK[i]);
        }
        CustomerNonCollectionImpl.entries = testEntries;
    }

    /**
     * Post test cleanup
     */
    @After
    public void testPostCleanup()
    {
        // Clear any old feed entries that have been added by previous tests
        CustomerNonCollectionImpl.entries = null;
    }

    /**
     * Test that we can retrieve entries from a feed that does not implement
     * the Collection interface.
     */
    @Test
    public void testThatCanGetFeedEntriesFromNonCollectionImplementation() {
        // Get the entries from the feed
        final Collection resourceCollection = testService.getCustomerCollection();
        Assert.assertNotNull(resourceCollection);
        final List<org.apache.abdera.model.Entry> entries = resourceCollection.getFeed().getEntries();

        // Validate the feed entries
        Assert.assertNotNull(entries);
        Assert.assertEquals(FEED_ENTRY_COUNT, entries.size());
        for (int i = 0; i < FEED_ENTRY_COUNT; i++) {
            final org.apache.abdera.model.Entry entry = entries.get(i);
            Assert.assertEquals(FEED_ENTRY_TITLES[i], entry.getTitle());
            Assert.assertEquals(FEED_ENTRY_CONTENT[i], entry.getContent());
            final List<Link> links = entry.getLinks();
            Assert.assertNotNull(links);
            Assert.assertEquals(1, links.size());
            final Link link = links.get(0);
            final String linkStr = link.getHref().toString();
            Assert.assertEquals(FEED_ENTRY_LINK[i], linkStr);
        }
    }

    /**
     * Tests that the title of the feed can be set by the title
     * attribute on the binding.atom
     */
    @Test
    public void testThatFeedTitleSet() {
        final String expectedFeedTitle = "Atom binding Non Collection";

        // Get the title of the feed
        final Collection resourceCollection = testService.getCustomerCollection();
        Assert.assertNotNull(resourceCollection);
        final String feedTitle = resourceCollection.getFeed().getTitle();

        // Validate the title
        Assert.assertEquals(expectedFeedTitle, feedTitle);
    }

    /**
     * Tests that the description of the feed can be set by the description
     * attribute on the binding.atom
     */
    @Test
    public void testThatFeedDescriptionSet() {
        final String expectedFeedDescription = "Feed used for unit testing";

        // Get the description of the feed
        final Collection resourceCollection = testService.getCustomerCollection();
        Assert.assertNotNull(resourceCollection);
        final String feedDescription = resourceCollection.getFeed().getSubtitle();

        // Validate the description
        Assert.assertEquals(expectedFeedDescription, feedDescription);
    }

    /**
     * Creates a feed entry.
     *
     * @param title Title for the feed entry
     * @param content Contents of the feed entry
     * @param link Link for the feed entry
     * @return A new feed entry.
     */
    private Entry<Object, Object> createFeedEntry(String title, String content, String link) {
        final Item item = new Item(title, content, link, null, new Date());
        final Entry<Object, Object> entry = new Entry<Object, Object>(nextFeedID(), item);
        return entry;
    }

    /**
     * Generates the feed entry ID.
     *
     * @return Next feed entry ID
     */
    private String nextFeedID() {
        return Integer.toString(ID_GEN.incrementAndGet());
    }
}
TOP

Related Classes of org.apache.tuscany.sca.binding.atom.AtomFeedNonCollectionTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.