Fix #644 Move duplicated validateTags method in tests to a common util

This commit is contained in:
sureshms 2021-10-03 18:03:42 -07:00
parent 08cefee56a
commit 5906e5a19c
7 changed files with 38 additions and 144 deletions

View File

@ -629,7 +629,7 @@ public class ChartResourceTest extends CatalogApplicationTest {
assertEquals(expectedService.getId(), chart.getService().getId());
assertEquals(expectedService.getType(), chart.getService().getType());
}
validateTags(expectedTags, chart.getTags());
TestUtils.validateTags(expectedTags, chart.getTags());
return chart;
}
@ -794,24 +794,4 @@ public class ChartResourceTest extends CatalogApplicationTest {
checkUserFollowing(userId, chartId, false, authHeaders);
return getChart;
}
private static void validateTags(List<TagLabel> expectedList, List<TagLabel> actualList)
throws HttpResponseException {
if (expectedList == null) {
return;
}
// When tags from the expected list is added to an entity, the derived tags for those tags are automatically added
// So add to the expectedList, the derived tags before validating the tags
List<TagLabel> updatedExpectedList = new ArrayList<>(expectedList);
for (TagLabel expected : expectedList) {
List<TagLabel> derived = EntityUtil.getDerivedTags(expected, TagResourceTest.getTag(expected.getTagFQN(),
adminAuthHeaders()));
updatedExpectedList.addAll(derived);
}
updatedExpectedList = updatedExpectedList.stream().distinct().collect(Collectors.toList());
assertTrue(actualList.containsAll(updatedExpectedList));
assertTrue(updatedExpectedList.containsAll(actualList));
}
}

View File

@ -673,7 +673,7 @@ public class DashboardResourceTest extends CatalogApplicationTest {
assertEquals(expectedService.getType(), dashboard.getService().getType());
}
validateDashboardCharts(dashboard, charts);
validateTags(expectedTags, dashboard.getTags());
TestUtils.validateTags(expectedTags, dashboard.getTags());
return dashboard;
}
@ -692,25 +692,6 @@ public class DashboardResourceTest extends CatalogApplicationTest {
}
}
private static void validateTags(List<TagLabel> expectedList, List<TagLabel> actualList)
throws HttpResponseException {
if (expectedList == null) {
return;
}
// When tags from the expected list is added to an entity, the derived tags for those tags are automatically added
// So add to the expectedList, the derived tags before validating the tags
List<TagLabel> updatedExpectedList = new ArrayList<>(expectedList);
for (TagLabel expected : expectedList) {
List<TagLabel> derived = EntityUtil.getDerivedTags(expected, TagResourceTest.getTag(expected.getTagFQN(),
adminAuthHeaders()));
updatedExpectedList.addAll(derived);
}
updatedExpectedList = updatedExpectedList.stream().distinct().collect(Collectors.toList());
assertTrue(actualList.containsAll(updatedExpectedList));
assertTrue(updatedExpectedList.containsAll(actualList));
}
private Dashboard patchDashboardAttributesAndCheck(Dashboard dashboard, String newDescription,
EntityReference newOwner, List<TagLabel> tags,
Map<String, String> authHeaders)

View File

