Search in sources :

Example 11 with OrchidContext

use of com.eden.orchid.api.OrchidContext in project Orchid by JavaEden.

the class GeneratorServiceTest method setUp.

@BeforeEach
public void setUp() {
    context = mock(OrchidContext.class);
    extractor = mock(OptionsExtractor.class);
    theme = mock(Theme.class);
    buildMetrics = new BuildMetrics(context);
    internalIndex = new OrchidRootIndex(context, "internal");
    when(context.resolve(OptionsExtractor.class)).thenReturn(extractor);
    when(context.findTheme(any())).thenReturn(theme);
    when(context.getIndex()).thenReturn(internalIndex);
    when(context.includeDrafts()).thenReturn(false);
    when(context.getEmbeddedData(anyString(), anyString())).thenReturn(new EdenPair<>("", new HashMap<>()));
    generators = new HashSet<>();
    mockPage1Reference = new OrchidReference(context, "page1.html");
    mockPage1Resource = new StringResource(mockPage1Reference, "");
    mockPage1 = spy(new OrchidPage(mockPage1Resource, RenderService.RenderMode.TEMPLATE, "mockPage1", ""));
    pages1 = new ArrayList<>();
    pages1.add(mockPage1);
    generator1 = spy(new MockGenerator("gen1", OrchidGenerator.Stage.COLLECTION, pages1));
    generators.add(generator1);
    mockPage2Reference = new OrchidReference(context, "page2.html");
    mockFreeableResource = spy(new OrchidResource(mockPage2Reference) {

        @Nonnull
        @Override
        public InputStream getContentStream() {
            return OrchidExtensionsKt.asInputStream("");
        }
    });
    mockPage2 = spy(new OrchidPage(mockFreeableResource, RenderService.RenderMode.TEMPLATE, "mockPage2", ""));
    pages2 = new ArrayList<>();
    pages2.add(mockPage2);
    generator2 = spy(new MockGenerator("gen2", OrchidGenerator.Stage.CONTENT, pages2));
    generators.add(generator2);
    pages3 = new ArrayList<>();
    generator3 = new MockGenerator("gen3", OrchidGenerator.Stage.WARM_UP, pages3);
    generator3 = spy(generator3);
    generators.add(generator3);
    when(context.resolveSet(OrchidGenerator.class)).thenReturn(generators);
    // test the service directly
    service = new GeneratorServiceImpl();
    service.initialize(context);
    // test that the default implementation is identical to the real implementation
    underTest = new GeneratorService() {

        public void initialize(OrchidContext context) {
        }

        public <T extends OrchidService> T getService(Class<T> serviceClass) {
            return (T) service;
        }
    };
}
Also used : OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) OrchidContext(com.eden.orchid.api.OrchidContext) HashMap(java.util.HashMap) OptionsExtractor(com.eden.orchid.api.options.OptionsExtractor) StringResource(com.eden.orchid.api.resources.resource.StringResource) OrchidReference(com.eden.orchid.api.theme.pages.OrchidReference) OrchidRootIndex(com.eden.orchid.api.indexing.OrchidRootIndex) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) Theme(com.eden.orchid.api.theme.Theme) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with OrchidContext

use of com.eden.orchid.api.OrchidContext in project Orchid by JavaEden.

the class IndexFileResponse method getResponse.

// TODO: convert this to a component
static OrchidResponse getResponse(OrchidContext context, File targetFile, String targetPath) {
    if (targetFile.isDirectory()) {
        Clog.i("Rendering directory index: {}", targetPath);
        File[] files = targetFile.listFiles();
        if (files != null) {
            List<FileRow> jsonDirs = new ArrayList<>();
            List<FileRow> jsonFiles = new ArrayList<>();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd-hh:mm:ss z", Locale.getDefault());
            jsonDirs.add(new FileRow(OrchidUtils.applyBaseUrl(context, StringUtils.strip(targetPath, "/")) + "/..", StringUtils.strip(targetPath, "/") + "/..", ".. (Go up)", "", "", "assets/svg/folder.svg"));
            for (File file : files) {
                FileRow newFile = new FileRow();
                newFile.url = OrchidUtils.applyBaseUrl(context, StringUtils.strip(targetPath, "/") + "/" + file.getName());
                newFile.path = StringUtils.strip(targetPath, "/") + "/" + file.getName();
                newFile.name = file.getName();
                newFile.date = formatter.format(new Date(file.lastModified()));
                String icon;
                if (file.isDirectory()) {
                    icon = "folder";
                    newFile.size = file.listFiles().length + " entries";
                    jsonDirs.add(newFile);
                } else {
                    icon = FilenameUtils.getExtension(file.getName());
                    newFile.size = humanReadableByteCount(file.length(), true);
                    jsonFiles.add(newFile);
                }
                newFile.icon = "assets/svg/" + icon + ".svg";
            }
            jsonDirs.sort(Comparator.comparing(fileRow -> fileRow.name));
            jsonFiles.sort(Comparator.comparing(fileRow -> fileRow.name));
            OrchidResource resource = context.getDefaultResourceSource(null, context.getTheme()).getResourceEntry(context, "templates/server/directoryListing.peb");
            Map<String, Object> indexPageVars = new HashMap<>();
            indexPageVars.put("title", "List of files/dirs under " + targetPath);
            indexPageVars.put("path", targetPath);
            indexPageVars.put("dirs", jsonDirs);
            indexPageVars.put("files", jsonFiles);
            Map<String, Object> object = new HashMap<>(context.getConfig());
            object.put("page", indexPageVars);
            object.put("theme", context.getTheme());
            String directoryListingContent;
            if (resource != null) {
                directoryListingContent = context.compile(resource, resource.getReference().getExtension(), resource.getContent(), object);
            } else {
                directoryListingContent = context.serialize("json", object);
            }
            OrchidPage page = new OrchidPage(new StringResource(new OrchidReference(context, "directoryListing.txt"), directoryListingContent), RenderService.RenderMode.TEMPLATE, "directoryListing", "Index");
            return new OrchidResponse(context).page(page).status(404);
        }
    }
    return new OrchidResponse(context).content("");
}
Also used : OrchidResponse(com.eden.orchid.api.server.OrchidResponse) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) RenderService(com.eden.orchid.api.render.RenderService) OrchidReference(com.eden.orchid.api.theme.pages.OrchidReference) StringUtils(org.apache.commons.lang3.StringUtils) OrchidContext(com.eden.orchid.api.OrchidContext) File(java.io.File) ArrayList(java.util.ArrayList) List(java.util.List) AssetPage(com.eden.orchid.api.theme.assets.AssetPage) Locale(java.util.Locale) Map(java.util.Map) AssetHolder(com.eden.orchid.api.theme.assets.AssetHolder) OrchidUtils(com.eden.orchid.utilities.OrchidUtils) Clog(com.caseyjbrooks.clog.Clog) AssetManagerDelegate(com.eden.orchid.api.theme.assets.AssetManagerDelegate) Comparator(java.util.Comparator) StringResource(com.eden.orchid.api.resources.resource.StringResource) FilenameUtils(org.apache.commons.io.FilenameUtils) OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) StringResource(com.eden.orchid.api.resources.resource.StringResource) OrchidReference(com.eden.orchid.api.theme.pages.OrchidReference) OrchidResponse(com.eden.orchid.api.server.OrchidResponse) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 13 with OrchidContext

