org.bonitasoft.web.designer.controller.WidgetDirectiveLoaderControllerTest.java Source code

Java tutorial

Introduction

Here is the source code for org.bonitasoft.web.designer.controller.WidgetDirectiveLoaderControllerTest.java

Source

/**
 * Copyright (C) 2015 Bonitasoft S.A.
 * Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2.0 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.bonitasoft.web.designer.controller;

import static java.nio.file.Files.readAllBytes;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(MockitoJUnitRunner.class)
public class WidgetDirectiveLoaderControllerTest {

    private MockMvc mockMvc;

    private Path widgetRepositoryPath = Paths.get("target/test-classes/workspace/widgets");

    @Before
    public void setUp() {
        WidgetDirectiveLoaderController componentLoaderController = new WidgetDirectiveLoaderController(
                widgetRepositoryPath);
        mockMvc = standaloneSetup(componentLoaderController).build();
    }

    @Test
    public void should_load_files_on_disk_according_to_asked_path() throws Exception {
        Path expectedFile = widgetRepositoryPath.resolve("pbLabel/pbLabel.js");

        mockMvc.perform(get("/runtime/widgets/pbLabel/pbLabel.js"))

                .andExpect(status().isOk()).andExpect(content().bytes(readAllBytes(expectedFile)))
                .andExpect(header().string("Content-Length", String.valueOf(expectedFile.toFile().length())))
                .andExpect(header().string("Content-Disposition", "inline; filename=\"pbLabel.js\""))
                .andExpect(content().encoding("UTF-8"));
    }

    @Test
    public void should_respond_404_not_found_when_file_is_not_found() throws Exception {

        mockMvc.perform(get("/runtime/widgets/unknownWidget/unknownWidget.js"))

                .andExpect(status().isNotFound());

    }

}