@Test public void testQueryClauseWithOffsetAndLimit() { final Expression expr1 = TestBeanFactory.createExpressionBuilder() .setId(UUID.randomUUID()) .setService("my-test-service") .setCluster("my-test-cluster") .build(); final Expression expr2 = TestBeanFactory.createExpressionBuilder() .setId(UUID.randomUUID()) .setService("my-test-service") .setCluster("my-test-cluster") .build(); final Expression expr3 = TestBeanFactory.createExpressionBuilder() .setId(UUID.randomUUID()) .setService("my-test-service") .setCluster("my-test-cluster") .build(); exprRepo.addOrUpdateExpression(expr1, Organization.DEFAULT); exprRepo.addOrUpdateExpression(expr2, Organization.DEFAULT); exprRepo.addOrUpdateExpression(expr3, Organization.DEFAULT); final ExpressionQuery query = new DefaultExpressionQuery(exprRepo, Organization.DEFAULT); query.service(Optional.of("my-test-service")); query.cluster(Optional.of("my-test-cluster")); query.offset(Optional.of(2)); query.limit(2); final QueryResult<Expression> result = exprRepo.query(query); Assert.assertEquals(1, result.values().size()); Assert.assertEquals(expr3.getId(), result.values().get(0).getId()); }
@Test public void testQueryWithContainsAndServiceClause() { final Expression expr1 = TestBeanFactory.createExpressionBuilder() .setId(UUID.randomUUID()) .setMetric("my-contained-metric") .setService("my-service") .build(); final Expression expr2 = TestBeanFactory.createExpressionBuilder() .setId(UUID.randomUUID()) .setScript("my-contained-script") .setService("my-service") .build(); final Expression expr3 = TestBeanFactory.createExpressionBuilder().setId(UUID.randomUUID()).build(); exprRepo.addOrUpdateExpression(expr1, Organization.DEFAULT); exprRepo.addOrUpdateExpression(expr2, Organization.DEFAULT); exprRepo.addOrUpdateExpression(expr3, Organization.DEFAULT); final ExpressionQuery query = new DefaultExpressionQuery(exprRepo, Organization.DEFAULT); query.contains(Optional.of("contained")); query.service(Optional.of("my-service")); final QueryResult<Expression> result = exprRepo.query(query); Assert.assertEquals(2, result.values().size()); Assert.assertEquals(expr1.getId(), result.values().get(0).getId()); Assert.assertEquals(expr2.getId(), result.values().get(1).getId()); }
@Test public void testQueryClauseWithLimit() { final Expression expr1 = TestBeanFactory.createExpressionBuilder() .setId(UUID.randomUUID()) .setService("my-test-service") .setCluster("my-test-cluster") .build(); final Expression expr2 = TestBeanFactory.createExpressionBuilder() .setId(UUID.randomUUID()) .setService("my-test-service") .setCluster("my-test-cluster") .build(); exprRepo.addOrUpdateExpression(expr1, Organization.DEFAULT); exprRepo.addOrUpdateExpression(expr2, Organization.DEFAULT); final ExpressionQuery query1 = new DefaultExpressionQuery(exprRepo, Organization.DEFAULT); query1.service(Optional.of("my-test-service")); query1.cluster(Optional.of("my-test-cluster")); query1.limit(1); final QueryResult<Expression> result1 = exprRepo.query(query1); Assert.assertEquals(1, result1.values().size()); final ExpressionQuery query2 = new DefaultExpressionQuery(exprRepo, Organization.DEFAULT); query2.service(Optional.of("my-test-service")); query2.cluster(Optional.of("my-test-cluster")); query2.limit(2); final QueryResult<Expression> result2 = exprRepo.query(query2); Assert.assertEquals(2, result2.values().size()); }
@Test public void testGetExpressionCountWithMultipleExpr() throws IOException { Assert.assertEquals(0, exprRepo.getExpressionCount(Organization.DEFAULT)); try (Transaction transaction = Ebean.beginTransaction()) { final models.ebean.Expression ebeanExpression1 = TestBeanFactory.createEbeanExpression(); ebeanExpression1.setUuid(UUID.randomUUID()); ebeanExpression1.setOrganization( models.ebean.Organization.findByOrganization(Organization.DEFAULT)); Ebean.save(ebeanExpression1); final models.ebean.Expression ebeanExpression2 = TestBeanFactory.createEbeanExpression(); ebeanExpression2.setUuid(UUID.randomUUID()); ebeanExpression2.setOrganization( models.ebean.Organization.findByOrganization(Organization.DEFAULT)); Ebean.save(ebeanExpression2); transaction.commit(); } Assert.assertEquals(2, exprRepo.getExpressionCount(Organization.DEFAULT)); }
@Test public void addOrUpdateExpressionUpdateCase() throws IOException { final UUID uuid = UUID.randomUUID(); final models.ebean.Organization organization; try (Transaction transaction = Ebean.beginTransaction()) { final models.ebean.Expression ebeanExpression = TestBeanFactory.createEbeanExpression(); ebeanExpression.setUuid(uuid); organization = ebeanExpression.getOrganization(); Ebean.save(ebeanExpression); transaction.commit(); } final Expression actual = TestBeanFactory.createExpressionBuilder().setId(uuid).setCluster("new-cluster").build(); exprRepo.addOrUpdateExpression(actual, TestBeanFactory.organizationFrom(organization)); final Expression expected = exprRepo.get(uuid, TestBeanFactory.organizationFrom(organization)).get(); Assert.assertEquals(expected, actual); }
@Test public void addOrUpdateExpressionAddCase() { final UUID uuid = UUID.randomUUID(); Assert.assertFalse(exprRepo.get(uuid, Organization.DEFAULT).isPresent()); final Expression actual = TestBeanFactory.createExpressionBuilder().setId(uuid).build(); exprRepo.addOrUpdateExpression(actual, Organization.DEFAULT); final Optional<Expression> expected = exprRepo.get(uuid, Organization.DEFAULT); Assert.assertTrue(expected.isPresent()); Assert.assertEquals(expected.get(), actual); }
@Test public void testQueryClauseWithServiceOnly() { final Expression expr1 = TestBeanFactory.createExpressionBuilder() .setId(UUID.randomUUID()) .setService("my-test-service") .build(); final Expression expr2 = TestBeanFactory.createExpressionBuilder().setId(UUID.randomUUID()).build(); exprRepo.addOrUpdateExpression(expr1, Organization.DEFAULT); exprRepo.addOrUpdateExpression(expr2, Organization.DEFAULT); final ExpressionQuery successQuery = new DefaultExpressionQuery(exprRepo, Organization.DEFAULT); successQuery.service(Optional.of("my-test-service")); final QueryResult<Expression> successResult = exprRepo.query(successQuery); Assert.assertEquals(1, successResult.total()); final ExpressionQuery failQuery = new DefaultExpressionQuery(exprRepo, Organization.DEFAULT); failQuery.cluster(Optional.of("some-random-service")); final QueryResult<Expression> failResult = exprRepo.query(failQuery); Assert.assertEquals(0, failResult.total()); }
@Test public void testGetForValidId() throws IOException { final UUID uuid = UUID.randomUUID(); Assert.assertFalse(exprRepo.get(uuid, Organization.DEFAULT).isPresent()); final models.ebean.Expression ebeanExpression = TestBeanFactory.createEbeanExpression(); ebeanExpression.setUuid(uuid); ebeanExpression.setOrganization( models.ebean.Organization.findByOrganization(Organization.DEFAULT)); try (Transaction transaction = Ebean.beginTransaction()) { Ebean.save(ebeanExpression); transaction.commit(); } final Optional<Expression> expected = exprRepo.get(uuid, Organization.DEFAULT); Assert.assertTrue(expected.isPresent()); Assert.assertTrue(isExpressionEbeanEquivalent(expected.get(), ebeanExpression)); }
@Test(expected = PersistenceException.class) public void testThrowsExceptionWhenQueryFails() { final UUID uuid = UUID.randomUUID(); exprRepo.addOrUpdateExpression( TestBeanFactory.createExpressionBuilder().setId(uuid).setCluster("new-cluster").build(), Organization.DEFAULT); models.ebean.Expression ebeanExpression1 = Ebean.find(models.ebean.Expression.class).where().eq("uuid", uuid).findUnique(); models.ebean.Expression ebeanExpression2 = Ebean.find(models.ebean.Expression.class).where().eq("uuid", uuid).findUnique(); try (Transaction transaction = Ebean.beginTransaction()) { ebeanExpression1.setCluster("new-cluster1"); ebeanExpression2.setCluster("new-cluster2"); ebeanExpression2.save(); ebeanExpression1.save(); transaction.commit(); } catch (final IOException e) { // Do Nothing } }