158 lines
6.9 KiB
Java
158 lines
6.9 KiB
Java
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationModule;
|
|
import jakarta.ws.rs.client.Client;
|
|
import jakarta.ws.rs.client.ClientBuilder;
|
|
import jakarta.ws.rs.client.Entity;
|
|
import jakarta.ws.rs.client.Invocation;
|
|
import jakarta.ws.rs.client.WebTarget;
|
|
import jakarta.ws.rs.core.MediaType;
|
|
import jakarta.ws.rs.core.Response;
|
|
import jakarta.ws.rs.core.UriBuilder;
|
|
import lombok.SneakyThrows;
|
|
import net.woggioni.jpacrepo.api.model.PkgData;
|
|
import net.woggioni.jpacrepo.api.service.FileSystemSynchronizer;
|
|
import net.woggioni.jpacrepo.impl.model.CompressionFormatImpl;
|
|
import net.woggioni.jpacrepo.impl.model.PkgDataParser;
|
|
import net.woggioni.jwo.Con;
|
|
import net.woggioni.jwo.Hash;
|
|
import net.woggioni.jwo.JWO;
|
|
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
|
|
import org.jboss.resteasy.spi.ResteasyProviderFactory;
|
|
import org.junit.jupiter.api.Assertions;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.io.TempDir;
|
|
|
|
import javax.naming.Context;
|
|
import javax.naming.InitialContext;
|
|
import javax.naming.NameClassPair;
|
|
import javax.naming.NamingEnumeration;
|
|
import javax.naming.NamingException;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.InputStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.Properties;
|
|
import java.util.stream.Stream;
|
|
|
|
public class ClientTest {
|
|
|
|
private static ObjectMapper om;
|
|
|
|
static {
|
|
om = new ObjectMapper();
|
|
JakartaXmlBindAnnotationModule module = new JakartaXmlBindAnnotationModule();
|
|
om.registerModule(module);
|
|
}
|
|
|
|
@SneakyThrows
|
|
public void testGET() {
|
|
ResteasyProviderFactory instance = ResteasyProviderFactory.getInstance();
|
|
RegisterBuiltin.register(instance);
|
|
// instance.registerProvider(ResteasyJacksonProvider.class);
|
|
Client client = ClientBuilder.newClient();
|
|
UriBuilder builder = UriBuilder.fromUri("http://woggioni.net/").path("jpacrepo/rest/pkg/search");
|
|
// builder.queryParam("name", "linux");
|
|
// builder.queryParam("version", "324");
|
|
builder.queryParam("md5sum", "19787793429AF74D4D2D09890247E2EC");
|
|
WebTarget target = client.target(builder.build());
|
|
Invocation invocation = target.request().accept("application/xml").buildGet();
|
|
Response response = invocation.invoke();
|
|
if (response.getStatusInfo() == Response.Status.OK) {
|
|
PkgData pkg = response.readEntity(PkgData.class);
|
|
om.writeValue(System.out, pkg);
|
|
} else {
|
|
throw JWO.newThrowable(RuntimeException.class,
|
|
"Server returned %d",
|
|
response.getStatusInfo().getStatusCode());
|
|
}
|
|
}
|
|
|
|
public void testPUT() throws Exception {
|
|
ResteasyProviderFactory instance = ResteasyProviderFactory.getInstance();
|
|
RegisterBuiltin.register(instance);
|
|
Client client = ClientBuilder.newClient();
|
|
UriBuilder builder = UriBuilder.fromUri("http://localhost:8080/").path("jpacrepo-1.0/rest/pkg/upload");
|
|
builder.matrixParam("filename", "k290-fnkeyctl-1.2-1-x86_64.pkg.tar.xz");
|
|
WebTarget target = client.target(builder.build());
|
|
FileInputStream fis = new FileInputStream(new File("/tmp/ciao/k290-fnkeyctl-1.2-1-x86_64.pkg.tar.xz"));
|
|
|
|
Entity<FileInputStream> e = Entity.entity(fis, MediaType.APPLICATION_OCTET_STREAM);
|
|
Invocation invocation = target.request().buildPost(e);
|
|
System.out.println(target.request().toString());
|
|
Response response = invocation.invoke();
|
|
System.out.println(response.getStatusInfo());
|
|
assert Response.Status.CREATED.getStatusCode() == response.getStatus();
|
|
}
|
|
|
|
@Test
|
|
public void hashTest(@TempDir Path testDir) {
|
|
ClassLoader cl = getClass().getClassLoader();
|
|
Stream.of("gvfs-nfs-1.50.2-1-x86_64.pkg.tar.zst")
|
|
.forEach((Con<String>) resourceName -> {
|
|
Path tmpFile = testDir.resolve(resourceName);
|
|
try(InputStream is = cl.getResourceAsStream(resourceName)) {
|
|
Files.copy(is, tmpFile);
|
|
}
|
|
Hash hash;
|
|
try(InputStream is = Files.newInputStream(tmpFile)) {
|
|
hash = Hash.md5(is);
|
|
}
|
|
PkgData p = PkgDataParser.parseFile(tmpFile, CompressionFormatImpl.guess(tmpFile));
|
|
Assertions.assertEquals(JWO.bytesToHex(hash.getBytes()), p.getMd5sum());
|
|
});
|
|
}
|
|
|
|
private static void traverseJndiNode(String nodeName, Context context) {
|
|
try {
|
|
NamingEnumeration<NameClassPair> list = context.list(nodeName);
|
|
while (list.hasMore()) {
|
|
String childName = nodeName + "" + list.next().getName();
|
|
System.out.println(childName);
|
|
traverseJndiNode(childName, context);
|
|
}
|
|
} catch (NamingException ex) {
|
|
// We reached a leaf
|
|
}
|
|
}
|
|
|
|
@Test
|
|
public void invokeStatelessBean() throws Exception {
|
|
Properties prop = new Properties();
|
|
InputStream in = getClass().getClassLoader().getResourceAsStream("jboss-ejb-client.properties");
|
|
prop.load(in);
|
|
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
|
|
|
|
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
|
|
// prop.put(Context.PROVIDER_URL, "http-remoting://localhost:1234");
|
|
prop.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
|
|
// prop.put(Context.PROVIDER_URL, "remote://odroid-u3:4447");
|
|
prop.put(Context.SECURITY_PRINCIPAL, "walter");
|
|
prop.put(Context.SECURITY_CREDENTIALS, "27ff5990757d1d");
|
|
// prop.put(Context.SECURITY_PRINCIPAL, "luser");
|
|
// prop.put(Context.SECURITY_CREDENTIALS, "123456");
|
|
|
|
prop.put("jboss.naming.client.ejb.context", true);
|
|
Context context = new InitialContext(prop);
|
|
Context ctx = new InitialContext(prop);
|
|
traverseJndiNode("/", context);
|
|
// final PacmanService stateService = (PacmanService) ctx.lookup("/jpacrepo-1.0/remote/PacmanServiceEJB!service.PacmanService");
|
|
final FileSystemSynchronizer service = (FileSystemSynchronizer) ctx.lookup(
|
|
"/jpacrepo/PackageSynchronizerEJB!net.woggioni.jpacrepo.api.service.FileSystemSynchronizer"
|
|
);
|
|
// List<PkgData> pkgs = service.searchPackage("google-earth", null, null, 1, 10);
|
|
// System.out.println(new XStream().toXML(pkgs));
|
|
service.syncDb();
|
|
// service.searchPkgId("jre8-openjdk", null, null, null)
|
|
// .stream()
|
|
// .map(service::getPackage)
|
|
// .filter(Objects::nonNull)
|
|
// .map(PkgData::getFileName)
|
|
// .forEach(System.out::println);
|
|
// final TestEJB service = (TestEJB) ctx.lookup(
|
|
// "/jpacrepo-2023.07TestEJBImpl!net.woggioni.jpacrepo.api.service.TestEJB"
|
|
// );
|
|
// System.out.println(service.hello());
|
|
}
|
|
|
|
} |