From d15ffd8daf858c2fc95dd87afed1f5635d08bd64 Mon Sep 17 00:00:00 2001 From: sureshms Date: Sat, 16 Oct 2021 03:26:37 -0700 Subject: [PATCH] Fix #789 Make assertResponse shorter in Chart and Dashboard tests --- .../resources/charts/ChartResourceTest.java | 116 +++++++----------- .../dashboards/DashboardResourceTest.java | 92 ++++++-------- .../databases/DatabaseResourceTest.java | 5 +- .../openmetadata/catalog/util/TestUtils.java | 20 ++- 4 files changed, 99 insertions(+), 134 deletions(-) diff --git a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/charts/ChartResourceTest.java b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/charts/ChartResourceTest.java index c0549d52223..51bedcd6cb2 100644 --- a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/charts/ChartResourceTest.java +++ b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/charts/ChartResourceTest.java @@ -31,11 +31,10 @@ import org.openmetadata.catalog.entity.services.DashboardService; 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.charts.ChartResource.ChartList; import org.openmetadata.catalog.resources.services.DashboardServiceResourceTest; -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.charts.ChartResource.ChartList; import org.openmetadata.catalog.type.ChartType; import org.openmetadata.catalog.type.EntityReference; import org.openmetadata.catalog.type.TagLabel; @@ -51,13 +50,10 @@ 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 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; @@ -70,6 +66,7 @@ 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.ENTITY_ALREADY_EXISTS; 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; @@ -116,18 +113,15 @@ public class ChartResourceTest extends CatalogApplicationTest { public void post_chartWithLongName_400_badRequest(TestInfo test) { // Create chart with mandatory name field empty CreateChart create = create(test).withName(LONG_ENTITY_NAME); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createChart(create, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[name size must be between 1 and 64]"); + assertResponse(() -> createChart(create, adminAuthHeaders()), + BAD_REQUEST, "[name size must be between 1 and 64]"); } @Test public void post_chartAlreadyExists_409_conflict(TestInfo test) throws HttpResponseException { CreateChart create = create(test); createChart(create, adminAuthHeaders()); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createChart(create, adminAuthHeaders())); - assertResponse(exception, CONFLICT, CatalogExceptionMessage.ENTITY_ALREADY_EXISTS); + assertResponse(() -> createChart(create, adminAuthHeaders()), CONFLICT, ENTITY_ALREADY_EXISTS); } @Test @@ -153,26 +147,19 @@ public class ChartResourceTest extends CatalogApplicationTest { @Test public void post_chart_as_non_admin_401(TestInfo test) { CreateChart create = create(test); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createChart(create, authHeaders("test@open-metadata.org"))); - assertResponse(exception, FORBIDDEN, "Principal: CatalogPrincipal{name='test'} is not admin"); + assertResponse(() -> createChart(create, authHeaders("test@open-metadata.org")), + FORBIDDEN, "Principal: CatalogPrincipal{name='test'} is not admin"); } @Test public void post_chartWithoutRequiredFields_4xx(TestInfo test) { - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createChart(create(test).withName(null), adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[name must not be null]"); - - exception = assertThrows(HttpResponseException.class, () -> - createChart(create(test).withName(LONG_ENTITY_NAME), adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[name size must be between 1 and 64]"); - + assertResponse(() -> createChart(create(test).withName(null), adminAuthHeaders()), BAD_REQUEST, + "[name must not be null]"); + assertResponse(() -> createChart(create(test).withName(LONG_ENTITY_NAME), adminAuthHeaders()), BAD_REQUEST, + "[name size must be between 1 and 64]"); // Service is required field - exception = assertThrows(HttpResponseException.class, () -> - createChart(create(test).withService(null), adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[service must not be null]"); - + assertResponse(() -> createChart(create(test).withService(null), adminAuthHeaders()), BAD_REQUEST, + "[service must not be null]"); } @Test @@ -189,9 +176,8 @@ public class ChartResourceTest extends CatalogApplicationTest { public void post_chartWithNonExistentOwner_4xx(TestInfo test) { EntityReference owner = new EntityReference().withId(NON_EXISTENT_ENTITY).withType("user"); CreateChart create = create(test).withOwner(owner); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createChart(create, adminAuthHeaders())); - assertResponse(exception, NOT_FOUND, entityNotFound("User", NON_EXISTENT_ENTITY)); + assertResponse(() -> createChart(create, adminAuthHeaders()), NOT_FOUND, + entityNotFound("User", NON_EXISTENT_ENTITY)); } @Test @@ -213,25 +199,21 @@ public class ChartResourceTest extends CatalogApplicationTest { @Test public void get_chartListWithInvalidLimitOffset_4xx() { // Limit must be >= 1 and <= 1000,000 - HttpResponseException exception = assertThrows(HttpResponseException.class, () - -> listCharts(null, null, -1, null, null, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[query param limit must be greater than or equal to 1]"); + assertResponse(() -> listCharts(null, null, -1, null, null, adminAuthHeaders()), + BAD_REQUEST, "[query param limit must be greater than or equal to 1]"); - exception = assertThrows(HttpResponseException.class, () - -> listCharts(null, null, 0, null, null, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[query param limit must be greater than or equal to 1]"); + assertResponse(() ->listCharts(null, null, 0, null, null, adminAuthHeaders()), + BAD_REQUEST, "[query param limit must be greater than or equal to 1]"); - exception = assertThrows(HttpResponseException.class, () - -> listCharts(null, null, 1000001, null, null, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[query param limit must be less than or equal to 1000000]"); + assertResponse(() -> listCharts(null, null, 1000001, null, null, adminAuthHeaders()), + BAD_REQUEST, "[query param limit must be less than or equal to 1000000]"); } @Test public void get_chartListWithInvalidPaginationCursors_4xx() { // Passing both before and after cursors is invalid - HttpResponseException exception = assertThrows(HttpResponseException.class, () - -> listCharts(null, null, 1, "", "", adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "Only one of before or after query parameter allowed"); + assertResponse(() -> listCharts(null, null, 1, "", "", adminAuthHeaders()), + BAD_REQUEST, "Only one of before or after query parameter allowed"); } @Test @@ -374,9 +356,7 @@ public class ChartResourceTest extends CatalogApplicationTest { @Test public void get_nonExistentChart_404_notFound() { - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - getChart(NON_EXISTENT_ENTITY, adminAuthHeaders())); - assertResponse(exception, NOT_FOUND, + assertResponse(() -> getChart(NON_EXISTENT_ENTITY, adminAuthHeaders()), NOT_FOUND, entityNotFound(Entity.CHART, NON_EXISTENT_ENTITY)); } @@ -430,15 +410,13 @@ public class ChartResourceTest extends CatalogApplicationTest { UUID chartId = chart.getId(); String chartJson = JsonUtils.pojoToJson(chart); chart.setId(UUID.randomUUID()); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - patchChart(chartId, chartJson, chart, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.CHART, "id")); + assertResponse(() -> patchChart(chartId, chartJson, chart, adminAuthHeaders()), + BAD_REQUEST, readOnlyAttribute(Entity.CHART, "id")); // ID can't be deleted chart.setId(null); - exception = assertThrows(HttpResponseException.class, () -> - patchChart(chartId, chartJson, chart, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.CHART, "id")); + assertResponse(() -> patchChart(chartId, chartJson, chart, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.CHART, "id")); } @Test @@ -447,15 +425,13 @@ public class ChartResourceTest extends CatalogApplicationTest { Chart chart = createChart(create(test), adminAuthHeaders()); String chartJson = JsonUtils.pojoToJson(chart); chart.setName("newName"); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - patchChart(chartJson, chart, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.CHART, "name")); + assertResponse(() -> patchChart(chartJson, chart, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.CHART, "name")); // Name can't be removed chart.setName(null); - exception = assertThrows(HttpResponseException.class, () -> - patchChart(chartJson, chart, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.CHART, "name")); + assertResponse(() -> patchChart(chartJson, chart, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.CHART, "name")); } @Test @@ -466,15 +442,13 @@ public class ChartResourceTest extends CatalogApplicationTest { String chartJson = JsonUtils.pojoToJson(chart); chart.setService(LOOKER_REFERENCE); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - patchChart(chartJson, chart, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.CHART, "service")); + assertResponse(() -> patchChart(chartJson, chart, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.CHART, "service")); // Service relationship can't be removed chart.setService(null); - exception = assertThrows(HttpResponseException.class, () -> - patchChart(chartJson, chart, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.CHART, "service")); + assertResponse(() -> patchChart(chartJson, chart, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.CHART, "service")); } @Test @@ -513,22 +487,19 @@ public class ChartResourceTest extends CatalogApplicationTest { Chart chart = createAndCheckChart(create(test), adminAuthHeaders()); // Add non existent user as follower to the chart - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - addAndCheckFollower(chart, NON_EXISTENT_ENTITY, CREATED, 1, adminAuthHeaders())); - assertResponse(exception, NOT_FOUND, CatalogExceptionMessage.entityNotFound("User", NON_EXISTENT_ENTITY)); + assertResponse(() -> addAndCheckFollower(chart, NON_EXISTENT_ENTITY, CREATED, 1, adminAuthHeaders()), + NOT_FOUND, CatalogExceptionMessage.entityNotFound("User", NON_EXISTENT_ENTITY)); // Delete non existent user as follower to the chart - exception = assertThrows(HttpResponseException.class, () -> - deleteAndCheckFollower(chart, NON_EXISTENT_ENTITY, 1, adminAuthHeaders())); - assertResponse(exception, NOT_FOUND, CatalogExceptionMessage.entityNotFound("User", NON_EXISTENT_ENTITY)); + assertResponse(() -> deleteAndCheckFollower(chart, NON_EXISTENT_ENTITY, 1, adminAuthHeaders()), + NOT_FOUND, CatalogExceptionMessage.entityNotFound("User", NON_EXISTENT_ENTITY)); } @Test public void delete_nonExistentChart_404() { - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - deleteChart(NON_EXISTENT_ENTITY, adminAuthHeaders())); - assertResponse(exception, NOT_FOUND, entityNotFound(Entity.CHART, NON_EXISTENT_ENTITY)); + assertResponse(() -> deleteChart(NON_EXISTENT_ENTITY, adminAuthHeaders()), NOT_FOUND, + entityNotFound(Entity.CHART, NON_EXISTENT_ENTITY)); } public static Chart createAndCheckChart(CreateChart create, @@ -709,8 +680,7 @@ public class ChartResourceTest extends CatalogApplicationTest { TestUtils.delete(getResource("charts/" + id), authHeaders); // Ensure deleted chart does not exist - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> getChart(id, authHeaders)); - assertResponse(exception, NOT_FOUND, entityNotFound(Entity.CHART, id)); + assertResponse(() -> getChart(id, authHeaders), NOT_FOUND, entityNotFound(Entity.CHART, id)); } public static String getChartName(TestInfo test) { diff --git a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/dashboards/DashboardResourceTest.java b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/dashboards/DashboardResourceTest.java index f1fe214c448..3c2615967af 100644 --- a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/dashboards/DashboardResourceTest.java +++ b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/dashboards/DashboardResourceTest.java @@ -67,6 +67,7 @@ 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.ENTITY_ALREADY_EXISTS; import static org.openmetadata.catalog.exception.CatalogExceptionMessage.entityNotFound; import static org.openmetadata.catalog.exception.CatalogExceptionMessage.readOnlyAttribute; import static org.openmetadata.catalog.util.TestUtils.adminAuthHeaders; @@ -121,27 +122,23 @@ public class DashboardResourceTest extends CatalogApplicationTest { public void post_dashboardWithLongName_400_badRequest(TestInfo test) { // Create dashboard with mandatory name field empty CreateDashboard create = create(test).withName(TestUtils.LONG_ENTITY_NAME); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createDashboard(create, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[name size must be between 1 and 64]"); + assertResponse(() -> createDashboard(create, adminAuthHeaders()), BAD_REQUEST, + "[name size must be between 1 and 64]"); } @Test public void post_DashboardWithoutName_400_badRequest(TestInfo test) { // Create Dashboard with mandatory name field empty CreateDashboard create = create(test).withName(""); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createDashboard(create, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[name size must be between 1 and 64]"); + assertResponse(() -> createDashboard(create, adminAuthHeaders()), BAD_REQUEST, + "[name size must be between 1 and 64]"); } @Test public void post_DashboardAlreadyExists_409_conflict(TestInfo test) throws HttpResponseException { CreateDashboard create = create(test); createDashboard(create, adminAuthHeaders()); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createDashboard(create, adminAuthHeaders())); - assertResponse(exception, CONFLICT, CatalogExceptionMessage.ENTITY_ALREADY_EXISTS); + assertResponse(() -> createDashboard(create, adminAuthHeaders()), CONFLICT, ENTITY_ALREADY_EXISTS); } @Test @@ -172,17 +169,15 @@ public class DashboardResourceTest extends CatalogApplicationTest { @Test public void post_Dashboard_as_non_admin_401(TestInfo test) { CreateDashboard create = create(test); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createDashboard(create, authHeaders("test@open-metadata.org"))); - assertResponse(exception, FORBIDDEN, "Principal: CatalogPrincipal{name='test'} is not admin"); + assertResponse(() -> createDashboard(create, authHeaders("test@open-metadata.org")), FORBIDDEN, + "Principal: CatalogPrincipal{name='test'} is not admin"); } @Test public void post_DashboardWithoutRequiredService_4xx(TestInfo test) { CreateDashboard create = create(test).withService(null); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createDashboard(create, adminAuthHeaders())); - TestUtils.assertResponseContains(exception, BAD_REQUEST, "service must not be null"); + TestUtils.assertResponseContains(() -> createDashboard(create, adminAuthHeaders()), BAD_REQUEST, + "service must not be null"); } @Test @@ -209,9 +204,8 @@ public class DashboardResourceTest extends CatalogApplicationTest { public void post_DashboardWithNonExistentOwner_4xx(TestInfo test) { EntityReference owner = new EntityReference().withId(TestUtils.NON_EXISTENT_ENTITY).withType("user"); CreateDashboard create = create(test).withOwner(owner); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createDashboard(create, adminAuthHeaders())); - assertResponse(exception, NOT_FOUND, entityNotFound("User", TestUtils.NON_EXISTENT_ENTITY)); + assertResponse(() -> createDashboard(create, adminAuthHeaders()), NOT_FOUND, + entityNotFound("User", TestUtils.NON_EXISTENT_ENTITY)); } @Test @@ -233,25 +227,21 @@ public class DashboardResourceTest extends CatalogApplicationTest { @Test public void get_DashboardListWithInvalidLimitOffset_4xx() { // Limit must be >= 1 and <= 1000,000 - HttpResponseException exception = assertThrows(HttpResponseException.class, () - -> listDashboards(null, null, -1, null, null, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[query param limit must be greater than or equal to 1]"); + assertResponse(() -> listDashboards(null, null, -1, null, null, adminAuthHeaders()), + BAD_REQUEST, "[query param limit must be greater than or equal to 1]"); - exception = assertThrows(HttpResponseException.class, () - -> listDashboards(null, null, 0, null, null, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[query param limit must be greater than or equal to 1]"); + assertResponse(() -> listDashboards(null, null, 0, null, null, adminAuthHeaders()), + BAD_REQUEST, "[query param limit must be greater than or equal to 1]"); - exception = assertThrows(HttpResponseException.class, () - -> listDashboards(null, null, 1000001, null, null, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[query param limit must be less than or equal to 1000000]"); + assertResponse(() -> listDashboards(null, null, 1000001, null, null, adminAuthHeaders()), + BAD_REQUEST, "[query param limit must be less than or equal to 1000000]"); } @Test public void get_DashboardListWithInvalidPaginationCursors_4xx() { // Passing both before and after cursors is invalid - HttpResponseException exception = assertThrows(HttpResponseException.class, () - -> listDashboards(null, null, 1, "", "", adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "Only one of before or after query parameter allowed"); + assertResponse(() -> listDashboards(null, null, 1, "", "", adminAuthHeaders()), + BAD_REQUEST, "Only one of before or after query parameter allowed"); } @Test @@ -424,9 +414,7 @@ public class DashboardResourceTest extends CatalogApplicationTest { @Test public void get_nonExistentDashboard_404_notFound() { - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - getDashboard(TestUtils.NON_EXISTENT_ENTITY, adminAuthHeaders())); - assertResponse(exception, NOT_FOUND, + assertResponse(() -> getDashboard(TestUtils.NON_EXISTENT_ENTITY, adminAuthHeaders()), NOT_FOUND, entityNotFound(Entity.DASHBOARD, TestUtils.NON_EXISTENT_ENTITY)); } @@ -481,15 +469,13 @@ public class DashboardResourceTest extends CatalogApplicationTest { UUID dashboardId = dashboard.getId(); String dashboardJson = JsonUtils.pojoToJson(dashboard); dashboard.setId(UUID.randomUUID()); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - patchDashboard(dashboardId, dashboardJson, dashboard, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.DASHBOARD, "id")); + assertResponse(() -> patchDashboard(dashboardId, dashboardJson, dashboard, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.DASHBOARD, "id")); // ID can't be deleted dashboard.setId(null); - exception = assertThrows(HttpResponseException.class, () -> - patchDashboard(dashboardId, dashboardJson, dashboard, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.DASHBOARD, "id")); + assertResponse(() -> patchDashboard(dashboardId, dashboardJson, dashboard, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.DASHBOARD, "id")); } @Test @@ -498,15 +484,13 @@ public class DashboardResourceTest extends CatalogApplicationTest { Dashboard dashboard = createDashboard(create(test), adminAuthHeaders()); String dashboardJson = JsonUtils.pojoToJson(dashboard); dashboard.setName("newName"); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - patchDashboard(dashboardJson, dashboard, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.DASHBOARD, "name")); + assertResponse(() -> patchDashboard(dashboardJson, dashboard, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.DASHBOARD, "name")); // Name can't be removed dashboard.setName(null); - exception = assertThrows(HttpResponseException.class, () -> - patchDashboard(dashboardJson, dashboard, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.DASHBOARD, "name")); + assertResponse(() -> patchDashboard(dashboardJson, dashboard, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.DASHBOARD, "name")); } @Test @@ -517,15 +501,13 @@ public class DashboardResourceTest extends CatalogApplicationTest { String dashboardJson = JsonUtils.pojoToJson(dashboard); dashboard.setService(LOOKER_REFERENCE); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - patchDashboard(dashboardJson, dashboard, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.DASHBOARD, "service")); + assertResponse(() -> patchDashboard(dashboardJson, dashboard, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.DASHBOARD, "service")); // Service relationship can't be removed dashboard.setService(null); - exception = assertThrows(HttpResponseException.class, () -> - patchDashboard(dashboardJson, dashboard, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, readOnlyAttribute(Entity.DASHBOARD, "service")); + assertResponse(() -> patchDashboard(dashboardJson, dashboard, adminAuthHeaders()), BAD_REQUEST, + readOnlyAttribute(Entity.DASHBOARD, "service")); } // TODO listing tables test:1 @@ -544,9 +526,8 @@ public class DashboardResourceTest extends CatalogApplicationTest { @Test public void delete_nonExistentDashboard_404() { - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - deleteDashboard(TestUtils.NON_EXISTENT_ENTITY, adminAuthHeaders())); - assertResponse(exception, NOT_FOUND, entityNotFound(Entity.DASHBOARD, TestUtils.NON_EXISTENT_ENTITY)); + assertResponse(() -> deleteDashboard(TestUtils.NON_EXISTENT_ENTITY, adminAuthHeaders()), NOT_FOUND, + entityNotFound(Entity.DASHBOARD, TestUtils.NON_EXISTENT_ENTITY)); } public static Dashboard createAndCheckDashboard(CreateDashboard create, @@ -781,8 +762,7 @@ public class DashboardResourceTest extends CatalogApplicationTest { TestUtils.delete(getResource("dashboards/" + id), authHeaders); // Ensure deleted Dashboard does not exist - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> getDashboard(id, authHeaders)); - assertResponse(exception, NOT_FOUND, entityNotFound(Entity.DASHBOARD, id)); + assertResponse(() -> getDashboard(id, authHeaders), NOT_FOUND, entityNotFound(Entity.DASHBOARD, id)); } public static String getDashboardName(TestInfo test) { diff --git a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/databases/DatabaseResourceTest.java b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/databases/DatabaseResourceTest.java index a2038491429..c713f88b063 100644 --- a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/databases/DatabaseResourceTest.java +++ b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/databases/DatabaseResourceTest.java @@ -108,9 +108,8 @@ public class DatabaseResourceTest extends CatalogApplicationTest { public void post_databaseWithLongName_400_badRequest(TestInfo test) { // Create database with mandatory name field empty CreateDatabase create = create(test).withName(TestUtils.LONG_ENTITY_NAME); - HttpResponseException exception = assertThrows(HttpResponseException.class, () -> - createDatabase(create, adminAuthHeaders())); - assertResponse(exception, BAD_REQUEST, "[name size must be between 1 and 64]"); + assertResponse(() -> createDatabase(create, adminAuthHeaders()), BAD_REQUEST, + "[name size must be between 1 and 64]"); } @Test diff --git a/catalog-rest-service/src/test/java/org/openmetadata/catalog/util/TestUtils.java b/catalog-rest-service/src/test/java/org/openmetadata/catalog/util/TestUtils.java index 2c3eb8149b0..d34788877a7 100644 --- a/catalog-rest-service/src/test/java/org/openmetadata/catalog/util/TestUtils.java +++ b/catalog-rest-service/src/test/java/org/openmetadata/catalog/util/TestUtils.java @@ -16,13 +16,14 @@ package org.openmetadata.catalog.util; +import org.apache.http.client.HttpResponseException; +import org.eclipse.jetty.http.HttpStatus; +import org.junit.jupiter.api.function.Executable; 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; @@ -47,6 +48,7 @@ import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; public final class TestUtils { @@ -111,6 +113,20 @@ public final class TestUtils { return response.readEntity(clz); } + public static void assertResponse(Executable executable, Response.Status expectedStatus, String expectedReason) { + HttpResponseException exception = assertThrows(HttpResponseException.class, executable); + assertEquals(expectedStatus.getStatusCode(), exception.getStatusCode()); + assertEquals(expectedReason, exception.getReasonPhrase()); + } + + public static void assertResponseContains(Executable executable, Response.Status expectedStatus, + String expectedReason) { + HttpResponseException exception = assertThrows(HttpResponseException.class, executable); + assertEquals(expectedStatus.getStatusCode(), exception.getStatusCode()); + assertTrue(exception.getReasonPhrase().contains(expectedReason), + expectedReason + " not in actual " + exception.getReasonPhrase()); + } + public static void assertResponse(HttpResponseException exception, Status expectedCode, String expectedReason) { assertEquals(expectedCode.getStatusCode(), exception.getStatusCode()); assertEquals(expectedReason, exception.getReasonPhrase());