Search in sources :

Example 26 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class JpaDataStoreTest method verifyJPQLGeneratorRegistration.

@Test
public void verifyJPQLGeneratorRegistration() {
    @Include(rootLevel = false)
    @Entity
    class Test {

        @Id
        private long id;

        @JPQLFilterFragment(operator = Operator.IN, generator = TestGenerator.class)
        private String name;
    }
    EntityType mockType = mock(EntityType.class);
    when(mockType.getJavaType()).thenReturn(Test.class);
    Metamodel mockModel = mock(Metamodel.class);
    when(mockModel.getEntities()).thenReturn(Sets.newHashSet(mockType));
    EntityManager managerMock = mock(EntityManager.class);
    when(managerMock.getMetamodel()).thenReturn(mockModel);
    JpaDataStore store = new JpaDataStore(() -> managerMock, unused -> null);
    EntityDictionary dictionary = EntityDictionary.builder().build();
    try {
        store.populateEntityDictionary(dictionary);
        assertNotNull(FilterTranslator.lookupJPQLGenerator(Operator.IN, ClassType.of(Test.class), "name"));
    } finally {
        FilterTranslator.registerJPQLGenerator(Operator.IN, ClassType.of(Test.class), "name", null);
    }
}
Also used : EntityType(javax.persistence.metamodel.EntityType) Entity(javax.persistence.Entity) EntityManager(javax.persistence.EntityManager) Test(org.junit.jupiter.api.Test) Include(com.yahoo.elide.annotation.Include) Metamodel(javax.persistence.metamodel.Metamodel) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test)

Example 27 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class TableTypeTest method testHiddenTableAnnotations.

@Test
void testHiddenTableAnnotations() throws Exception {
    Table testTable = Table.builder().cardinality("medium").description("A test table").friendlyName("foo").table("table1").name("Table").hidden(true).schema("db1").category("category1").build();
    TableType testType = new TableType(testTable);
    Include include = (Include) testType.getAnnotation(Include.class);
    assertNotNull(include);
    TableMeta tableMeta = (TableMeta) testType.getAnnotation(TableMeta.class);
    assertNotNull(tableMeta);
    assertTrue(tableMeta.isHidden());
}
Also used : Table(com.yahoo.elide.modelconfig.model.Table) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) Include(com.yahoo.elide.annotation.Include) TableMeta(com.yahoo.elide.datastores.aggregation.annotation.TableMeta) Test(org.junit.jupiter.api.Test)

Example 28 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class TableTypeTest method testTableAnnotations.

@Test
void testTableAnnotations() throws Exception {
    Set<String> tags = new HashSet<>(Arrays.asList("tag1", "tag2"));
    Set<String> hints = new HashSet<>(Arrays.asList("hint1", "hint2"));
    Table testTable = Table.builder().cardinality("medium").description("A test table").friendlyName("foo").table("table1").name("Table").schema("db1").category("category1").readAccess("Admin").dbConnectionName("dbConn").isFact(true).filterTemplate("a==b").tags(tags).hints(hints).build();
    TableType testType = new TableType(testTable);
    Include include = (Include) testType.getAnnotation(Include.class);
    assertEquals("Table", include.name());
    FromTable fromTable = (FromTable) testType.getAnnotation(FromTable.class);
    assertEquals("db1.table1", fromTable.name());
    assertEquals("dbConn", fromTable.dbConnectionName());
    TableMeta tableMeta = (TableMeta) testType.getAnnotation(TableMeta.class);
    assertEquals("foo", tableMeta.friendlyName());
    assertEquals(CardinalitySize.MEDIUM, tableMeta.size());
    assertEquals("A test table", tableMeta.description());
    assertEquals("category1", tableMeta.category());
    assertTrue(tableMeta.isFact());
    assertEquals(tags, new HashSet<>(Arrays.asList(tableMeta.tags())));
    assertEquals(hints, new HashSet<>(Arrays.asList(tableMeta.hints())));
    assertEquals("a==b", tableMeta.filterTemplate());
    ReadPermission readPermission = (ReadPermission) testType.getAnnotation(ReadPermission.class);
    assertEquals("Admin", readPermission.expression());
}
Also used : Table(com.yahoo.elide.modelconfig.model.Table) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) Include(com.yahoo.elide.annotation.Include) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) TableMeta(com.yahoo.elide.datastores.aggregation.annotation.TableMeta) ReadPermission(com.yahoo.elide.annotation.ReadPermission) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 29 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class SwaggerBuilderTest method testSortParameter.

