use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.
the class MetadataDatasetTest method testSearchOnTypes.
@Test
public void testSearchOnTypes() throws Exception {
MetadataEntity myField1 = MetadataEntity.builder(MetadataEntity.ofDataset(NamespaceId.DEFAULT.getEntityName(), "myDs")).appendAsType("field", "myField1").build();
MetadataEntity myField2 = MetadataEntity.builder(MetadataEntity.ofDataset(NamespaceId.DEFAULT.getEntityName(), "myDs")).appendAsType("field", "myField2").build();
final MetadataEntry myFieldEntry1 = new MetadataEntry(myField1, "testKey1", "testValue1");
final MetadataEntry myFieldEntry2 = new MetadataEntry(myField2, "testKey2", "testValue2");
txnl.execute(() -> {
dataset.addProperty(myField1, "testKey1", "testValue1");
dataset.addProperty(myField2, "testKey2", "testValue2");
});
// Search for it based on value
txnl.execute(() -> {
List<MetadataEntry> results = searchByDefaultIndex("default", "field:myField1", ALL_TYPES);
Assert.assertEquals(ImmutableList.of(myFieldEntry1), results);
// should return both fields
results = searchByDefaultIndex("default", "field:myFie*", ALL_TYPES);
Assert.assertEquals(ImmutableList.of(myFieldEntry1, myFieldEntry2), results);
results = searchByDefaultIndex("default", "field*", ALL_TYPES);
Assert.assertEquals(ImmutableList.of(myFieldEntry1, myFieldEntry2), results);
});
}
use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.
the class MetadataDatasetTest method testCrossNamespaceDefaultSearch.
@Test
public void testCrossNamespaceDefaultSearch() throws Exception {
MetadataEntity ns1App = new NamespaceId("ns1").app("a").toMetadataEntity();
MetadataEntity ns2App = new NamespaceId("ns2").app("a").toMetadataEntity();
txnl.execute(() -> {
dataset.addProperty(ns1App, "k1", "v1");
dataset.addProperty(ns1App, "k2", "v2");
dataset.addProperty(ns2App, "k1", "v1");
});
SearchRequest request1 = new SearchRequest(null, "v1", ALL_TYPES, SortInfo.DEFAULT, 0, 10, 0, null, false, EnumSet.allOf(EntityScope.class));
SearchResults results = txnl.execute(() -> dataset.search(request1));
Set<MetadataEntry> actual = new HashSet<>(results.getResults());
Set<MetadataEntry> expected = new HashSet<>();
expected.add(new MetadataEntry(ns1App, "k1", "v1"));
expected.add(new MetadataEntry(ns2App, "k1", "v1"));
Assert.assertEquals(expected, actual);
SearchRequest request2 = new SearchRequest(null, "v2", ALL_TYPES, SortInfo.DEFAULT, 0, 10, 0, null, false, EnumSet.allOf(EntityScope.class));
results = txnl.execute(() -> dataset.search(request2));
Assert.assertEquals(Collections.singletonList(new MetadataEntry(ns1App, "k2", "v2")), results.getResults());
SearchRequest star = new SearchRequest(null, "*", ALL_TYPES, SortInfo.DEFAULT, 0, 10, 0, null, false, EnumSet.allOf(EntityScope.class));
results = txnl.execute(() -> dataset.search(star));
expected.add(new MetadataEntry(ns1App, "k2", "v2"));
Assert.assertEquals(expected, new HashSet<>(results.getResults()));
}
use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.
the class MetadataKeyTest method testVersionedEntitiesKey.
@Test
public void testVersionedEntitiesKey() {
// CDAP-13597 Metadata for versioned entity is version independent i.e if there are two application version v1
// and v2 and a tag 'tag1' is added to either one it will be be reflected to both as we don't store the
// application/schedule/programs with it's version. Following tests test that for such versioned entity the keys
// are the same i.e default version
// Key for versioned application/schedule/program should be the same
// application
// default version
ApplicationId applicationId1 = new ApplicationId("ns", "app");
// custom version
ApplicationId applicationId2 = new ApplicationId("ns", "app", "2");
// non-version Application metadata entity
MDSKey mdsValueKey = MetadataKey.createValueRowKey(applicationId1.toMetadataEntity(), "key1");
MetadataEntity actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
Assert.assertEquals(applicationId1.toMetadataEntity(), actual);
mdsValueKey = MetadataKey.createValueRowKey(applicationId2.toMetadataEntity(), "key1");
actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
Assert.assertEquals(applicationId1.toMetadataEntity(), actual);
// program
// default version
ProgramId programId1 = new ApplicationId("ns", "app").program(ProgramType.SERVICE, "s");
// custom version
ProgramId programId2 = new ApplicationId("ns", "app", "2").program(ProgramType.SERVICE, "s");
mdsValueKey = MetadataKey.createValueRowKey(programId1.toMetadataEntity(), "key1");
actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
Assert.assertEquals(programId1.toMetadataEntity(), actual);
mdsValueKey = MetadataKey.createValueRowKey(programId2.toMetadataEntity(), "key1");
actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
Assert.assertEquals(programId1.toMetadataEntity(), actual);
// schedule
// default version
ScheduleId scheduleId1 = new ApplicationId("ns", "app").schedule("s");
// custom version
ScheduleId scheduleId2 = new ApplicationId("ns", "app", "2").schedule("s");
mdsValueKey = MetadataKey.createValueRowKey(scheduleId1.toMetadataEntity(), "key1");
actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
Assert.assertEquals(scheduleId1.toMetadataEntity(), actual);
mdsValueKey = MetadataKey.createValueRowKey(scheduleId2.toMetadataEntity(), "key1");
actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
Assert.assertEquals(scheduleId1.toMetadataEntity(), actual);
}
use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.
the class MetadataKeyTest method testGetMetadataEntityFromKey.
@Test
public void testGetMetadataEntityFromKey() {
ApplicationId expectedAppId = new ApplicationId("ns1", "app1");
MDSKey mdsValueKey = MetadataKey.createValueRowKey(expectedAppId.toMetadataEntity(), "key1");
MDSKey mdsIndexKey = MetadataKey.createIndexRowKey(expectedAppId.toMetadataEntity(), "key1", "value1");
// check that we can get MetadataEntity from value and index key
MetadataEntity actualAppId = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
Assert.assertEquals(expectedAppId.toMetadataEntity(), actualAppId);
actualAppId = MetadataKey.extractMetadataEntityFromKey(mdsIndexKey.getKey());
Assert.assertEquals(expectedAppId.toMetadataEntity(), actualAppId);
}
use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.
the class MetadataTest method testToEntityId.
@Test
public void testToEntityId() {
// should be able to get get an EntityId if Metadata belong to a cdap entity id
DatasetId myDs = NamespaceId.DEFAULT.dataset("myDs");
MetadataDataset.Record metadata1 = new MetadataDataset.Record(myDs);
Assert.assertEquals(myDs, metadata1.getEntityId());
MetadataEntity metadataEntity = MetadataEntity.builder(MetadataEntity.ofDataset(NamespaceId.DEFAULT.getEntityName(), "myDs")).appendAsType("field", "myField").build();
MetadataDataset.Record metadata2 = new MetadataDataset.Record(metadataEntity);
try {
metadata2.getEntityId();
Assert.fail();
} catch (IllegalArgumentException e) {
// expected
}
}
Aggregations