summaryrefslogtreecommitdiffstats
path: root/collaboration/wink-helloworld-sca/src/test/java/org/apache/wink/example/helloworld/HelloWorldTest.java
blob: ed121ffe29a1670ac784e325a577285f06a756a4 (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
/*******************************************************************************
 * 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.wink.example.helloworld;

import java.io.FileNotFoundException;
import java.io.StringReader;
import java.util.Set;

import org.apache.wink.common.http.HttpStatus;
import org.apache.wink.common.internal.application.ApplicationFileLoader;
import org.apache.wink.common.internal.utils.MediaTypeUtils;
import org.apache.wink.common.model.atom.AtomEntry;
import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

/**
 * Test of response from HelloWorld Resource.
 */
public class HelloWorldTest extends MockServletInvocationTest {

    @Override
    protected Class<?>[] getClasses() {
        try {
            Set<Class<?>> classes = new ApplicationFileLoader("application").getClasses();
            Class<?>[] classesArray = new Class[classes.size()];
            return classes.toArray(classesArray);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public void testHelloWorld() throws Exception {
        // prepare a mock request and an empty mock response
        MockHttpServletRequest request =
            constructMockRequest("GET", "/world", MediaTypeUtils.ATOM_ENTRY);
        MockHttpServletResponse response = invoke(request);

        // check resulting mock response
        assertEquals("HTTP status", HttpStatus.OK.getCode(), response.getStatus());
        AtomEntry entry = AtomEntry.unmarshal(new StringReader(response.getContentAsString()));
        String id = entry.getId();
        assertEquals("entry id", HelloWorld.ID, id);
    }

    // test helper
    private MockHttpServletRequest constructMockRequest(String method,
                                                        String requestURI,
                                                        String acceptHeader) {
        MockHttpServletRequest mockRequest = new MockHttpServletRequest() {

            public String getPathTranslated() {
                return null; // prevent Spring to resolve the file on the
                             // filesystem which fails
            }

        };
        mockRequest.setMethod(method);
        mockRequest.setRequestURI(requestURI);
        mockRequest.addHeader("Accept", acceptHeader);
        return mockRequest;
    }
}