@ -54,7 +54,6 @@ import org.openmetadata.catalog.type.TableJoins;
import org.openmetadata.catalog.type.TableProfile;
import org.openmetadata.catalog.type.TableType;
import org.openmetadata.catalog.type.TagLabel;
import org.openmetadata.catalog.util.EntityUtil;
import org.openmetadata.catalog.util.EntityUtil.Fields;
import org.openmetadata.catalog.util.JsonUtils;
import org.openmetadata.catalog.util.RestUtil;
@ -75,7 +74,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import static java.util.Collections.singletonList;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
@ -476,8 +474,8 @@ public class TableResourceTest extends CatalogApplicationTest {
.withDataLength(10).withFullyQualifiedName(table.getFullyQualifiedName() + ".c2").withTags(tags));
table = updateAndCheckTable(request.withColumns(updatedColumns), OK, adminAuthHeaders());
assertEquals(2, table.getColumns().size());
validateTags(updatedColumns.get(0).getTags(), table.getColumns().get(0).getTags());
validateTags(updatedColumns.get(1).getTags(), table.getColumns().get(1).getTags());
TestUtils.validateTags(updatedColumns.get(0).getTags(), table.getColumns().get(0).getTags());
TestUtils.validateTags(updatedColumns.get(1).getTags(), table.getColumns().get(1).getTags());
// Ensure tag usage counts are updated - column c2 added both address and bank tags
assertEquals(tagCategoryUsageCount + 4, getTagCategoryUsageCount("user", userAuthHeaders()));
@ -490,7 +488,7 @@ public class TableResourceTest extends CatalogApplicationTest {
updatedColumns.remove(1);
table = updateAndCheckTable(request.withColumns(updatedColumns), OK, adminAuthHeaders());
assertEquals(1, table.getColumns().size());
validateTags(columns.get(0).getTags(), table.getColumns().get(0).getTags());
TestUtils.validateTags(columns.get(0).getTags(), table.getColumns().get(0).getTags());
// Ensure tag usage counts are updated to reflect removal of column c2
assertEquals(tagCategoryUsageCount + 2, getTagCategoryUsageCount("user", userAuthHeaders()));
@ -1203,29 +1201,6 @@ public class TableResourceTest extends CatalogApplicationTest {
assertEquals(table.getDatabase().getName(), DATABASE.getName());
}
private static void validateTags(List<TagLabel> expectedList, List<TagLabel> actualList)
throws HttpResponseException {
if (expectedList == null) {
return;
}
// When tags from the expected list is added to an entity, the derived tags for those tags are automatically added
// So add to the expectedList, the derived tags before validating the tags
List<TagLabel> updatedExpectedList = new ArrayList<>(expectedList);
for (TagLabel expected : expectedList) {
List<TagLabel> derived = EntityUtil.getDerivedTags(expected, TagResourceTest.getTag(expected.getTagFQN(),
adminAuthHeaders()));
updatedExpectedList.addAll(derived);
}
updatedExpectedList = updatedExpectedList.stream().distinct().collect(Collectors.toList());
updatedExpectedList.sort(new TagLabelComparator());
actualList.sort(new TagLabelComparator());
assertEquals(updatedExpectedList.size(), actualList.size());
for (int i = 0; i < actualList.size(); i++) {
assertEquals(updatedExpectedList.get(i), actualList.get(i));
}
}
public static Table createTable(CreateTable create, Map<String, String> authHeaders) throws HttpResponseException {
return TestUtils.post(CatalogApplicationTest.getResource("tables"), create, Table.class, authHeaders);
}
@ -1260,7 +1235,7 @@ public class TableResourceTest extends CatalogApplicationTest {
// Validate table constraints
assertEquals(expectedTableConstraints, table.getTableConstraints());
validateTags(expectedTags, table.getTags());
TestUtils.validateTags(expectedTags, table.getTags());
TestUtils.validateEntityReference(table.getFollowers());
}
@ -1272,7 +1247,7 @@ public class TableResourceTest extends CatalogApplicationTest {
if (expectedColumn.getDataTypeDisplay() != null) {
assertEquals(expectedColumn.getDataTypeDisplay().toLowerCase(Locale.ROOT), actualColumn.getDataTypeDisplay());
}
validateTags(expectedColumn.getTags(), actualColumn.getTags());
TestUtils.validateTags(expectedColumn.getTags(), actualColumn.getTags());
// Check the nested columns
validateColumns(expectedColumn.getChildren(), actualColumn.getChildren());

View File

@ -35,7 +35,6 @@ import org.openmetadata.catalog.exception.CatalogExceptionMessage;
import org.openmetadata.catalog.resources.tasks.TaskResourceTest;
import org.openmetadata.catalog.resources.pipelines.PipelineResource.PipelineList;
import org.openmetadata.catalog.resources.services.PipelineServiceResourceTest;
import org.openmetadata.catalog.resources.tags.TagResourceTest;
import org.openmetadata.catalog.resources.teams.TeamResourceTest;
import org.openmetadata.catalog.resources.teams.UserResourceTest;
import org.openmetadata.catalog.type.EntityReference;
@ -55,7 +54,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import static java.util.Collections.singletonList;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
@ -673,7 +671,7 @@ public class PipelineResourceTest extends CatalogApplicationTest {
assertEquals(expectedService.getType(), pipeline.getService().getType());
}
validatePipelineTASKs(pipeline, tasks);
validateTags(expectedTags, pipeline.getTags());
TestUtils.validateTags(expectedTags, pipeline.getTags());
return pipeline;
}
@ -692,25 +690,6 @@ public class PipelineResourceTest extends CatalogApplicationTest {
}
}
private static void validateTags(List<TagLabel> expectedList, List<TagLabel> actualList)
throws HttpResponseException {
if (expectedList == null) {
return;
}
// When tags from the expected list is added to an entity, the derived tags for those tags are automatically added
// So add to the expectedList, the derived tags before validating the tags
List<TagLabel> updatedExpectedList = new ArrayList<>(expectedList);
for (TagLabel expected : expectedList) {
List<TagLabel> derived = EntityUtil.getDerivedTags(expected, TagResourceTest.getTag(expected.getTagFQN(),
adminAuthHeaders()));
updatedExpectedList.addAll(derived);
}
updatedExpectedList = updatedExpectedList.stream().distinct().collect(Collectors.toList());
assertTrue(actualList.containsAll(updatedExpectedList));
assertTrue(updatedExpectedList.containsAll(actualList));
}
private Pipeline patchPipelineAttributesAndCheck(Pipeline pipeline, String newDescription,
EntityReference newOwner, List<TagLabel> tags,
Map<String, String> authHeaders)

