Skip to content

Commit dd59eb8

Browse files
committed
feat(QTDI-1449): add CacheClear class
1 parent f9f4752 commit dd59eb8

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

component-server-parent/component-server-api/src/main/java/org/talend/sdk/component/server/api/CacheResource.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
*/
1616
package org.talend.sdk.component.server.api;
1717

18+
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
19+
1820
import javax.ws.rs.GET;
1921
import javax.ws.rs.Path;
20-
import javax.ws.rs.core.Response;
2122

2223
import org.eclipse.microprofile.openapi.annotations.Operation;
24+
import org.eclipse.microprofile.openapi.annotations.media.Content;
2325
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
2426
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
27+
import org.talend.sdk.component.server.front.model.CacheClear;
2528

2629
@Path("cache")
2730
@Tag(name = "Cache", description = "Endpoints related to caches management.")
@@ -30,6 +33,7 @@ public interface CacheResource {
3033
@GET
3134
@Path("clear")
3235
@Operation(operationId = "clearCaches", description = "Clear all caches.")
33-
@APIResponse(responseCode = "200", description = "Cleared caches.")
34-
Response clearCaches();
36+
@APIResponse(responseCode = "200", description = "Cleared caches.",
37+
content = @Content(mediaType = APPLICATION_JSON))
38+
CacheClear clearCaches();
3539
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (C) 2006-2025 Talend Inc. - www.talend.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.talend.sdk.component.server.front.model;
17+
18+
import lombok.AllArgsConstructor;
19+
import lombok.Data;
20+
import lombok.NoArgsConstructor;
21+
22+
@Data
23+
@AllArgsConstructor
24+
@NoArgsConstructor
25+
public class CacheClear {
26+
27+
private long clearedCacheCount;
28+
}

component-server-parent/component-server/src/main/java/org/talend/sdk/component/server/service/jcache/FrontCacheResolver.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
package org.talend.sdk.component.server.service.jcache;
1717

1818
import static java.util.Optional.ofNullable;
19-
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
2019

2120
import java.lang.annotation.Annotation;
22-
import java.util.Collections;
23-
import java.util.Map;
2421
import java.util.concurrent.TimeUnit;
2522
import java.util.stream.StreamSupport;
2623

@@ -35,7 +32,6 @@
3532
import javax.cache.configuration.Configuration;
3633
import javax.enterprise.context.ApplicationScoped;
3734
import javax.inject.Inject;
38-
import javax.ws.rs.core.Response;
3935

4036
import org.apache.geronimo.jcache.simple.cdi.CacheResolverImpl;
4137
import org.eclipse.microprofile.config.inject.ConfigProperty;
@@ -44,6 +40,7 @@
4440
import org.talend.sdk.component.server.front.ComponentResourceImpl;
4541
import org.talend.sdk.component.server.front.ConfigurationTypeResourceImpl;
4642
import org.talend.sdk.component.server.front.EnvironmentResourceImpl;
43+
import org.talend.sdk.component.server.front.model.CacheClear;
4744
import org.talend.sdk.component.server.front.model.Environment;
4845
import org.talend.sdk.component.server.service.ComponentManagerService;
4946
import org.talend.sdk.components.vault.jcache.CacheConfigurationFactory;
@@ -147,14 +144,10 @@ public void cleanupCaches() {
147144
}
148145

149146
@Override
150-
public Response clearCaches() {
147+
public CacheClear clearCaches() {
151148
final long clearedCacheCount = countActiveCaches();
152-
Map<String, Long> stringStringMap = Collections.singletonMap("clearedCacheCount", clearedCacheCount);
153149
service.redeployPlugins();
154-
return Response
155-
.ok(stringStringMap)
156-
.type(APPLICATION_JSON_TYPE)
157-
.build();
150+
return new CacheClear(clearedCacheCount);
158151
}
159152

160153
/**

component-server-parent/component-server/src/test/java/org/talend/sdk/component/server/service/jcache/FrontCacheResolverTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@
1818
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020

21-
import java.util.Map;
22-
2321
import javax.inject.Inject;
2422
import javax.ws.rs.client.WebTarget;
2523
import javax.ws.rs.core.Response;
2624

2725
import com.fasterxml.jackson.core.JsonProcessingException;
28-
import com.fasterxml.jackson.databind.ObjectMapper;
2926

3027
import org.apache.meecrowave.junit5.MonoMeecrowaveConfig;
3128
import org.junit.jupiter.api.BeforeEach;
3229
import org.junit.jupiter.api.Test;
30+
import org.talend.sdk.component.server.front.model.CacheClear;
3331
import org.talend.sdk.component.server.service.ComponentManagerService;
3432
import org.talend.sdk.component.server.test.ComponentClient;
3533

@@ -77,11 +75,9 @@ void clearCaches() throws JsonProcessingException {
7775
.request(APPLICATION_JSON_TYPE)
7876
.get();
7977
assertEquals(200, resp.getStatus());
78+
final CacheClear cleared = resp.readEntity(CacheClear.class);
8079

81-
ObjectMapper objectMapper = new ObjectMapper();
82-
Map<String, Object> responseMap = objectMapper.readValue(resp.readEntity(String.class), Map.class);
83-
84-
assertEquals(2, (Integer) responseMap.get("clearedCacheCount"));
80+
assertEquals(2, cleared.getClearedCacheCount());
8581
assertEquals(0, cacheResolver.countActiveCaches());
8682
assertEquals(connectors, service.getConnectors().getPluginsList().size());
8783
}

0 commit comments

Comments
 (0)