@Test
public void testSortParameter() {
    @Entity
    @Include
    class NothingToSort {

        @Id
        long name;
    }
    EntityDictionary entityDictionary = EntityDictionary.builder().build();
    entityDictionary.bindEntity(NothingToSort.class);
    Info info = new Info().title("Test Service").version(NO_VERSION);
    SwaggerBuilder builder = new SwaggerBuilder(entityDictionary, info);
    Swagger testSwagger = builder.build();
    List<Parameter> params = testSwagger.getPaths().get("/nothingToSort").getGet().getParameters();
    QueryParameter sortParam = (QueryParameter) params.stream().filter((param) -> param.getName().equals("sort")).findFirst().get();
    assertEquals("query", sortParam.getIn());
    List<String> sortValues = Arrays.asList("id", "-id");
    assertEquals(sortValues, ((StringProperty) sortParam.getItems()).getEnum());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Resource(com.yahoo.elide.swagger.model.Resource) Arrays(java.util.Arrays) Data(com.yahoo.elide.swagger.property.Data) Swagger(io.swagger.models.Swagger) Tag(io.swagger.models.Tag) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Include(com.yahoo.elide.annotation.Include) StringProperty(io.swagger.models.properties.StringProperty) HashMap(java.util.HashMap) Publisher(example.models.Publisher) ArrayProperty(io.swagger.models.properties.ArrayProperty) Model(io.swagger.models.Model) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Author(example.models.Author) BeforeAll(org.junit.jupiter.api.BeforeAll) Path(io.swagger.models.Path) Map(java.util.Map) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) Book(example.models.Book) RefProperty(io.swagger.models.properties.RefProperty) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Operation(io.swagger.models.Operation) Datum(com.yahoo.elide.swagger.property.Datum) Property(io.swagger.models.properties.Property) Id(javax.persistence.Id) Entity(javax.persistence.Entity) BodyParameter(io.swagger.models.parameters.BodyParameter) Set(java.util.Set) Parameter(io.swagger.models.parameters.Parameter) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Info(io.swagger.models.Info) QueryParameter(io.swagger.models.parameters.QueryParameter) Test(org.junit.jupiter.api.Test) Response(io.swagger.models.Response) ObjectProperty(io.swagger.models.properties.ObjectProperty) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Relationship(com.yahoo.elide.swagger.property.Relationship) Optional(java.util.Optional) Entity(javax.persistence.Entity) QueryParameter(io.swagger.models.parameters.QueryParameter) Swagger(io.swagger.models.Swagger) Include(com.yahoo.elide.annotation.Include) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) Info(io.swagger.models.Info) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test)

Example 30 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class AggregationStorePermissionExecutorTest method testUserPermissions.

@Test
public void testUserPermissions() {
    @Entity
    @Include
    @Value
    @ReadPermission(expression = "user none or filter check")
    class Model {

        String filterDim;

        long metric;

        long metric2;

        @ReadPermission(expression = "user all")
        public String getFilterDim() {
            return filterDim;
        }

        @ReadPermission(expression = "user none")
        public long getMetric() {
            return metric;
        }
    }
    com.yahoo.elide.core.RequestScope scope = bindAndgetRequestScope(Model.class);
    PermissionExecutor executor = scope.getPermissionExecutor();
    // evaluated expression = user all
    Assertions.assertEquals(ExpressionResult.PASS, executor.checkUserPermissions(ClassType.of(Model.class), ReadPermission.class, "filterDim"));
    // evaluated expression = user none -> ForbiddenAccess
    Assertions.assertThrows(ForbiddenAccessException.class, () -> executor.checkUserPermissions(ClassType.of(Model.class), ReadPermission.class, "metric"));
    // evaluated expression = null -> false
    Assertions.assertEquals(ExpressionResult.PASS, executor.checkSpecificFieldPermissions(new PersistentResource(new Model("dim1", 0, 1), "1", scope), null, ReadPermission.class, "metric2"));
    // evaluated expression = (user none or filter check) AND (user all OR user none)
    Assertions.assertEquals(ExpressionResult.DEFERRED, executor.checkUserPermissions(ClassType.of(Model.class), ReadPermission.class, new HashSet<>(Arrays.asList("filterDim", "metric"))));
    // evaluated expression = (user all OR user none)
    Assertions.assertEquals(ExpressionResult.PASS, executor.checkPermission(ReadPermission.class, new PersistentResource(new Model("dim1", 0, 1), "1", scope), new HashSet<>(Arrays.asList("filterDim", "metric"))));
    // evaluated expression = (user none OR null)
    Assertions.assertEquals(ExpressionResult.PASS, executor.checkPermission(ReadPermission.class, new PersistentResource(new Model("dim1", 0, 1), "1", scope), new HashSet<>(Arrays.asList("metric", "metric2"))));
}
Also used : Entity(javax.persistence.Entity) PersistentResource(com.yahoo.elide.core.PersistentResource) Value(lombok.Value) AggregationStorePermissionExecutor(com.yahoo.elide.core.security.executors.AggregationStorePermissionExecutor) Include(com.yahoo.elide.annotation.Include) ReadPermission(com.yahoo.elide.annotation.ReadPermission) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Include (com.yahoo.elide.annotation.Include)46 Test (org.junit.jupiter.api.Test)41 Entity (javax.persistence.Entity)37 RequestScope (com.yahoo.elide.core.RequestScope)26 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)19 HashSet (java.util.HashSet)17 PersistentResource (com.yahoo.elide.core.PersistentResource)16 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)12 ReadPermission (com.yahoo.elide.annotation.ReadPermission)11 Annotation (java.lang.annotation.Annotation)4 Date (java.util.Date)4 ApiVersion (com.yahoo.elide.annotation.ApiVersion)3 NO_VERSION (com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION)3 FromTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 Exclude (com.yahoo.elide.annotation.Exclude)2 AggregationStorePermissionExecutor (com.yahoo.elide.core.security.executors.AggregationStorePermissionExecutor)2 Expression (com.yahoo.elide.core.security.permissions.expressions.Expression)2 TableMeta (com.yahoo.elide.datastores.aggregation.annotation.TableMeta)2