Search in sources :

Example 1 with PersistenceContext

use of org.eclipse.persistence.jpa.rs.PersistenceContext in project eclipselink by eclipse-ee4j.

the class TestService method testUpdateRelationship.

@Test
public void testUpdateRelationship() throws Exception {
    EntityResource resource = new EntityResource();
    resource.setPersistenceFactory(factory);
    PersistenceContext context = getAuctionPersistenceContext(null);
    DynamicEntity entity1 = context.newEntity("Auction");
    entity1.set("name", "Computer");
    DynamicEntity entity2 = context.newEntity("User");
    entity2.set("name", "Bob");
    context.create(null, entity2);
    DynamicEntity entity3 = context.newEntity("Bid");
    entity3.set("amount", 200d);
    entity3.set("user", entity2);
    entity3.set("auction", entity1);
    context.create(null, entity3);
    entity3 = (DynamicEntity) context.find(null, "Bid", entity3.get("id"), null);
    entity3.set("amount", 201d);
    entity1 = entity3.get("auction");
    entity1.set("name", "Laptop");
    StreamingOutput output = (StreamingOutput) resource.update("jpars_auction", "Bid", TestHttpHeaders.generateHTTPHeader(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON), new TestURIInfo(), serializeToStream(entity3, context, MediaType.APPLICATION_JSON_TYPE)).getEntity();
    String resultString = stringifyResults(output);
    assertTrue("amount was not in results.", resultString.replace(" ", "").contains("\"amount\":201.0"));
    String uri = RestUtils.getServerURI() + "jpars_auction/entity/Bid/";
    assertTrue("link was not in results.", resultString.replace(" ", "").contains(uri));
    assertTrue("rel was not in results.", resultString.replace(" ", "").contains("\"rel\":\"user\""));
    assertTrue("Laptop was not a link in results.", !resultString.replace(" ", "").contains("\"name\":\"Laptop\""));
}
Also used : DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) EntityResource(org.eclipse.persistence.jpa.rs.resources.unversioned.EntityResource) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) TestURIInfo(org.eclipse.persistence.jpars.test.util.TestURIInfo) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) Test(org.junit.Test)

Example 2 with PersistenceContext

use of org.eclipse.persistence.jpa.rs.PersistenceContext in project eclipselink by eclipse-ee4j.

the class TestService method testUnmarshallNonExistantLink.

@Test
public void testUnmarshallNonExistantLink() throws URISyntaxException {
    PersistenceResource resource = new PersistenceResource();
    resource.setPersistenceFactory(factory);
    PersistenceContext context = getAuctionPersistenceContext(null);
    LinkAdapter adapter = new LinkAdapter("http://localhost:9090/JPA-RS/", context);
    DynamicEntity entity1 = null;
    DynamicEntity entity2 = null;
    try {
        entity1 = (DynamicEntity) adapter.unmarshal("http://localhost:9090/JPA-RS/jpars_auction/entity/Auction/1");
    } catch (Exception e) {
        fail(e.toString());
    }
    assertTrue("id for Auction was missing", entity1.get("id").equals(1));
    try {
        entity2 = (DynamicEntity) adapter.unmarshal("http://localhost:9090/JPA-RS/jpars_auction/entity/Address/1+Home");
    } catch (Exception e) {
        fail(e.toString());
    }
    assertTrue("id for Address was missing", entity2.get("id").equals(1));
    assertTrue("type for Address was missing", entity2.get("type").equals("Home"));
}
Also used : PersistenceResource(org.eclipse.persistence.jpa.rs.resources.unversioned.PersistenceResource) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) LinkAdapter(org.eclipse.persistence.jpa.rs.util.xmladapters.LinkAdapter) URISyntaxException(java.net.URISyntaxException) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException) IOException(java.io.IOException) JAXBException(jakarta.xml.bind.JAXBException) Test(org.junit.Test)

Example 3 with PersistenceContext

use of org.eclipse.persistence.jpa.rs.PersistenceContext in project eclipselink by eclipse-ee4j.

the class TestService method testMultitenant.