View File

@ -32,7 +32,6 @@ import org.openmetadata.catalog.entity.teams.Team;
import org.openmetadata.catalog.entity.teams.User;
import org.openmetadata.catalog.exception.CatalogExceptionMessage;
import org.openmetadata.catalog.resources.tasks.TaskResource.TaskList;
import org.openmetadata.catalog.resources.tags.TagResourceTest;
import org.openmetadata.catalog.resources.teams.TeamResourceTest;
import org.openmetadata.catalog.resources.teams.UserResourceTest;
import org.openmetadata.catalog.type.EntityReference;
@ -49,11 +48,9 @@ import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response.Status;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.CONFLICT;
@ -65,7 +62,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openmetadata.catalog.exception.CatalogExceptionMessage.entityNotFound;
import static org.openmetadata.catalog.exception.CatalogExceptionMessage.readOnlyAttribute;
import static org.openmetadata.catalog.util.TestUtils.LONG_ENTITY_NAME;
@ -588,7 +584,7 @@ public class TaskResourceTest extends CatalogApplicationTest {
assertEquals(expectedService.getId(), task.getService().getId());
assertEquals(expectedService.getType(), task.getService().getType());
}
validateTags(expectedTags, task.getTags());
TestUtils.validateTags(expectedTags, task.getTags());
return task;
}
@ -690,29 +686,9 @@ public class TaskResourceTest extends CatalogApplicationTest {
.withTaskUrl(new URI("http://localhost:0"));
}
private static void validateTags(List<TagLabel> expectedList, List<TagLabel> actualList)
throws HttpResponseException {
if (expectedList == null) {
return;
}
// When tags from the expected list is added to an entity, the derived tags for those tags are automatically added
// So add to the expectedList, the derived tags before validating the tags
List<TagLabel> updatedExpectedList = new ArrayList<>(expectedList);
for (TagLabel expected : expectedList) {
List<TagLabel> derived = EntityUtil.getDerivedTags(expected, TagResourceTest.getTag(expected.getTagFQN(),
adminAuthHeaders()));
updatedExpectedList.addAll(derived);
}
updatedExpectedList = updatedExpectedList.stream().distinct().collect(Collectors.toList());
assertTrue(actualList.containsAll(updatedExpectedList));
assertTrue(updatedExpectedList.containsAll(actualList));
}
public static PipelineService createService(CreatePipelineService create,
Map<String, String> authHeaders) throws HttpResponseException {
return TestUtils.post(CatalogApplicationTest.getResource("services/pipelineServices"),
create, PipelineService.class, authHeaders);
}
}

View File

