Package org.springframework.hateoas.alps

Source Code of org.springframework.hateoas.alps.JacksonSerializationTests

/*
* Copyright 2014 the original author or authors.
*
* Licensed 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.springframework.hateoas.alps;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.hateoas.alps.Alps.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
* Unit tests for serialization of ALPS documents.
*
* @author Oliver Gierke
*/
public class JacksonSerializationTests {

  ObjectMapper mapper;

  @Before
  public void setUp() {

    mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
  }

  /**
   * @see #141
   */
  @Test
  public void writesSampleDocument() throws Exception {

    Alps alps = alps().//
        doc(doc().href("http://example.org/samples/full/doc.html").build()). //
        descriptors(Arrays.asList(//
            descriptor().id("search").type(Type.SAFE).//
                doc(new Doc("A search form with two inputs.", Format.TEXT)).//
                descriptors(Arrays.asList( //
                    descriptor().href("#resultType").build(), //
                    descriptor().id("value").name("search").type(Type.SEMANTIC).build())//
                ).build(), //
            descriptor().id("resultType").type(Type.SEMANTIC).//
                doc(doc().value("results format").build()).//
                ext(ext().href("http://alps.io/ext/range").value("summary,detail").build()//
                ).build())//
        ).build();

    assertThat(mapper.writeValueAsString(alps), is(read(new ClassPathResource("reference.json", getClass()))));
  }

  private static String read(Resource resource) throws IOException {

    Scanner scanner = null;

    try {

      scanner = new Scanner(resource.getInputStream());
      StringBuilder builder = new StringBuilder();

      while (scanner.hasNextLine()) {

        builder.append(scanner.nextLine());

        if (scanner.hasNextLine()) {
          builder.append("\n");
        }
      }

      return builder.toString();

    } finally {
      if (scanner != null) {
        scanner.close();
      }
    }
  }
}
TOP

Related Classes of org.springframework.hateoas.alps.JacksonSerializationTests

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.