@Test(expected = JPARSException.class)
public void testMultitenant() throws JAXBException, URISyntaxException {
    EntityResource resource = new EntityResource();
    resource.setPersistenceFactory(factory);
    Map<String, Object> properties = new HashMap<>();
    properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, null);
    PersistenceContext context = factory.get("jpars_auction-static-local", RestUtils.getServerURI(), null, properties);
    Account account = new Account();
    account.setAccountNumber("AAA111");
    TestURIInfo ui = new TestURIInfo();
    ui.addMatrixParameter("jpars_auction-static-local", "tenant.id", "AcctOwner1");
    StreamingOutput output = (StreamingOutput) resource.update("jpars_auction-static-local", "Account", TestHttpHeaders.generateHTTPHeader(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON), ui, serializeToStream(account, context, MediaType.APPLICATION_JSON_TYPE)).getEntity();
    String result = stringifyResults(output);
    account = (Account) context.unmarshal(Account.class, MediaType.APPLICATION_JSON_TYPE, new ByteArrayInputStream(result.getBytes()));
    output = (StreamingOutput) resource.find("jpars_auction-static-local", "Account", String.valueOf(account.getId()), TestHttpHeaders.generateHTTPHeader(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON), ui).getEntity();
    result = stringifyResults(output);
    account = (Account) context.unmarshal(Account.class, MediaType.APPLICATION_JSON_TYPE, new ByteArrayInputStream(result.getBytes()));
    assertTrue("account is null", account != null);
    TestURIInfo ui2 = new TestURIInfo();
    ui2.addMatrixParameter("jpars_auction-static-local", "tenant.id", "AcctOwner2");
    output = (StreamingOutput) resource.find("jpars_auction-static-local", "Account", String.valueOf(account.getId()), TestHttpHeaders.generateHTTPHeader(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON), ui2).getEntity();
}
Also used : Account(org.eclipse.persistence.jpars.test.model.multitenant.Account) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityResource(org.eclipse.persistence.jpa.rs.resources.unversioned.EntityResource) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) TestURIInfo(org.eclipse.persistence.jpars.test.util.TestURIInfo) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) Test(org.junit.Test)

Example 4 with PersistenceContext

use of org.eclipse.persistence.jpa.rs.PersistenceContext in project eclipselink by eclipse-ee4j.

the class TestService method testNamedQuerySingleResult.

@Test
public void testNamedQuerySingleResult() throws Exception {
    SingleResultQueryResource resource = new SingleResultQueryResource();
    resource.setPersistenceFactory(factory);
    PersistenceContext context = getAuctionPersistenceContext(null);
    DynamicEntity entity1 = context.newEntity("Auction");
    entity1.set("name", "Computer");
    context.create(null, entity1);
    DynamicEntity entity2 = context.newEntity("Auction");
    entity2.set("name", "Word Processor");
    context.create(null, entity2);
    TestHttpHeaders headers = new TestHttpHeaders();
    headers.getAcceptableMediaTypes().add(MediaType.APPLICATION_JSON_TYPE);
    List<String> mediaTypes = new ArrayList<>();
    mediaTypes.add(MediaType.APPLICATION_JSON);
    TestURIInfo ui = new TestURIInfo();
    ui.addMatrixParameter("Auction.forName", "name", "Computer");
    StreamingOutput output = (StreamingOutput) resource.namedQuerySingleResult("jpars_auction", "Auction.forName", headers, ui).getEntity();
    String resultString = stringifyResults(output);
    assertTrue("Computer was not in results.", resultString.contains("\"name\":\"Computer\""));
    assertFalse("Word Processor was in results.", resultString.contains("\"name\":\"Word Processor\""));
    clearData();
}
Also used : DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) TestHttpHeaders(org.eclipse.persistence.jpars.test.util.TestHttpHeaders) SingleResultQueryResource(org.eclipse.persistence.jpa.rs.resources.unversioned.SingleResultQueryResource) ArrayList(java.util.ArrayList) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) TestURIInfo(org.eclipse.persistence.jpars.test.util.TestURIInfo) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) Test(org.junit.Test)

Example 5 with PersistenceContext

use of org.eclipse.persistence.jpa.rs.PersistenceContext in project eclipselink by eclipse-ee4j.

the class TestService method testDelete.

@Test
public void testDelete() throws Exception {
    EntityResource resource = new EntityResource();
    resource.setPersistenceFactory(factory);
    PersistenceContext context = getAuctionPersistenceContext(null);
    DynamicEntity entity1 = context.newEntity("Auction");
    entity1.set("name", "Computer1");
    context.create(null, entity1);
    TestURIInfo ui = new TestURIInfo();
    resource.delete("jpars_auction", "Auction", entity1.get("id").toString(), TestHttpHeaders.generateHTTPHeader(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON), ui);
    entity1 = (DynamicEntity) context.find("Auction", entity1.get("id"));
    assertTrue("Entity was not deleted.", entity1 == null);
}
Also used : DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) EntityResource(org.eclipse.persistence.jpa.rs.resources.unversioned.EntityResource) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) TestURIInfo(org.eclipse.persistence.jpars.test.util.TestURIInfo) Test(org.junit.Test)

Aggregations

PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)37 StreamingOutputMarshaller (org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)17 JPARSException (org.eclipse.persistence.jpa.rs.exceptions.JPARSException)15 Test (org.junit.Test)14 TestURIInfo (org.eclipse.persistence.jpars.test.util.TestURIInfo)12 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)11 JAXBException (jakarta.xml.bind.JAXBException)10 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)9 StreamingOutput (jakarta.ws.rs.core.StreamingOutput)8 EntityResource (org.eclipse.persistence.jpa.rs.resources.unversioned.EntityResource)8 ArrayList (java.util.ArrayList)7 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)7 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 SingleResultQueryResource (org.eclipse.persistence.jpa.rs.resources.unversioned.SingleResultQueryResource)4 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)4 ReportQuery (org.eclipse.persistence.queries.ReportQuery)4