summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/binding-atom-runtime/src/test/java/org/apache/tuscany/sca/binding/atom/ProviderServiceDocumentTestCase.java
blob: f94df3cff85ac199c2af6c440a4b272422bba793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * 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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import junit.framework.Assert;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Content;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
import org.apache.abdera.model.Service;
import org.apache.abdera.parser.Parser;
import org.apache.abdera.protocol.Response.ResponseType;
import org.apache.abdera.protocol.client.AbderaClient;
import org.apache.abdera.protocol.client.ClientResponse;
import org.apache.tuscany.sca.binding.atom.collection.Collection;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests use of service documents provided by atom binding based collections.
 * Uses the SCA provided Provider composite to act as a server.
 * Uses the Abdera provided Client to act as a client.
 * 
 * @version $Rev$ $Date$
 */
public class ProviderServiceDocumentTestCase extends AbstractProviderConsumerTestCase {
    public final static String providerURI = "http://localhost:8084/customer";

    protected static CustomerClient testService;
    protected static Abdera abdera;
    protected static AbderaClient client;
    protected static Parser abderaParser;    

    @BeforeClass
    public static void init() throws Exception {
        initTestEnvironment(AtomPostTestCase.class);

        testService = scaConsumerNode.getService(CustomerClient.class, "CustomerClient");

        abdera = new Abdera();
        client = new AbderaClient(abdera);
        abderaParser = Abdera.getNewParser();
    }

    @AfterClass
    public static void destroy() throws Exception {
        destroyTestEnvironment();
    }

    @Test
    public void testPrelim() throws Exception {
        Assert.assertNotNull(scaProviderNode);
        Assert.assertNotNull(scaConsumerNode);
        Assert.assertNotNull( client );
    }

    @Test
    public void testFeedBasics() throws Exception {		
        // Normal feed request
        ClientResponse res = client.get(providerURI);
        Assert.assertNotNull(res);
        try {
            // Assert feed provided since no predicates
            Assert.assertEquals(200, res.getStatus());
            Assert.assertEquals(ResponseType.SUCCESS, res.getType());
            // AtomTestCaseUtils.printResponseHeaders( "Feed response headers:", "   ", res );
            // System.out.println("Feed response content:");
            // AtomTestCaseUtils.prettyPrint(abdera, res.getDocument());

            // Perform other tests on feed.
            // Warning. AbderaClient.getEntityTag is very particular on tag pattern.
            // Document<Feed> doc = res.getDocument();
            String body = read( res.getInputStream() );
            // RFC 4287 requires non-null id, title, updated elements
            Assert.assertTrue( -1 != body.indexOf( "</id>" ));
            Assert.assertTrue( -1 != body.indexOf( "</title>" ));
            Assert.assertTrue( -1 != body.indexOf( "</updated>" ));
        } finally {
            res.release();
        }
    }		

    @Test
    public void testServiceDocumentGet() throws Exception {
        Collection resourceCollection = testService.getCustomerCollection();
        Assert.assertNotNull(resourceCollection);

        Entry postEntry = postEntry("Sponge Bob");
        Entry newEntry = resourceCollection.post(postEntry);
        postEntry = postEntry("Austin Powers");
        newEntry = resourceCollection.post(postEntry);
        postEntry = postEntry("Count Dracula");
        newEntry = resourceCollection.post(postEntry);

        // Service document
        ClientResponse res = client.get(providerURI + "/atomsvc");
        Assert.assertNotNull(res);
        try {
            // Asser feed provided since no predicates
            Assert.assertEquals(200, res.getStatus());
            Assert.assertEquals(ResponseType.SUCCESS, res.getType());

            // Perform other tests on feed.
            // AtomTestCaseUtils.prettyPrint(abdera, res.getDocument());	    	
            Document<Service> serviceDoc = res.getDocument();
            Service service = serviceDoc.getRoot();
            Assert.assertNotNull( service );
            org.apache.abdera.model.Collection collection =  service.getCollection( "workspace", "customers" );
            String title = collection.getTitle();
            Assert.assertEquals("customers", title);
            String href = collection.getHref().toString();
            Assert.assertTrue( href.contains( "customer") );
        } finally {
            res.release();
        }
    }

    public static void printFeed( String title, String indent, Feed feed ) {
        if ( feed == null ) {
            System.out.println( title + " feed is null");
            return;
        }

        System.out.println( title );
        System.out.println( indent + "id=" + feed.getId() );
        System.out.println( indent + "title=" + feed.getTitle() );
        System.out.println( indent + "updated=" + feed.getUpdated() );
        System.out.println( indent + "author=" + feed.getAuthor() );
        // Collection collection = feed.getCollection();
        // if ( collection == null ) {
        // 	System.out.println( indent + "collection=null" );
        // } else {
        // 	System.out.println( indent + "collection=" + collection );
        // }
        // System.out.println( indent + "collection size=" + feed.getCollection() );
        // for (Collection collection : workspace.getCollections()) {
        //    if (collection.getTitle().equals("customers")) {
        //       String expected = uri + "customers";
        //       String actual = collection.getResolvedHref().toString();
        //       assertEquals(expected, actual);
        //    }
        // }

    }

    private Entry postEntry(String value) {
        Entry entry = abdera.newEntry();
        entry.setTitle("customer " + value);

        Content content = abdera.getFactory().newContent();
        content.setContentType(Content.Type.TEXT);
        content.setValue(value);
        entry.setContentElement(content);

        return entry;
    }

    /**
     * Read response ream from the given socket.
     * @param socket
     * @return
     * @throws IOException
     */
    private static String read(InputStream inputStream) throws IOException {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader( inputStream ));
            StringBuffer sb = new StringBuffer();
            String str;
            while ((str = reader.readLine()) != null) {
                sb.append(str);
            }
            return sb.toString();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
}