Skip to content

Commit

Permalink
feat: add search by url for external api (#49)
Browse files Browse the repository at this point in the history
Co-authored-by: milan.horvath <[email protected]>
  • Loading branch information
milanhorvath and milan.horvath authored Apr 4, 2024
1 parent 8c206b3 commit bf11ebc
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ public Workspace findByName(String workspaceName) {
}
}

public Workspace findByUrl(String url) {

try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Workspace.class);
var root = cq.from(Workspace.class);
cq.where(cb.like(cb.literal(url), cb.concat(root.get(BASE_URL), "%")));
return this.getEntityManager().createQuery(cq).getSingleResult();
} catch (NoResultException nre) {
return null;
} catch (Exception ex) {
throw handleConstraint(ex, ErrorKeys.ERROR_FIND_WORKSPACE_BY_URL);
}
}

/**
* This method fetches the whole workspace with all his lazy load objects
*/
Expand Down Expand Up @@ -107,6 +122,7 @@ public PageResult<Workspace> findBySearchCriteria(WorkspaceSearchCriteria criter
public enum ErrorKeys {

ERROR_FIND_WORKSPACE_BY_NAME,
ERROR_FIND_WORKSPACE_BY_URL,
FIND_ENTITY_BY_ID_FAILED,
ERROR_FIND_BY_CRITERIA,
ERROR_LOAD_WORKSPACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tkit.onecx.workspace.rs.external.v1.WorkspaceExternalV1Api;
import gen.org.tkit.onecx.workspace.rs.external.v1.model.GetWorkspaceByUrlRequestDTOV1;
import gen.org.tkit.onecx.workspace.rs.external.v1.model.WorkspaceSearchCriteriaDTOV1;

@LogService
Expand Down Expand Up @@ -42,6 +43,16 @@ public Response getWorkspaceByName(String name) {
return Response.ok(mapper.map(item)).build();
}

@Override
public Response getWorkspaceByUrl(GetWorkspaceByUrlRequestDTOV1 getWorkspaceByUrlRequestDTOV1) {
var item = workspaceDAO.findByUrl(getWorkspaceByUrlRequestDTOV1.getUrl());

if (item == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.ok(mapper.map(item)).build();
}

@Override
@Transactional
public Response searchWorkspaces(WorkspaceSearchCriteriaDTOV1 workspaceSearchCriteriaDTOV1) {
Expand Down
27 changes: 27 additions & 0 deletions src/main/openapi/onecx-workspace-v1-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ servers:
tags:
- name: workspaceExternal
paths:
/v1/workspaces/byUrl:
post:
tags:
- workspaceExternal
description: search workspace by url
operationId: getWorkspaceByUrl
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/GetWorkspaceByUrlRequest'
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Workspace'
"404":
description: Not found
/v1/workspaces/search:
post:
tags:
Expand Down Expand Up @@ -192,6 +212,13 @@ components:
type: string
basePath:
type: string
GetWorkspaceByUrlRequest:
type: object
required:
- url
properties:
url:
type: string
parameters:
name:
in: path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void methodExceptionTests() {
WorkspaceDAO.ErrorKeys.FIND_ENTITY_BY_ID_FAILED);
methodExceptionTests(() -> dao.findByName(null),
WorkspaceDAO.ErrorKeys.ERROR_FIND_WORKSPACE_BY_NAME);
methodExceptionTests(() -> dao.findByUrl(null),
WorkspaceDAO.ErrorKeys.ERROR_FIND_WORKSPACE_BY_URL);
}

void methodExceptionTests(Executable fn, Enum<?> key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ private static Stream<Arguments> criteriaAndResults() {
arguments(new WorkspaceSearchCriteriaDTOV1().themeName("11-111"), 3, "org1"),
arguments(new WorkspaceSearchCriteriaDTOV1().themeName("22-222"), 0, "org1"), // different tenant so will not find it
arguments(new WorkspaceSearchCriteriaDTOV1().themeName("does-not-exists"), 0, "org1"),
arguments(new WorkspaceSearchCriteriaDTOV1(), 3, "org1"));
arguments(new WorkspaceSearchCriteriaDTOV1(), 6, "org1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void getALlWorkspacesByProductNameTest() {
.extract().as(WorkspacePageResultDTOV1.class);

assertThat(dto).isNotNull();
assertThat(dto.getStream()).isNotNull().isNotEmpty().hasSize(3);
assertThat(dto.getStream()).isNotNull().isNotEmpty().hasSize(6);
}

@Test
Expand Down Expand Up @@ -115,7 +115,7 @@ private static Stream<Arguments> criteriaAndResults() {
arguments(criteria1, 3),
arguments(criteria2, 0), // different tenant so will not find it
arguments(criteria3, 0),
arguments(emptyCriteria, 3));
arguments(emptyCriteria, 6));
}

@Test
Expand Down Expand Up @@ -144,4 +144,47 @@ void getProductsForWorkspaceNameTest() {
assertThat(dto.getProducts().get(0).getProductName()).isNotEmpty();
assertThat(dto.getProducts().get(1).getProductName()).isNotEmpty();
}

@Test
void getWorkspaceByUrlTest() {

GetWorkspaceByUrlRequestDTOV1 requestDTOV1 = new GetWorkspaceByUrlRequestDTOV1();
requestDTOV1.setUrl("does-not-exist-url");
// not existing workspace
given()
.when()
.contentType(APPLICATION_JSON)
.body(requestDTOV1)
.post("/byUrl")
.then()
.statusCode(NOT_FOUND.getStatusCode());

// existing workspace
requestDTOV1.setUrl("/company2/admin/my/url");
var dto = given()
.when()
.contentType(APPLICATION_JSON)
.body(requestDTOV1)
.post("/byUrl")
.then()
.statusCode(OK.getStatusCode())
.extract().as(WorkspaceDTOV1.class);

assertThat(dto).isNotNull();
assertThat(dto.getCompanyName()).isNotNull().isNotEmpty().isEqualTo("Company2");

// more workspaces for the url
requestDTOV1.setUrl("/de/test/some/strange");
dto = given()
.when()
.contentType(APPLICATION_JSON)
.body(requestDTOV1)
.post("/byUrl")
.then()
.statusCode(OK.getStatusCode())
.extract().as(WorkspaceDTOV1.class);

assertThat(dto).isNotNull();
assertThat(dto.getCompanyName()).isNotNull().isNotEmpty().isEqualTo("Company44");
}
}
11 changes: 11 additions & 0 deletions src/test/resources/data/testdata-external.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<WORKSPACE guid="11-111" optlock="0" company_name="Company1" theme="11-111" description="Company1 test" home_page="main_page" name="test01" phone_number="606450342" rss_feed_url="url.com" city="Poznan" country="Poland" base_url="/company1" postalcode="56-789" street="Kolorowa" streetno="6" tenant_id="tenant-100"/>
<WORKSPACE guid="11-222" optlock="0" company_name="Company2" theme="11-111" description="Company2 Test" home_page="main_page" name="test02" phone_number="3856382" rss_feed_url="url.com" city="Berlin" country="Germany" base_url="/company2" postalcode="12-345" street="Unter den Linden" streetno="23" tenant_id="tenant-100"/>
<WORKSPACE guid="22-111" optlock="0" company_name="Company3" theme="11-111" description="Company3 Test" home_page="main_page" name="test03" phone_number="123456789" rss_feed_url="url.com" city="Stuttgart" country="Germany" base_url="/company3" postalcode="45-341" street="Wilhelmstrasse" streetno="15" tenant_id="tenant-100"/>
<WORKSPACE guid="22-222" optlock="0" company_name="Company22" theme="cap" description="Company22 Test" home_page="main_page" name="test22" phone_number="123456789" rss_feed_url="url.com" city="Stuttgart" country="Germany" base_url="/de/cap/admin" postalcode="45-341" street="Wilhelmstrasse" streetno="15" tenant_id="tenant-100"/>
<WORKSPACE guid="22-333" optlock="0" company_name="Company33" theme="tkom" description="Company33 Test" home_page="main_page" name="test33" phone_number="123456789" rss_feed_url="url.com" city="Stuttgart" country="Germany" base_url="/de/tkom/admin" postalcode="45-341" street="Wilhelmstrasse" streetno="15" tenant_id="tenant-100"/>
<WORKSPACE guid="22-444" optlock="0" company_name="Company44" theme="germany" description="Company44 Test" home_page="main_page" name="test44" phone_number="123456789" rss_feed_url="url.com" city="Stuttgart" country="Germany" base_url="/de" postalcode="45-341" street="Wilhelmstrasse" streetno="15" tenant_id="tenant-100"/>
<WORKSPACE guid="33-111" optlock="0" company_name="Company4" theme="22-222" description="Company4 test" home_page="main_page" name="test04" phone_number="606450342" rss_feed_url="url.com" city="Poznan" country="Poland" base_url="/company4" postalcode="56-789" street="Kolorowa" streetno="6" tenant_id="tenant-200"/>
<WORKSPACE guid="33-222" optlock="0" company_name="Company5" theme="22-222" description="Company5 Test" home_page="main_page" name="test05" phone_number="3856382" rss_feed_url="url.com" city="Berlin" country="Germany" base_url="/company5" postalcode="12-345" street="Unter den Linden" streetno="23" tenant_id="tenant-200"/>

Expand All @@ -19,4 +22,12 @@
<PRODUCT guid="5678" optlock="0" workspace_guid="11-222" product_name="onecx-core" base_url="/test" tenant_id="tenant-100"/>
<PRODUCT guid="1111" optlock="0" workspace_guid="11-111" product_name="onecx-apm" base_url="/apm" tenant_id="tenant-100"/>

<!-- MICROFRONTEND -->
<MICROFRONTEND guid="1234" mfe_id="menu" base_path="/menu" product_guid="1234"/>
<MICROFRONTEND guid="5678" mfe_id="theme" base_path="/theme" product_guid="1234"/>

<ROLE guid="r11" workspace_guid="11-111" optlock="0" name="n1" description="d1" tenant_id="tenant-100"/>
<ROLE guid="r12" workspace_guid="11-111" optlock="0" name="n2" description="d1" tenant_id="tenant-100"/>

<ASSIGNMENT guid="a11" optlock="0" menu_item_id="33-1" role_id="r11" tenant_id="tenant-100"/>
</dataset>

0 comments on commit bf11ebc

Please sign in to comment.