JAXRSClientServerBookRecordTest.java

/**
 * 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.cxf.systest.jaxrs;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ThreadLocalRandom;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.UriBuilder;
import org.apache.cxf.Bus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
import org.apache.cxf.testutil.common.AbstractServerTestServerBase;

import org.junit.BeforeClass;
import org.junit.Test;

import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;

public class JAXRSClientServerBookRecordTest extends AbstractBusClientServerTestBase {
    public static final String PORT = BookRecordServer.PORT;

    @BeforeClass
    public static void startServers() throws Exception {
        assertTrue("server did not launch correctly", launchServer(BookRecordServer.class, true));
    }

    @Test
    public final void postBook() throws URISyntaxException {
        final String origin = String.format("Origin(%d)", ThreadLocalRandom.current().nextInt(1000));
        try (Client client = ClientBuilder
                .newBuilder()
                .build()) {

            final BookRecord book = new BookRecord(origin, 1L);

            final URI effectiveUri = UriBuilder
                .fromUri("http://localhost:" + PORT + "/")
                .path("bookstore")
                .build();

            final BookRecord response = client
                .target(effectiveUri)
                .request(APPLICATION_JSON_TYPE)
                .buildPost(Entity.entity(book, APPLICATION_JSON_TYPE))
                .invoke(BookRecord.class);

            assertThat(response.name(), is(origin + ",BookResource"));
        }
    }
    
    @Test
    public final void getBook() throws URISyntaxException {
        try (Client client = ClientBuilder
                .newBuilder()
                .build()) {

            final URI effectiveUri = UriBuilder
                .fromUri("http://localhost:" + PORT + "/")
                .path("bookstore/sub")
                .build();

            final BookRecord response = client
                .target(effectiveUri)
                .request(APPLICATION_JSON_TYPE)
                .buildGet()
                .invoke(BookRecord.class);

            assertThat(response.name(), is("BookResourceSub"));
        }
    }

    public static final class BookRecordServer extends AbstractServerTestServerBase {
        public static final String PORT = allocatePort(BookRecordServer.class);

        @Override
        protected Server createServer(Bus bus) throws Exception {
            JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
            sf.setResourceClasses(BookStoreResource.class);
            sf.setAddress("http://localhost:" + PORT + "/");
            return sf.create();
        }

        public static void main(String[] args) throws Exception {
            new BookRecordServer().start();
        }
    }

    @Path("/bookstore")
    public static class BookStoreResource {
        @POST
        @Consumes(APPLICATION_JSON)
        @Produces(APPLICATION_JSON)
        public BookRecord echo(final BookRecord book) {
            return new BookRecord(String.join(",", book.name(), "BookResource"), book.id());
        }
        
        @Path("/sub")
        public BookRecord getBookRecord() {
            return new BookRecord("BookResourceSub", 1L);
        }
    }
}