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\""));
}
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"));
}
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();
}
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();
}
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);
}
Aggregations