use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACOfferManagerTest method testDeleteOffer.
@Test
public void testDeleteOffer() {
OLATResourceable testOreable = new TypedResourceable(UUID.randomUUID().toString().replace("-", ""));
OLATResource testOres = OLATResourceManager.getInstance().findOrPersistResourceable(testOreable);
assertNotNull(testOres);
dbInstance.commitAndCloseSession();
// create an offer
Offer offer = acOfferManager.createOffer(testOres, "TestDeleteOffer");
assertNotNull(offer);
assertEquals(OfferImpl.class, offer.getClass());
// and save the offer
acOfferManager.saveOffer(offer);
dbInstance.commitAndCloseSession();
// create a link offer to method
List<AccessMethod> methods = acMethodManager.getAvailableMethodsByType(TokenAccessMethod.class);
AccessMethod method = methods.get(0);
OfferAccess access = acMethodManager.createOfferAccess(offer, method);
acMethodManager.save(access);
dbInstance.commitAndCloseSession();
// retrieve the offer
List<Offer> offers = acOfferManager.findOfferByResource(testOres, true, null);
assertNotNull(offers);
assertEquals(1, offers.size());
assertEquals(offer, offers.get(0));
dbInstance.commitAndCloseSession();
// delete the offer
acOfferManager.deleteOffer(offer);
dbInstance.commitAndCloseSession();
// try to retrieve the offer
List<Offer> noOffers = acOfferManager.findOfferByResource(testOres, true, null);
assertNotNull(noOffers);
assertEquals(0, noOffers.size());
dbInstance.commitAndCloseSession();
// retrieve all offers, deleted too
List<Offer> delOffers = acOfferManager.findOfferByResource(testOres, false, null);
assertNotNull(delOffers);
assertEquals(1, delOffers.size());
assertEquals(offer, delOffers.get(0));
assertEquals(false, delOffers.get(0).isValid());
dbInstance.commitAndCloseSession();
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACOfferManagerTest method testSaveOffer.
@Test
public void testSaveOffer() {
// create a resource
OLATResourceable testOreable = new TypedResourceable(UUID.randomUUID().toString().replace("-", ""));
OLATResource testOres = OLATResourceManager.getInstance().findOrPersistResourceable(testOreable);
assertNotNull(testOres);
dbInstance.commitAndCloseSession();
// create an offer
Offer offer = acOfferManager.createOffer(testOres, "TestSaveOffer");
assertNotNull(offer);
assertEquals(OfferImpl.class, offer.getClass());
if (offer instanceof OfferImpl) {
OfferImpl offerImpl = (OfferImpl) offer;
offerImpl.setToken("token1");
}
offer.setValidFrom(new Date());
offer.setValidTo(new Date());
// and save the offer
acOfferManager.saveOffer(offer);
dbInstance.commitAndCloseSession();
// create a link offer to method
List<AccessMethod> methods = acMethodManager.getAvailableMethodsByType(TokenAccessMethod.class);
AccessMethod method = methods.get(0);
OfferAccess access = acMethodManager.createOfferAccess(offer, method);
acMethodManager.save(access);
dbInstance.commitAndCloseSession();
// check if the offer is saved
List<Offer> offers = acOfferManager.findOfferByResource(testOres, true, null);
assertNotNull(offers);
assertEquals(1, offers.size());
Offer savedOffer = offers.get(0);
assertNotNull(savedOffer);
assertEquals(OfferImpl.class, savedOffer.getClass());
if (savedOffer instanceof OfferImpl) {
OfferImpl offerImpl = (OfferImpl) savedOffer;
assertEquals("token1", offerImpl.getToken());
}
assertNotNull(offer.getValidFrom());
assertNotNull(offer.getValidTo());
assertEquals(testOres.getResourceableId(), savedOffer.getResourceId());
assertEquals(testOres.getResourceableTypeName(), savedOffer.getResourceTypeName());
assertEquals("TestSaveOffer", savedOffer.getResourceDisplayName());
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACOrderManagerTest method testLoadBy.
@Test
public void testLoadBy() {
// create some offers to buy
OLATResource randomOres1 = createResource();
Offer offer1 = acService.createOffer(randomOres1, "TestLoadBy 1");
offer1 = acService.save(offer1);
OLATResource randomOres2 = createResource();
Offer offer2 = acService.createOffer(randomOres2, "TestLoadBy 2");
offer2 = acService.save(offer2);
dbInstance.commitAndCloseSession();
// create a link offer to method
List<AccessMethod> methods = acMethodManager.getAvailableMethodsByType(TokenAccessMethod.class);
assertNotNull(methods);
assertEquals(1, methods.size());
AccessMethod method = methods.get(0);
OfferAccess access1 = acMethodManager.createOfferAccess(offer1, method);
acMethodManager.save(access1);
OfferAccess access2 = acMethodManager.createOfferAccess(offer2, method);
acMethodManager.save(access2);
dbInstance.commitAndCloseSession();
// one clicks
Order order1 = acOrderManager.saveOneClick(ident4, access1);
Order order2 = acOrderManager.saveOneClick(ident5, access2);
Order order3_1 = acOrderManager.saveOneClick(ident6, access1);
Order order3_2 = acOrderManager.saveOneClick(ident6, access2);
dbInstance.commitAndCloseSession();
// load by delivery: ident 1
List<Order> retrivedOrder1 = acOrderManager.findOrdersByDelivery(ident4);
assertNotNull(retrivedOrder1);
assertEquals(1, retrivedOrder1.size());
assertEquals(order1, retrivedOrder1.get(0));
// load by delivery: ident 2
List<Order> retrievedOrder2 = acOrderManager.findOrdersByDelivery(ident5);
assertNotNull(retrievedOrder2);
assertEquals(1, retrievedOrder2.size());
assertEquals(order2, retrievedOrder2.get(0));
// load by delivery: ident 3
List<Order> retrievedOrder3 = acOrderManager.findOrdersByDelivery(ident6);
assertNotNull(retrievedOrder3);
assertEquals(2, retrievedOrder3.size());
assertTrue(order3_1.equals(retrievedOrder3.get(0)) || order3_1.equals(retrievedOrder3.get(1)));
assertTrue(order3_2.equals(retrievedOrder3.get(0)) || order3_2.equals(retrievedOrder3.get(1)));
dbInstance.commitAndCloseSession();
// load by resource: ores 1
List<Order> retrievedOrderOres1 = acOrderManager.findOrdersByResource(randomOres1);
assertNotNull(retrievedOrderOres1);
assertEquals(2, retrievedOrderOres1.size());
assertTrue(order1.equals(retrievedOrderOres1.get(0)) || order1.equals(retrievedOrderOres1.get(1)));
assertTrue(order3_1.equals(retrievedOrderOres1.get(0)) || order3_1.equals(retrievedOrderOres1.get(1)));
// load by resource: ores 2
List<Order> retrievedOrderOres2 = acOrderManager.findOrdersByResource(randomOres2);
assertNotNull(retrievedOrderOres2);
assertEquals(2, retrievedOrderOres2.size());
assertTrue(order2.equals(retrievedOrderOres2.get(0)) || order2.equals(retrievedOrderOres2.get(1)));
assertTrue(order3_2.equals(retrievedOrderOres2.get(0)) || order3_2.equals(retrievedOrderOres2.get(1)));
dbInstance.commitAndCloseSession();
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACTransactionManagerTest method testSaveTransaction.
@Test
public void testSaveTransaction() {
// pick up a method
List<AccessMethod> methods = acMethodManager.getAvailableMethodsByType(TokenAccessMethod.class);
assertNotNull(methods);
assertEquals(1, methods.size());
AccessMethod tokenMethod = methods.get(0);
// create an offer to buy
OLATResource randomOres = createResource();
Offer offer = acService.createOffer(randomOres, "TestSaveTransaction");
offer = acService.save(offer);
dbInstance.commitAndCloseSession();
// create and save an order
Order order = acOrderManager.createOrder(ident1);
OrderPart orderPart = acOrderManager.addOrderPart(order);
OrderLine item = acOrderManager.addOrderLine(orderPart, offer);
assertNotNull(order);
assertNotNull(item);
assertNotNull(order.getDelivery());
assertEquals(ident1, order.getDelivery());
acOrderManager.save(order);
AccessTransaction accessTransaction = acTransactionManager.createTransaction(order, orderPart, tokenMethod);
assertNotNull(accessTransaction);
acTransactionManager.save(accessTransaction);
dbInstance.commitAndCloseSession();
{
// test load by key
AccessTransaction retrievedTransaction = acTransactionManager.loadTransactionByKey(accessTransaction.getKey());
assertNotNull(retrievedTransaction);
assertNotNull(retrievedTransaction.getOrder());
assertEquals(order, retrievedTransaction.getOrder());
assertNotNull(retrievedTransaction.getOrderPart());
assertEquals(orderPart, retrievedTransaction.getOrderPart());
assertNotNull(retrievedTransaction.getMethod());
assertEquals(tokenMethod, retrievedTransaction.getMethod());
}
{
// test load by order
List<AccessTransaction> retrievedTransactions = acTransactionManager.loadTransactionsForOrder(order);
assertNotNull(retrievedTransactions);
assertEquals(1, retrievedTransactions.size());
AccessTransaction retrievedTransaction = retrievedTransactions.get(0);
assertNotNull(retrievedTransaction);
assertNotNull(retrievedTransaction.getOrder());
assertEquals(order, retrievedTransaction.getOrder());
assertNotNull(retrievedTransaction.getOrderPart());
assertEquals(orderPart, retrievedTransaction.getOrderPart());
assertNotNull(retrievedTransaction.getMethod());
assertEquals(tokenMethod, retrievedTransaction.getMethod());
}
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACFrontendManagerTest method makeAccessible.
@Test
public void makeAccessible() {
Identity id = JunitTestHelper.createAndPersistIdentityAsUser("acc-" + UUID.randomUUID());
List<AccessMethod> methods = acMethodManager.getAvailableMethodsByType(FreeAccessMethod.class);
AccessMethod method = methods.get(0);
RepositoryEntry re = createRepositoryEntry();
Assert.assertNotNull(re);
// create an offer to buy
OLATResource randomOres = re.getOlatResource();
Offer offer = acService.createOffer(randomOres, "Test auto access");
offer.setAutoBooking(true);
OfferAccess link = acService.createOfferAccess(offer, method);
offer = acService.save(offer);
acService.saveOfferAccess(link);
dbInstance.commit();
long start = System.nanoTime();
AccessResult acResult = acService.isAccessible(re, id, false, true);
Assert.assertNotNull(acResult);
Assert.assertTrue(acResult.isAccessible());
dbInstance.commit();
CodeHelper.printNanoTime(start, "One click");
}
Aggregations