Package org.apache.wink.example.helloworld

Source Code of org.apache.wink.example.helloworld.HelloWorldTest

/*******************************************************************************
* 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;
    }
}
TOP

Related Classes of org.apache.wink.example.helloworld.HelloWorldTest

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.