com.orange.ngsi2.Utils.java Source code

Java tutorial

Introduction

Here is the source code for com.orange.ngsi2.Utils.java

Source

/*
 * Copyright (C) 2016 Orange
 *
 * 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 com.orange.ngsi2;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Utils methods for tests
 */
public class Utils {

    /**
     * Common object mapper used for most tests with Java 8 support for Optional
     */
    public final static ObjectMapper objectMapper = new ObjectMapper().registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    /**
     * Load a resource as an UTF-8 string
     * @param name name of the resource
     * @return the content of the resource
     */
    public static String loadResource(String name) {
        try {
            URL url = Utils.class.getClassLoader().getResource(name);
            if (url == null) {
                return "";
            }
            return new String(Files.readAllBytes(Paths.get(url.toURI())), "UTF-8");
        } catch (Exception e) {
            return "";
        }
    }

}