@SuppressWarnings("unchecked") @Test public void testAppendCloudServiceGuidConstraint() throws Exception { CustomerDAO dao = new CustomerDAO(); // list List<String> list = new LinkedList<String>(); list.add("val1"); list.add("val2"); SearchConstraint sc = new SearchConstraint("TEST_PROP", SearchConstraintOperator.CONSTRAINT_IN_LIST, list); String got = dao.appendCloudServiceGuidConstraint(sc); String expected = CustomerDAO.SQL_CUSTOMER_QUERY_CLOUD_SERVICE_CONSTRAINT + "in (E'val1',E'val2'))"; assertEquals("Wrong constraint added.", expected, got); // equal sc = new SearchConstraint( ICustomerService.PROP_CLOUD_SERVICE_GUID, SearchConstraintOperator.CONSTRAINT_EQUALS, "val3"); got = dao.appendCloudServiceGuidConstraint(sc); expected = CustomerDAO.SQL_CUSTOMER_QUERY_CLOUD_SERVICE_CONSTRAINT + "= E'val3' and g_inner.disabled = false)"; assertEquals("Wrong constraint added.", expected, got); }
@Test public void testGetCustomerWithConnection() throws Exception { CustomerDAO dao = EasyMock.createMockBuilder(CustomerDAO.class) .addMockedMethod("findCustomers", List.class, Connection.class) .createStrictMock(); Customer customer = EasyMock.createStrictMock(Customer.class); Connection connection = EasyMock.createStrictMock(Connection.class); int custId = 322; List<ICustomer> customerList = new LinkedList<ICustomer>(); customerList.add(customer); Capture<List<SearchConstraint>> capture = new Capture<List<SearchConstraint>>(); EasyMock.expect(dao.findCustomers(EasyMock.capture(capture), EasyMock.eq(connection))) .andReturn(customerList); EasyMock.replay(dao, customer, connection); assertEquals("Wrong customer returned.", customer, dao.getCustomer(custId, connection)); EasyMock.verify(dao, customer, connection); assertEquals("Wrong # of constraints.", 1, capture.getValue().size()); SearchConstraint constraint = capture.getValue().get(0); assertEquals( "Wrong property in constraint.", ICustomerService.PROP_CUSTOMER_ID, constraint.getProperty()); assertEquals( "Wrong operator in constraint.", SearchConstraintOperator.CONSTRAINT_EQUALS, constraint.getOperator()); assertEquals("Wrong value in constraint.", custId, constraint.getValue()); // no customers customerList.clear(); EasyMock.reset(dao, customer, connection); capture = new Capture<List<SearchConstraint>>(); EasyMock.expect(dao.findCustomers(EasyMock.capture(capture), EasyMock.eq(connection))) .andReturn(customerList); EasyMock.replay(dao, customer, connection); assertEquals( "Should not have found a customer returned.", null, dao.getCustomer(custId, connection)); EasyMock.verify(dao, customer, connection); }
@Test public void testNewGetPaymentsApiMethods() throws Exception { JbillingAPI api = JbillingAPIFactory.getAPI(); // Create a user with balance $1.00 UserWS user = com.sapienter.jbilling.server.user.WSTest.createUser(true, null, null); List<PaymentWS> payments = new ArrayList<PaymentWS>(); for (int i = 0; i < 5; i++) { payments.add( createPaymentWS( user.getUserId(), new DateTime().plusMonths(i).toDate(), String.valueOf(i))); } // get two latest payments except the latest one. Integer[] paymentsId = api.getLastPaymentsPage(user.getUserId(), 2, 1); assertEquals(2, paymentsId.length); assertEquals("3", api.getPayment(paymentsId[0]).getPaymentNotes()); assertEquals("2", api.getPayment(paymentsId[1]).getPaymentNotes()); // get the payments between next month and four months from now. Integer[] paymentsId2 = api.getPaymentsByDate( user.getUserId(), new DateTime().plusDays(1).toDate(), new DateTime().plusMonths(3).plusDays(1).toDate()); assertEquals(3, paymentsId2.length); assertEquals("3", api.getPayment(paymentsId2[0]).getPaymentNotes()); assertEquals("2", api.getPayment(paymentsId2[1]).getPaymentNotes()); assertEquals("1", api.getPayment(paymentsId2[2]).getPaymentNotes()); // Delete orders for (PaymentWS payment : payments) { api.deletePayment(payment.getId()); } // Delete user api.deleteUser(user.getUserId()); }
private Integer getOrCreateSuspendedStatus(JbillingAPI api) { List<AgeingWS> steps = Arrays.asList(api.getAgeingConfiguration(LANGUAGE_ID)); for (AgeingWS step : steps) { if (step.getSuspended().booleanValue()) { return step.getStatusId(); } } AgeingWS suspendStep = new AgeingWS(); suspendStep.setSuspended(Boolean.TRUE); suspendStep.setDays(Integer.valueOf(180)); suspendStep.setStatusStr("Ageing Step 180"); suspendStep.setFailedLoginMessage("You are suspended"); suspendStep.setWelcomeMessage("Welcome"); steps.add(suspendStep); api.saveAgeingConfiguration(steps.toArray(new AgeingWS[steps.size()]), LANGUAGE_ID); return getOrCreateOrderChangeStatusApply(api); }
@SuppressWarnings("unchecked") @Test public void testAppendDomainConstraint() throws Exception { CustomerDAO dao = new CustomerDAO(); // list List<String> list = new LinkedList<String>(); list.add("domain0"); list.add("domain1"); SearchConstraint sc = new SearchConstraint("TEST_PROP", SearchConstraintOperator.CONSTRAINT_IN_LIST, list); String got = dao.appendDomainConstraint(sc); String expected = CustomerDAO.SQL_CUSTOMER_QUERY_DOMAIN_CONSTRAINT + "in (E'domain0',E'domain1'))"; assertEquals("Wrong constraint added.", expected, got); // equal sc = new SearchConstraint("TEST_PROP", SearchConstraintOperator.CONSTRAINT_EQUALS, "domain3"); got = dao.appendDomainConstraint(sc); expected = CustomerDAO.SQL_CUSTOMER_QUERY_DOMAIN_CONSTRAINT + "= E'domain3')"; assertEquals("Wrong constraint added.", expected, got); }
@SuppressWarnings("unchecked") @Test public void testAppendConstraints() throws Exception { CustomerDAO dao = EasyMock.createMockBuilder(CustomerDAO.class) .addMockedMethod("appendDomainConstraint") .addMockedMethod("appendCloudServiceGuidConstraint") .addMockedMethod("appendCustomerPrefixConstraint") .addMockedMethod("appendUserStateConstraint") .createStrictMock(); List<SearchConstraint> constraints = new LinkedList<SearchConstraint>(); // domain constraint SearchConstraint sc = new SearchConstraint( ICustomerService.PROP_DOMAINS, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendDomainConstraint(EasyMock.eq(sc))).andReturn("a"); // cloud service guid sc = new SearchConstraint( ICustomerService.PROP_CLOUD_SERVICE_GUID, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendCloudServiceGuidConstraint(EasyMock.eq(sc))).andReturn("b"); sc = new SearchConstraint( ICustomerService.PROP_CLOUD_SERVICE_ANY_GUID, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendCloudServiceGuidConstraint(EasyMock.eq(sc))).andReturn("c"); // customer prefix sc = new SearchConstraint( ICustomerService.PROP_CUST_CAPABILITY_PREFIX, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); dao.appendCustomerPrefixConstraint(EasyMock.eq(sc), EasyMock.isA(StringBuffer.class)); EasyMock.expectLastCall(); // users in state sc = new SearchConstraint( ICustomerService.PROP_USERS_IN_STATE, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendUserStateConstraint(EasyMock.eq(sc))).andReturn("d"); // users NOT in state sc = new SearchConstraint( ICustomerService.PROP_NO_USERS_IN_STATE, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendUserStateConstraint(EasyMock.eq(sc))).andReturn("e"); StringBuilder whereClause = new StringBuilder(); EasyMock.replay(dao); dao.appendConstraints(constraints, whereClause); EasyMock.verify(dao); String expected = " and (a) and (b) and (c) and (not )"; assertEquals("Wrong where clause returned.", expected, whereClause.toString()); }
@Test public void testFindCustomerByGuid() throws Exception { CustomerDAO dao = EasyMock.createMockBuilder(CustomerDAO.class) .addMockedMethod("findCustomers", List.class) .createStrictMock(); // null guid EasyMock.replay(dao); try { dao.findCustomerByGuid(null, true); fail("Should have thrown an exception when null guid passed in."); } catch (IllegalArgumentException ex) { // expected } EasyMock.verify(dao); // no customer found EasyMock.reset(dao); boolean allowDeleted = true; String guid = "myGuid"; Capture<List<SearchConstraint>> capture = new Capture<List<SearchConstraint>>(); List<ICustomer> customerList = new LinkedList<ICustomer>(); EasyMock.expect(dao.findCustomers(EasyMock.capture(capture))).andReturn(customerList); EasyMock.replay(dao); assertNull("Should not have found a customer.", dao.findCustomerByGuid(guid, allowDeleted)); EasyMock.verify(dao); assertEquals("Wrong # of constraints.", 1, capture.getValue().size()); SearchConstraint constraint = capture.getValue().get(0); assertEquals( "Wrong property in constraint.", ICustomerService.PROP_CLOUD_SERVICE_ANY_GUID, constraint.getProperty()); assertEquals( "Wrong operator in constraint.", SearchConstraintOperator.CONSTRAINT_EQUALS, constraint.getOperator()); assertEquals("Wrong value in constraint.", guid, constraint.getValue()); // customer found and don't allow deleted EasyMock.reset(dao); allowDeleted = false; capture = new Capture<List<SearchConstraint>>(); Customer customer = EasyMock.createStrictMock(Customer.class); customerList.add(customer); EasyMock.expect(dao.findCustomers(EasyMock.capture(capture))).andReturn(customerList); EasyMock.replay(dao); assertEquals( "Should have found customer.", customer, dao.findCustomerByGuid(guid, allowDeleted)); EasyMock.verify(dao); assertEquals("Wrong # of constraints.", 1, capture.getValue().size()); constraint = capture.getValue().get(0); assertEquals( "Wrong property in constraint.", ICustomerService.PROP_CLOUD_SERVICE_GUID, constraint.getProperty()); assertEquals( "Wrong operator in constraint.", SearchConstraintOperator.CONSTRAINT_EQUALS, constraint.getOperator()); assertEquals("Wrong value in constraint.", guid, constraint.getValue()); }