@Test public void testNeverTransactionalShouldPassThrough() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is wrong - it is going to be exception customerService.createCustomerComplexInsideNevetTrans_NoTransOutside( customerWithTooLongCity, okRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // records created by insertCustomer method - no transactional so address is created and // commited and then exception and rollback on person assertEquals(new Integer(0), addressVol); assertEquals(new Integer(1), personVol); // record created by createRoleNeverTransaction method - no transactional so role is commited // - as a first line of execution inside // customerService.createCustomerComplexInsideNevetTrans_NoTransOutside method assertEquals(new Integer(1), roleVol); } }
@Test @Transactional public void shouldNotCreateAddressInSecondCustomer() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // first customer is ok, second has wrong data too long address name so there will be // exception customerService.createCustomerComplexProxyBasedExample(okCustomer, customerWithTooLongCity); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); System.out.println("Address vol =" + addressVol + " personVol=" + personVol); // first person from okCustomer, second person from customerWithTooLongCity assertEquals(new Integer(2), personVol); // first address from okCustomer, second not exists because of exception and rollback in this // step assertEquals(new Integer(1), addressVol); /** * This assertion shows that in second customer creation there is no transactional because * person was created - only addres was rolledback */ } }
/** * In this case exception because outside were created transaction and inside transaction is not * allowed */ @Test(expected = IllegalTransactionStateException.class) public void testNeverTransactionalShouldThrowException() { customerService.cleanDatabase(); roleService.cleanDatabase(); // customer is wrong - it is going to be exception customerService.createCustomerComplexInsideNevetTrans_WithTransOutside( customerWithTooLongCity, okRole); }
@Test public void testStoreCustomer() { when(customerRepository.save(customer)).thenReturn(customer); customerService.storeCustomer(customer); }
@Test public void testCreateCustomer() { Customer customer = new Customer(); FullName fullName = new FullName(); fullName.setFirstName("test"); fullName.setMiddleName("test"); fullName.setSurName("test"); customer.setFullName(fullName); customer.setCreditRating(80); customer.setInitials("S"); customer.setMaritalStatus(MaritalStatus.valueOf("MARRIED")); customer.setNabCustomer(true); Address address = new Address(); address.setStreetNumber(12); address.setStreetName("test"); address.setSuburb("test"); address.setCity("test"); address.setState("test"); address.setPincode(3166); address.setCountry("test"); customer.setResidenceAddress(address); customer.setSex(Sex.valueOf("MALE")); customer.setTitle("Mr"); assert (customerService.createCustomer(customer)); }
public static void main(String... args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); CustomerService customerService = (CustomerService) applicationContext.getBean("customerServiceProxy"); customerService.printName(); customerService.printURL(); try { customerService.printThrowException(); } catch (Exception e) { } finally { if (applicationContext != null) { ((ClassPathXmlApplicationContext) applicationContext).close(); } } }
@Test public void testCreateCustomer() { Customer cust = new Customer(); cust.setId(1L); service.createCustomer(cust); verify(customerDaoMock).create(cust); verifyNoMoreInteractions(customerDaoMock); }
@Test public void testFindAll() { List<Customer> customers = new ArrayList<Customer>(); customers.add(customer); when(customerRepository.findAll()).thenReturn(customers); assertEquals(1, customerService.findAll().size()); }
@Test public void testFindCustomerById() { Customer c1 = new Customer(); c1.setId(1L); doReturn(c1).when(customerDaoMock).findById(1L); Customer customer = service.findCustomerById(1L); verify(customerDaoMock).findById(1L); verifyNoMoreInteractions(customerDaoMock); Assert.assertEquals(Long.valueOf(1), customer.getId()); }
@Test public void testDeleteCustomer() { Customer c1 = new Customer(); c1.setId(1L); doReturn(c1).when(customerDaoMock).findById(1L); service.deleteCustomer(1L); verify(customerDaoMock).findById(1L); verify(customerDaoMock).remove(c1); verifyNoMoreInteractions(customerDaoMock); }
/** * Please compare it with test shouldNotCreateAddressInSecondCustomer(). Here you can see that * inside body executed method are transactional. Creation second customer is transactional, so * when rollback is executed in second step then whole actions inside * createCustomerComplexProxyBasedExampleAllTransactional() are rolledback */ @Test @Transactional public void shouldNotCreateAnyItemsDuringExecution() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // first customer is ok, second has wrong data (too long city), rollback is executed in second // step // here whole method is rolledback customerService.createCustomerComplexProxyBasedExampleAllTransactional( okCustomer, customerWithTooLongCity); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); assertEquals(new Integer(0), personVol); assertEquals(new Integer(0), addressVol); } }
public Order criarPedido(Order pedido, String emailCliente, String senhaCliente) throws CustomerNotFoundException { Customer cliente = clienteServices.findByEmailAndSenha(emailCliente, senhaCliente); if (cliente == null) { throw new CustomerNotFoundException(); } pedido.setData(new Date()); pedido.setCliente(cliente); em.persist(pedido); return pedido; }
@Test public void testFindCustomerByEmail() { Customer c1 = new Customer(); String email = "*****@*****.**"; c1.setEmail(email); doReturn(c1).when(customerDaoMock).findByEmail(email); Customer customer = service.findCustomerByEmail(email); verify(customerDaoMock).findByEmail(email); verifyNoMoreInteractions(customerDaoMock); Assert.assertEquals(email, customer.getEmail()); }
/** * In this example method is not transactional * * @throws Exception */ @Test public void shouldRollbackOnlyAddress() throws Exception { try { // customer is wrong - it is going to be exception customerService.createCustomerNoTransactional(customerWithTooLongCity); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); assertEquals(new Integer(0), addressVol); assertEquals(new Integer(1), personVol); } }
public TravelOffice() { MailServiceImpl mailServiceImpl = new MailServiceImpl(); mailServiceImpl.setSmtpHost("test"); mailServiceImpl.setSmtpPass("test"); mailServiceImpl.setUseSSL(true); mailServiceImpl.setSmtpUser("tets"); mailService = mailServiceImpl; newsletterService = new NewsletterServiceImpl(customerService, tripService, mailService); customerService.loadFromXml(); }
/** * In this example there is transaction during execution createCustomerComplexInsideSupport_Trans, * exception is thrown during customer creation */ @Test public void shouldRollbackAllCreationRoleAddressAndPerson() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is wrong - it is going to be exception - first roles are created customerService.createCustomerComplexInsideSupport_WithTransOutside( customerWithTooLongCity, okRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // records created by insertCustomer method assertEquals(new Integer(0), addressVol); assertEquals(new Integer(0), personVol); // record created by createRoleTransactionSupport method assertEquals(new Integer(0), roleVol); } }
@Test public void testCanRemoveCustomer() { String sql = "insert into sys_user(user_name, customer_id, status) values('rgm', 99, 1)"; this.simpleJdbcTemplate.getJdbcOperations().update(sql); List<CustomerBean> customers = new ArrayList<CustomerBean>(); CustomerBean customer = new CustomerBean(); customer.setId(99); customers.add(customer); boolean result = service.canRemoveCustomer(customers); Assert.assertEquals(false, result); }
/** * Exception is thrown during customer creation, no transation and no support transaction inside * role creation */ @Test public void shouldCreatePersonAndRolesButNoAddress() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is wrong - it is going to be exception customerService.createCustomerComplexInsideNotSupported_NoTransOutside( customerWithTooLongCity, okRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // records created by insertCustomer method - no transaction so executed independently but in // address insertion no because of exception - in this case is rollback assertEquals(new Integer(0), addressVol); assertEquals(new Integer(1), personVol); // record created by createRoleTransactionNotSupported method - no transaction so commited assertEquals(new Integer(1), roleVol); } }
@Test public void testFindAllCustomers() { Customer c1 = new Customer(); c1.setId(1L); Customer c2 = new Customer(); c2.setId(2L); doReturn(toList(new Customer[] {c1, c2})).when(customerDaoMock).findAll(); Collection<Customer> customers = service.findAllCustomers(); verify(customerDaoMock).findAll(); verifyNoMoreInteractions(orderDaoMock); Assert.assertEquals(2, customers.size()); }
/** * Exception is thrown during customer creation, no transation and no support transaction inside * role creation */ @Test public void shouldCreatePersonAndRolesButNoAddress2() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is wrong - it is going to be exception customerService.createCustomerComplexInsideNotSupported_WithTransOutside( customerWithTooLongCity, okRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // records created by insertCustomer method - transactional so rollback all inserts in this // method, rollback because of problem during inserting address assertEquals(new Integer(0), addressVol); assertEquals(new Integer(0), personVol); // record created by createRoleTransactionNotSupported method - transaction is not supported // even if outside method is transactional - so in this example role record is commited assertEquals(new Integer(1), roleVol); } }
/** In this case */ @Test @Transactional public void shouldCreateNewTransactionForCreateRoles() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is OK, but wrongRole has too long name customerService.createCustomerComplexNewRequired_NoTransOutside( okCustomer, okRole, wrongRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // address and person are commited independently (because of no transactional) but commited assertEquals(new Integer(1), addressVol); assertEquals(new Integer(1), personVol); // here second role was to long and exception was thrown but bacause of transaction required // (and created) all steps in this method are rolledback assertEquals(new Integer(0), roleVol); } }
@Test public void testAddingProductsToCart() throws Exception { Customer customer = customerService.createCustomer("A", "Customer"); Purchase purchase = customerOrderService.createPurchase(customer.getId()); Product product1 = productService.createProduct("Widget1", "a widget that slices (but not dices)", 12.0); Product product2 = productService.createProduct("Widget2", "a widget that dices (but not slices)", 7.5); LineItem one = customerOrderService.addProductToPurchase(purchase.getId(), product1.getId()); LineItem two = customerOrderService.addProductToPurchase(purchase.getId(), product2.getId()); purchase = customerOrderService.getPurchaseById(purchase.getId()); assertTrue(purchase.getTotal() == (product1.getPrice() + product2.getPrice())); assertEquals(one.getPurchase().getId(), purchase.getId()); assertEquals(two.getPurchase().getId(), purchase.getId()); // this part requires XA to work correctly customerOrderService.checkout(purchase.getId()); }
@Test public void testGetCustomer() { Customer customer = customerService.getCustomer(getMaxCustomerId()); assertEquals(customer.getFullName().getSurName(), "test"); }
public int addCustomer(JSONObject jObj) { int cid = 0; String emailId = null; Customer customer = new Customer(); customer.setCreatedAt(Utilities.getCurrentDateTime()); customer.setUpdatedAt(Utilities.getCurrentDateTime()); try { customer.setCustomerGroupId(jObj.getInt("customerGroup")); emailId = jObj.getString("email"); System.out.println("EMAIL ID:" + emailId); customer.setEmail(emailId); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (null == emailId) { return cid; } else if (isEmailAlreadyExists(emailId)) { return cid; } customerService.addcustomer(customer); cid = customer.getId(); System.out.println("customer==>" + customer.toString()); List<Entities> listAttributes = this.entitiesService.listAttributes(); HashMap<Integer, String> attrs = getAttributesList(listAttributes, "Customer"); System.out.println("attrs==>" + attrs.toString()); for (Entry<Integer, String> entry : attrs.entrySet()) { Integer attrId = entry.getKey(); String attrName = entry.getValue(); String attrValue = null; try { attrValue = jObj.getString(attrName); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (null != attrValue) { CustomerEntityValues custAttrValue = new CustomerEntityValues(); custAttrValue.setCustomerId(cid); custAttrValue.setLanguage("enUS"); custAttrValue.setAttributeId(attrId); custAttrValue.setValue(attrValue); customerEntityValuesService.addCustomerAttributeValues(custAttrValue); System.out.println("custAttrValue-->" + custAttrValue.toString()); } } return cid; /* //Rest call final String SERVER_URI = "http://localhost:8080/jvoidattributes/listCustomerAttriburtes"; RestTemplate restTemplate = new RestTemplate(); String returnString = restTemplate.getForObject(SERVER_URI, String.class); JSONObject returnJsonObj = null; try { returnJsonObj = new JSONObject(returnString); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("returnJsonObj=>"+returnJsonObj); JSONArray arr = null; try { arr = returnJsonObj.getJSONArray("Attributes"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i=0; i<arr.length(); i++) { try { String attrValue = arr.getJSONObject(i).getString("code"); Integer attrId = Integer.parseInt(arr.getJSONObject(i).getString("id")); System.out.println("id = "+attrId+" code = "+attrValue); CustomerAttributeValues custAttrValue = new CustomerAttributeValues(); custAttrValue.setCustomerId(customer.getId());//get it from customer table custAttrValue.setLanguage("enUS"); custAttrValue.setAttributeId(attrId); custAttrValue.setValue(customerMaster.toAttributedHashMap().get(attrValue)); customerAttributeValuesService.addCustomerAttributeValues(custAttrValue); System.out.println("custAttrValue-->"+custAttrValue.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } */ }
@Test public void testUpdateCustomer() { Customer customer = customerService.getCustomer(getMaxCustomerId()); customer.getFullName().setFirstName("test1"); assert (customerService.updateCustomer(customer)); }
public void displayCustomers() { customerService.displayCustomers(); }
public List<Customer> loadCustomers() { return customerService.loadCustomers(); }
public void saveCustomers() { customerService.saveCustomers(); }
@Test public void testDeleteCustomer() { assert (customerService.removeCustomer(getMaxCustomerId())); }
public void addCustomer(Customer c) { customerService.addCustomer(c); }