Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
import org.entando.entando.ent.util.EntLogging.EntLogFactory;
import org.entando.entando.web.common.model.PagedMetadata;
import org.entando.entando.web.common.model.RestListRequest;
import org.entando.entando.plugins.jacms.web.resource.ResourcesController;
import org.springframework.beans.factory.annotation.Autowired;
import org.entando.entando.web.common.model.RestNamedId;

public class ResourceService implements IResourceService,
GroupServiceUtilizer<ResourceDto>, CategoryServiceUtilizer<ResourceDto>, IComponentUsageService {

private final EntLogger logger = EntLogFactory.getSanitizedLogger(this.getClass());

public static final String TYPE_ASSET = ComponentUsageEntity.TYPE_ASSET;

private IResourceManager resourceManager;
private IDtoBuilder<ResourceInterface, ResourceDto> dtoBuilder;
private List<? extends ResourceServiceUtilizer> resourceServiceUtilizers = new ArrayList<>();
Expand Down Expand Up @@ -131,24 +131,25 @@ private List<ResourceDto> buildDtoList(List<String> idList) {

@Override
public Optional<IComponentDto> getComponentDto(String code) throws EntException {
return Optional.ofNullable(this.resourceManager.loadResource(code))
return Optional.ofNullable(this.resourceManager.loadResource(code, this.extractCorrelationCodeByCode(code).orElse(null)))
.map(c -> this.getDtoBuilder().convert(c));
}

@Override
public boolean exists(String code) throws EntException {
return resourceManager.exists(null, code);
return resourceManager.exists(code, this.extractCorrelationCodeByCode(code).orElse(null));
}

@Override
public String getObjectType() {
return TYPE_ASSET;
return ComponentUsageEntity.TYPE_ASSET;
}

@Override
public void deleteComponent(String componentCode) {
try {
ResourceInterface resource = this.getResourceManager().loadResource(componentCode);
String cc = this.extractCorrelationCodeByCode(componentCode).orElse(null);
ResourceInterface resource = this.getResourceManager().loadResource(componentCode, cc);
if (null != resource) {
this.resourceManager.deleteResource(resource);
}else{
Expand All @@ -170,8 +171,9 @@ public Integer getComponentUsage(String componentCode) {
@Override
public PagedMetadata<ComponentUsageEntity> getComponentUsageDetails(String componentCode, RestListRequest restListRequest) {
List<ComponentUsageEntity> components = new ArrayList<>();
String resourceCode = this.extractCorrelationCodeByCode(componentCode).orElse(componentCode);
for (var utilizer : this.resourceServiceUtilizers) {
List<IComponentDto> objects = utilizer.getResourceUtilizer(componentCode);
List<IComponentDto> objects = utilizer.getResourceUtilizer(resourceCode);
List<ComponentUsageEntity> utilizerForService = objects.stream()
.map(o -> o.buildUsageEntity()).collect(Collectors.toList());
components.addAll(utilizerForService);
Expand All @@ -181,5 +183,9 @@ public PagedMetadata<ComponentUsageEntity> getComponentUsageDetails(String compo
usageEntries.setBody(sublist);
return usageEntries;
}

private Optional<String> extractCorrelationCodeByCode(String code) {
return new RestNamedId(code).getValidValue(ResourcesController.ID_NAME_OF_CORRELATION_CODE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,41 @@ void shouldFindComponentDto() throws Exception {
ResourceInterface resource = Mockito.mock(ImageResource.class);
Mockito.when(resource.getType()).thenReturn("Image");
Mockito.when(resource.getDefaultInstance()).thenReturn(Mockito.mock(ResourceInstance.class));
Mockito.when(this.resourceManager.loadResource("id")).thenReturn(resource);
Mockito.when(this.resourceManager.loadResource("id", null)).thenReturn(resource);
Optional<IComponentDto> dto = this.resourceService.getComponentDto("id");
assertThat(dto).isNotEmpty();
Assertions.assertTrue(dto.get() instanceof ResourceDto);
}

@Test
void shouldFindComponentDtoByCorrelationCode() throws Exception {
ResourceInterface resource = Mockito.mock(ImageResource.class);
Mockito.when(resource.getType()).thenReturn("Image");
Mockito.when(resource.getDefaultInstance()).thenReturn(Mockito.mock(ResourceInstance.class));
Mockito.when(this.resourceManager.loadResource(Mockito.anyString(), Mockito.eq("correlationCode"))).thenReturn(resource);
Optional<IComponentDto> dto = this.resourceService.getComponentDto("cc=correlationCode");
assertThat(dto).isNotEmpty();
Assertions.assertTrue(dto.get() instanceof ResourceDto);
}

@Test
void shouldDeleteNotExistingComponent() throws Exception {
Mockito.when(this.resourceManager.loadResource("test")).thenReturn(null);
Mockito.when(this.resourceManager.loadResource("test", null)).thenReturn(null);
this.resourceService.deleteComponent("test");
Mockito.verify(resourceManager, Mockito.times(0)).deleteResource(Mockito.any());
}

@Test
void shouldDeleteExistingComponent() throws Exception {
Mockito.when(this.resourceManager.loadResource("test")).thenReturn(Mockito.mock(ResourceInterface.class));
Mockito.when(this.resourceManager.loadResource("test", null)).thenReturn(Mockito.mock(ResourceInterface.class));
this.resourceService.deleteComponent("test");
Mockito.verify(resourceManager, Mockito.times(1)).deleteResource(Mockito.any());
}

@Test
void shouldFailDeletingComponent() throws Exception {
Assertions.assertThrows(RestServerError.class, () -> {
when(this.resourceManager.loadResource("test")).thenThrow(EntException.class);
when(this.resourceManager.loadResource("test", null)).thenThrow(EntException.class);
this.resourceService.deleteComponent("test");
});
Mockito.verify(resourceManager, Mockito.times(0)).deleteResource(Mockito.any());
Expand Down