@ -32,7 +32,6 @@ import org.openmetadata.catalog.entity.teams.Team;
import org.openmetadata.catalog.entity.teams.User;
import org.openmetadata.catalog.exception.CatalogExceptionMessage;
import org.openmetadata.catalog.resources.services.MessagingServiceResourceTest;
import org.openmetadata.catalog.resources.tags.TagResourceTest;
import org.openmetadata.catalog.resources.teams.TeamResourceTest;
import org.openmetadata.catalog.resources.teams.UserResourceTest;
import org.openmetadata.catalog.resources.topics.TopicResource.TopicList;
@ -48,13 +47,10 @@ import org.slf4j.LoggerFactory;
import javax.json.JsonPatch;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response.Status;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import static java.util.Collections.singletonList;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.CONFLICT;
import static javax.ws.rs.core.Response.Status.CREATED;
@ -85,7 +81,6 @@ public class TopicResourceTest extends CatalogApplicationTest {
public static EntityReference TEAM_OWNER1;
public static EntityReference KAFKA_REFERENCE;
public static EntityReference PULSAR_REFERENCE;
public static final TagLabel USER_ADDRESS_TAG_LABEL = new TagLabel().withTagFQN("User.Address");
public static final TagLabel TIER1_TAG_LABEL = new TagLabel().withTagFQN("Tier.Tier1");
public static final TagLabel TIER2_TAG_LABEL = new TagLabel().withTagFQN("Tier.Tier2");
@ -624,7 +619,7 @@ public class TopicResourceTest extends CatalogApplicationTest {
assertEquals(expectedService.getId(), topic.getService().getId());
assertEquals(expectedService.getType(), topic.getService().getType());
}
validateTags(expectedTags, topic.getTags());
TestUtils.validateTags(expectedTags, topic.getTags());
return topic;
}
@ -789,24 +784,4 @@ public class TopicResourceTest extends CatalogApplicationTest {
checkUserFollowing(userId, topicId, false, authHeaders);
return getTopic;
}
private static void validateTags(List<TagLabel> expectedList, List<TagLabel> actualList)
throws HttpResponseException {
if (expectedList == null) {
return;
}
// When tags from the expected list is added to an entity, the derived tags for those tags are automatically added
// So add to the expectedList, the derived tags before validating the tags
List<TagLabel> updatedExpectedList = new ArrayList<>(expectedList);
for (TagLabel expected : expectedList) {
List<TagLabel> derived = EntityUtil.getDerivedTags(expected, TagResourceTest.getTag(expected.getTagFQN(),
adminAuthHeaders()));
updatedExpectedList.addAll(derived);
}
updatedExpectedList = updatedExpectedList.stream().distinct().collect(Collectors.toList());
assertTrue(actualList.containsAll(updatedExpectedList));
assertTrue(updatedExpectedList.containsAll(actualList));
}
}

View File

@ -16,11 +16,14 @@
package org.openmetadata.catalog.util;
import org.openmetadata.catalog.resources.databases.TableResourceTest.TagLabelComparator;
import org.openmetadata.catalog.resources.tags.TagResourceTest;
import org.openmetadata.catalog.security.CatalogOpenIdAuthorizationRequestFilter;
import org.openmetadata.catalog.type.EntityReference;
import org.openmetadata.catalog.type.JdbcInfo;
import org.apache.http.client.HttpResponseException;
import org.eclipse.jetty.http.HttpStatus;
import org.openmetadata.catalog.type.TagLabel;
import javax.json.JsonObject;
import javax.json.JsonPatch;
@ -32,12 +35,14 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@ -210,6 +215,29 @@ public final class TestUtils {
return headers;
}
public static void validateTags(List<TagLabel> expectedList, List<TagLabel> actualList)
throws HttpResponseException {
if (expectedList == null) {
return;
}
// When tags from the expected list is added to an entity, the derived tags for those tags are automatically added
// So add to the expectedList, the derived tags before validating the tags
List<TagLabel> updatedExpectedList = new ArrayList<>(expectedList);
for (TagLabel expected : expectedList) {
List<TagLabel> derived = EntityUtil.getDerivedTags(expected, TagResourceTest.getTag(expected.getTagFQN(),
adminAuthHeaders()));
updatedExpectedList.addAll(derived);
}
updatedExpectedList = updatedExpectedList.stream().distinct().collect(Collectors.toList());
updatedExpectedList.sort(new TagLabelComparator());
actualList.sort(new TagLabelComparator());
assertEquals(updatedExpectedList.size(), actualList.size());
for (int i = 0; i < actualList.size(); i++) {
assertEquals(updatedExpectedList.get(i), actualList.get(i));
}
}
public static Map<String, String> adminAuthHeaders() {
return authHeaders("admin@open-metadata.org");
}