curly.artifactory.ArtifactoryTestHelper.java Source code

Java tutorial

Introduction

Here is the source code for curly.artifactory.ArtifactoryTestHelper.java

Source

/*
 *        Copyright 2015 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 curly.artifactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.curly.artifact.ArtifactoryApplication;
import io.curly.artifact.model.Artifact;
import io.curly.artifact.model.Category;
import io.curly.artifact.model.Language;
import io.curly.artifact.model.Tag;
import io.curly.commons.github.User;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.mock.http.MockHttpOutputMessage;
import org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Joao Pedro Evangelista
 */
@DirtiesContext
@RunWith(SpringJUnit4ClassRunner.class)
@OAuth2ContextConfiguration
@SpringApplicationConfiguration(classes = { ArtifactoryApplication.class })
@WebIntegrationTest
public abstract class ArtifactoryTestHelper {

    public static String json(Object o, HttpMessageConverter<Object> httpMessageConverter) {
        MockHttpOutputMessage message = new MockHttpOutputMessage();
        try {
            httpMessageConverter.write(o, jsonMediaType(), message);
        } catch (IOException ignore) {
        }
        return message.getBodyAsString();
    }

    public static MediaType jsonMediaType() {
        return new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(),
                Charset.forName("utf-8"));
    }

    public static Artifact createArtifact(MongoTemplate mongoTemplate) {
        mongoTemplate.findAllAndRemove(new Query(), Artifact.class);
        Artifact artifact = new Artifact();
        Set<Language> languages = new HashSet<>(0);
        Set<Tag> tags = new HashSet<>(0);
        tags.add(new Tag("document"));
        tags.add(new Tag("nosql"));
        languages.add(new Language("java"));
        languages.add(new Language("groovy"));
        languages.add(new Language("ruby"));
        languages.add(new Language("scala"));
        languages.add(new Language("javascript"));
        artifact.setName("curly");
        artifact.setDescription("a hobby project");
        artifact.setAuthor("joaoevangelista");
        artifact.setCategory(new Category("database"));
        artifact.setHomePage("http://example.com");
        artifact.setIncubation(LocalDate.now().toString());
        artifact.setLanguages(languages);
        artifact.setTags(tags);
        artifact.setOwner("6969");
        mongoTemplate.insert(artifact);
        Assert.assertTrue(artifact.getId() != null);
        try {
            System.out.println(new ObjectMapper().writeValueAsString(artifact));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return artifact;
    }

    public User user() {
        return User.builder().id("6969").build();
    }
}