Skip to content

Commit

Permalink
#9 add test and simple refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Jun 6, 2024
1 parent c9997e6 commit e1237fc
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.thmarx.cms.extensions.repository;

/*-
* #%L
* cms-extensions
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* 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 3 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/gpl-3.0.html>.
* #L%
*/

import lombok.Data;

/**
*
* @author t.marx
*/
@Data
public class Extension {

private String id;
private String version;
private String name;
private String description;
private String author;
private String url;
private String compatibility;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

/**
*
* @author t.marx
*/
@Slf4j
public class Repository {
public class RemoteRepository {

HttpClient client = HttpClient.newHttpClient();

Expand Down Expand Up @@ -72,7 +73,7 @@ public Optional<String> getContent (String extension) {
return Optional.empty();
}

public Map<String, Object> getInfo (String extension) {
public Optional<Extension> getInfo (String extension) {
try {
var moduleInfoUrl = "https://raw.githubusercontent.com/thmarx/extension-registry/main/%s/%s.yaml"
.formatted(extension, extension);
Expand All @@ -81,11 +82,12 @@ public Map<String, Object> getInfo (String extension) {
HttpRequest request = HttpRequest.newBuilder(uri).build();
String content = client.send(request, BodyHandlers.ofString()).body();

return new Yaml().load(content);

return Optional.of(new Yaml().loadAs(content, Extension.class));
} catch (IOException | InterruptedException ex) {
log.error("", ex);
}

return Collections.emptyMap();
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ public class RepositoryTest {

@Test
public void getInfo() {
Repository repository = new Repository();
RemoteRepository repository = new RemoteRepository();

var info = repository.getInfo("test-extension");

Assertions.assertThat(info)
.containsEntry("id", "test-extension");
Assertions.assertThat(info).isNotEmpty();

Assertions.assertThat(info.get().getId()).isEqualTo("test-extension");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
* @author t.marx
*/
public class CMSCli {

public static CommandLine getCommandLine () {
return new CommandLine(new ServerCommand());
}

public static void main(String[] args) {
new CommandLine(new ServerCommand()).execute(args);
CMSCli.getCommandLine().execute(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

import com.github.thmarx.cms.CMSServer;
import com.github.thmarx.cms.extensions.repository.Repository;
import com.github.thmarx.cms.extensions.repository.RemoteRepository;
import lombok.Getter;

/**
Expand All @@ -33,11 +33,14 @@
public abstract class AbstractExtensionCommand {

@Getter
private Repository repository = new Repository();
private RemoteRepository repository = new RemoteRepository();

public boolean isCompatibleWithServer(String extension) {
var info = repository.getInfo(extension);
var compatibility = (String) info.get("compatibility");
return CMSServer.getVersion().satisfies(compatibility);
if (info.isEmpty()) {
throw new RuntimeException("extension not found");
}

return CMSServer.getVersion().satisfies(info.get().getCompatibility());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.CMSServer;
import com.github.thmarx.cms.extensions.repository.Repository;
import org.semver4j.Semver;
import com.github.thmarx.cms.extensions.repository.RemoteRepository;
import lombok.Setter;
import picocli.CommandLine;

/**
Expand All @@ -34,25 +33,31 @@
@CommandLine.Command(name = "info")
public class InfoCommand implements Runnable {

Repository repository = new Repository();
RemoteRepository repository = new RemoteRepository();

@CommandLine.Parameters(
paramLabel = "<extension>",
index = "0",
description = "The id of the extension."
)
@Setter
private String extension = "";

@Override
public void run() {
System.out.println("ext info command");
System.out.println("module: " + repository.exists(extension));
if (repository.exists(extension)) {
var info = repository.getInfo(extension);
var compatibility = (String)info.get("compatibility");
System.out.println("server: " + CMSServer.getVersion().getVersion());
System.out.println("compatibility: " + compatibility);
System.out.println("compatible with server version: " + CMSServer.getVersion().satisfies(compatibility));
if (!repository.exists(extension)) {
throw new RuntimeException("Extension not available");
}
var info = repository.getInfo(extension).get();

System.out.println("extension: " + info.getId());
System.out.println("name: " + info.getName());
System.out.println("description: " + info.getDescription());
System.out.println("author: " + info.getAuthor());
System.out.println("url: " + info.getUrl());
System.out.println("compatibility: " + info.getCompatibility());
System.out.println("your server version: " + CMSServer.getVersion().getVersion());
System.out.println("compatibility with server version: " + CMSServer.getVersion().satisfies(info.getCompatibility()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* #L%
*/

import com.github.thmarx.cms.extensions.repository.Repository;
import com.github.thmarx.cms.extensions.repository.RemoteRepository;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -38,7 +38,7 @@
@CommandLine.Command(name = "install")
public class InstallCommand extends AbstractExtensionCommand implements Runnable {

Repository repository = new Repository();
RemoteRepository repository = new RemoteRepository();

@CommandLine.Parameters(
paramLabel = "<extension>",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.thmarx.cms.cli.commands.extensions;

import org.junit.jupiter.api.Test;

/**
*
* @author t.marx
*/
public class InfoCommandTest {

@Test
public void testSomeMethod() {

var cmd = new InfoCommand();
cmd.setExtension("test-extension");
cmd.run();

}

}

0 comments on commit e1237fc

Please sign in to comment.