Search in sources :

Example 1 with JSONCodec

use of aQute.lib.json.JSONCodec in project bnd by bndtools.

the class PersistentResourceTest method testSimple.

public void testSimple() throws Exception {
    ResourceBuilder rb = new ResourceBuilder();
    rb.addCapability(new CapReqBuilder("test").addAttribute("double", 3.0).addAttribute("long", 3L).addAttribute("string", "3.0").addAttribute("version", new Version("3.0")).buildSyntheticCapability());
    Resource r = rb.build();
    PersistentResource pr = new PersistentResource(r);
    String s = new JSONCodec().enc().put(pr).toString();
    PersistentResource pr2 = new JSONCodec().dec().from(s).get(PersistentResource.class);
    List<Capability> capabilities = pr.getResource().getCapabilities(null);
    List<Requirement> requirements = pr.getResource().getRequirements(null);
    assertEquals(1, capabilities.size());
    assertEquals(0, requirements.size());
    Capability capability = capabilities.get(0);
    assertEquals("test", capability.getNamespace());
    assertEquals(3.0, capability.getAttributes().get("double"));
    assertEquals(3L, capability.getAttributes().get("long"));
    assertEquals("3.0", capability.getAttributes().get("string"));
    assertEquals(new Version("3.0"), capability.getAttributes().get("version"));
    assertEquals(0, capability.getDirectives().size());
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) Requirement(org.osgi.resource.Requirement) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) Resource(org.osgi.resource.Resource) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) JSONCodec(aQute.lib.json.JSONCodec)

Example 2 with JSONCodec

use of aQute.lib.json.JSONCodec in project bnd by bndtools.

the class HttpTestServerTest method get.

private <T> T get(HttpTestServer http, TypeReference<T> tref, String path) throws Exception {
    URL uri = new URI(http.getBaseURI() + path).toURL();
    HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
    String collect = IO.collect(connection.getInputStream());
    return new JSONCodec().dec().from(collect).get(tref);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JSONCodec(aQute.lib.json.JSONCodec) URI(java.net.URI) URL(java.net.URL)

Aggregations

JSONCodec (aQute.lib.json.JSONCodec)2 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)1 PersistentResource (aQute.bnd.osgi.resource.PersistentResource)1 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)1 HttpURLConnection (java.net.HttpURLConnection)1 URI (java.net.URI)1 URL (java.net.URL)1 Version (org.osgi.framework.Version)1 Capability (org.osgi.resource.Capability)1 Requirement (org.osgi.resource.Requirement)1 Resource (org.osgi.resource.Resource)1