-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#3388): Remove asset links from adapters when deleted
- Loading branch information
Showing
19 changed files
with
506 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...nagement/src/main/java/org/apache/streampipes/assetmodel/management/AssetModelHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.streampipes.assetmodel.management; | ||
|
||
import org.apache.streampipes.model.assets.SpAsset; | ||
import org.apache.streampipes.model.assets.SpAssetModel; | ||
import org.apache.streampipes.storage.management.StorageDispatcher; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class AssetModelHelper { | ||
private static final Logger LOG = LoggerFactory.getLogger(AssetModelHelper.class); | ||
|
||
private final AssetModelManagement assetModelManagement; | ||
|
||
public AssetModelHelper(AssetModelManagement assetModelManagement) { | ||
this.assetModelManagement = assetModelManagement; | ||
} | ||
|
||
|
||
public AssetModelHelper() { | ||
var genericStorage = StorageDispatcher.INSTANCE.getNoSqlStore() | ||
.getGenericStorage(); | ||
assetModelManagement = new AssetModelManagement(genericStorage); | ||
} | ||
|
||
/** | ||
* Removes the asset link with the given resource ID from all asset models. | ||
* | ||
* @param resourceId The ID of the resource to be removed from the asset links. | ||
* @throws IOException If an I/O error occurs while fetching or updating asset models. | ||
*/ | ||
public void removeAssetLinkFromAllAssets(String resourceId) throws IOException { | ||
var allAssetModels = getAllAssetModelsFromStorage(); | ||
|
||
removeAssetLinksFromAssetModels(resourceId, allAssetModels); | ||
} | ||
|
||
private void removeAssetLinksFromAssetModels(String resourceId, List<SpAssetModel> allAssetModels) | ||
throws IOException { | ||
for (SpAssetModel assetModel : allAssetModels) { | ||
removeAssetLinksFromAssetModelRecursively(assetModel, resourceId); | ||
updateAssetModel(assetModel); | ||
} | ||
} | ||
|
||
private void updateAssetModel(SpAssetModel assetModel) throws IOException { | ||
try { | ||
assetModelManagement.update(assetModel.getId(), assetModel); | ||
} catch (IOException e) { | ||
LOG.error("Could not fetch all asset models from storage", e); | ||
throw new IOException("Could not fetch all asset models from storage", e); | ||
} | ||
} | ||
|
||
private List<SpAssetModel> getAllAssetModelsFromStorage() throws IOException { | ||
try { | ||
return assetModelManagement.findAll(); | ||
} catch (IOException e) { | ||
LOG.error("Could not fetch all asset models from storage", e); | ||
throw new IOException("Could not fetch all asset models from storage", e); | ||
} | ||
} | ||
|
||
/** | ||
* This method removes the asset link from the asset model and recursively from all sub-assets. | ||
*/ | ||
private void removeAssetLinksFromAssetModelRecursively(SpAssetModel assetModel, String resourceId) { | ||
removeAssetLinks(assetModel, resourceId); | ||
|
||
assetModel.getAssets() | ||
.forEach(asset -> removeAssetLinksFromAsset(asset, resourceId)); | ||
} | ||
|
||
/** | ||
* Removes the resourceId from the asset links and recursively from all sub-assets. | ||
*/ | ||
private void removeAssetLinksFromAsset(SpAsset asset, String resourceId) { | ||
removeAssetLinks(asset, resourceId); | ||
|
||
if (asset.getAssets() != null) { | ||
asset.getAssets() | ||
.forEach(subAsset -> removeAssetLinks(subAsset, resourceId)); | ||
} | ||
} | ||
|
||
/** | ||
* Takes the asset as an input and removes the asset link with the given resource ID. | ||
*/ | ||
private void removeAssetLinks(SpAsset asset, String resourceId) { | ||
var assetLinks = asset.getAssetLinks(); | ||
if (assetLinks != null) { | ||
assetLinks.removeIf(link -> resourceId.equals(link.getResourceId())); | ||
} | ||
} | ||
} |
File renamed without changes.
115 changes: 115 additions & 0 deletions
115
...ment/src/test/java/org/apache/streampipes/assetmodel/management/AssetModelHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.streampipes.assetmodel.management; | ||
|
||
import org.apache.streampipes.model.assets.AssetLink; | ||
import org.apache.streampipes.model.assets.SpAsset; | ||
import org.apache.streampipes.model.assets.SpAssetModel; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class AssetModelHelperTest { | ||
|
||
private static final String RESOURCE_ID_TO_BE_REMOVED = "resourceId"; | ||
|
||
private AssetModelManagement assetModelManagement; | ||
private AssetModelHelper assetModelHelper; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
assetModelManagement = Mockito.mock(AssetModelManagement.class); | ||
assetModelHelper = new AssetModelHelper(assetModelManagement); | ||
} | ||
|
||
@Test | ||
void removeAssetLinkFromAllAssets_FromSpAssetModel() throws IOException { | ||
// Provide a sample asset model that contains the asset link to be removed and mock the asset model management | ||
// findAll | ||
var sampleAssetModel = getSampleAssetModel(); | ||
addAssetLink(sampleAssetModel, RESOURCE_ID_TO_BE_REMOVED); | ||
when(assetModelManagement.findAll()).thenReturn(List.of(sampleAssetModel)); | ||
|
||
assetModelHelper.removeAssetLinkFromAllAssets(RESOURCE_ID_TO_BE_REMOVED); | ||
|
||
// Verify that asset model was updated and does not contain the asset link anymore | ||
verify(assetModelManagement, times(1)).update(sampleAssetModel.getId(), sampleAssetModel); | ||
assertTrue(sampleAssetModel.getAssetLinks().isEmpty()); | ||
} | ||
|
||
@Test | ||
void removeAssetLinkFromAllAssets_FromSpAsset() throws IOException { | ||
// Provide a sample asset model with one asset that contains the asset link to be removed and mock the asset model | ||
// management findAll | ||
var sampleAssetModel = getSampleAssetModel(); | ||
var asset = new SpAsset(); | ||
addAssetLink(asset, RESOURCE_ID_TO_BE_REMOVED); | ||
sampleAssetModel.setAssets(List.of(asset)); | ||
when(assetModelManagement.findAll()).thenReturn(List.of(sampleAssetModel)); | ||
|
||
assetModelHelper.removeAssetLinkFromAllAssets(RESOURCE_ID_TO_BE_REMOVED); | ||
|
||
// Verify that asset was updated and does not contain the asset link anymore | ||
verify(assetModelManagement, times(1)).update(sampleAssetModel.getId(), sampleAssetModel); | ||
assertTrue(sampleAssetModel.getAssets().get(0).getAssetLinks().isEmpty()); | ||
} | ||
|
||
@Test | ||
void removeAssetLinkFromAllAssets_IOExceptionOnReadingAssetModels() throws IOException { | ||
when(assetModelManagement.findAll()).thenThrow(new IOException()); | ||
|
||
assertThrows(IOException.class, () -> assetModelHelper.removeAssetLinkFromAllAssets(RESOURCE_ID_TO_BE_REMOVED)); | ||
} | ||
|
||
@Test | ||
void removeAssetLinkFromAllAssets_IOExceptionWhenUpdatingModel() throws IOException { | ||
var sampleAssetModel = getSampleAssetModel(); | ||
addAssetLink(sampleAssetModel, RESOURCE_ID_TO_BE_REMOVED); | ||
when(assetModelManagement.findAll()).thenReturn(List.of(sampleAssetModel)); | ||
|
||
when(assetModelManagement.update(sampleAssetModel.getId(), sampleAssetModel)).thenThrow(new IOException()); | ||
|
||
assertThrows(IOException.class, () -> assetModelHelper.removeAssetLinkFromAllAssets(RESOURCE_ID_TO_BE_REMOVED)); | ||
} | ||
|
||
|
||
private SpAssetModel getSampleAssetModel() { | ||
var sampleAssetModel = new SpAssetModel(); | ||
sampleAssetModel.setId("1"); | ||
|
||
return sampleAssetModel; | ||
} | ||
|
||
private void addAssetLink(SpAsset asset, String assetLinkResourceId) { | ||
var assetLink = new AssetLink(); | ||
assetLink.setResourceId(assetLinkResourceId); | ||
asset.setAssetLinks(new ArrayList<>(List.of(assetLink))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.