use of com.eden.orchid.api.OrchidContext in project Orchid by JavaEden.

the class OrchidUtilsTest method applyBaseUrlTest.

@ParameterizedTest
@CsvSource({ "https://www.example.com, one/two,  https://www.example.com/one/two", "https://www.example.com, /one/two, https://www.example.com/one/two" })
public void applyBaseUrlTest(String baseUrl, String input, String expected) {
    OrchidContext context = mock(OrchidContext.class);
    when(context.getBaseUrl()).thenReturn(baseUrl);
    String output = OrchidUtils.applyBaseUrl(context, input);
    assertThat(output, is(equalTo(expected)));
}
Also used : OrchidContext(com.eden.orchid.api.OrchidContext) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with OrchidContext

use of com.eden.orchid.api.OrchidContext in project Orchid by JavaEden.

the class TaskServiceTest method setUp.

@BeforeEach
public void setUp() {
    Set<OrchidTask> tasks = new HashSet<>();
    OrchidTask task1 = mock(OrchidTask.class);
    when(task1.getName()).thenReturn("task1");
    tasks.add(task1);
    OrchidTask task2 = mock(OrchidTask.class);
    when(task2.getName()).thenReturn("task2");
    tasks.add(task2);
    Set<OrchidCommand> commands = new HashSet<>();
    OrchidFileController controller = mock(OrchidFileController.class);
    OrchidServer server = new OrchidServer(new HashSet<>(), controller);
    FileWatcher fileWatcher = mock(FileWatcher.class);
    // test the service directly
    context = mock(OrchidContext.class);
    service = new TaskServiceImpl(tasks, commands, "", 8080, server, fileWatcher);
    service.initialize(context);
    // test that the default implementation is identical to the real implementation
    underTest = new TaskService() {

        public void initialize(OrchidContext context) {
        }

        public <T extends OrchidService> T getService(Class<T> serviceClass) {
            return (T) service;
        }
    };
}
Also used : OrchidContext(com.eden.orchid.api.OrchidContext) OrchidServer(com.eden.orchid.api.server.OrchidServer) FileWatcher(com.eden.orchid.api.server.FileWatcher) OrchidFileController(com.eden.orchid.api.server.OrchidFileController) HashSet(java.util.HashSet) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

OrchidContext (com.eden.orchid.api.OrchidContext)14 OrchidPage (com.eden.orchid.api.theme.pages.OrchidPage)6 TemplateTag (com.eden.orchid.api.compilers.TemplateTag)4 PebbleWrapperTemplateTag (com.eden.orchid.impl.compilers.pebble.PebbleWrapperTemplateTag)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Map (java.util.Map)3 OrchidResource (com.eden.orchid.api.resources.resource.OrchidResource)2 StringResource (com.eden.orchid.api.resources.resource.StringResource)2 ModularList (com.eden.orchid.api.theme.components.ModularList)2 OrchidReference (com.eden.orchid.api.theme.pages.OrchidReference)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 JSONObject (org.json.JSONObject)2 Clog (com.caseyjbrooks.clog.Clog)1 JSONElement (com.eden.common.json.JSONElement)1 OrchidSecurityManager (com.eden.orchid.api.OrchidSecurityManager)1 OrchidRootIndex (com.eden.orchid.api.indexing.OrchidRootIndex)1 OptionsExtractor (com.eden.orchid.api.options.OptionsExtractor)1 ImpliedKey (com.eden.orchid.api.options.annotations.ImpliedKey)1