Here is the below code looking for full code coverage.
ValueMap vm = assetResource.getValueMap();
String version = vm.get("jcr:lastModifiedVersion", String.class);
boolean isNewAsset = vm.containsKey("newAsset");
if (!isNewAsset) {
Resource rr= resourceResolver.getResource(path);
ModifiableValueMap mvm = rr.adaptTo(ModifiableValueMap.class);
mvm.put(newversion,version + 1));
} else if (isNewAsset) {
Resource rr= resourceResolver.getResource(path);
ModifiableValueMap mvm= rr.adaptTo(ModifiableValueMap.class);
mvm.put(newversion,version + 2));
}
Solved! Go to Solution.
Views
Replies
Total Likes
Below is the junit code
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class AssetTest {
@Mock
private Resource assetResource;
@Mock
private ResourceResolver resourceResolver;
@Mock
private ValueMap valueMap;
@Mock
private Resource resource;
@Mock
private ModifiableValueMap modifiableValueMap;
@InjectMocks
private AssetService assetService; // Assuming the code belongs to a service class named AssetService
private final String path = "/content/dam/sample";
private final String versionKey = "jcr:lastModifiedVersion";
private final String newVersionKey = "newVersion";
private final String version = "1.0";
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(assetResource.getValueMap()).thenReturn(valueMap);
when(resourceResolver.getResource(path)).thenReturn(resource);
when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(modifiableValueMap);
}
@Test
public void testExistingAsset() {
// Setup
when(valueMap.get(versionKey, String.class)).thenReturn(version);
when(valueMap.containsKey("newAsset")).thenReturn(false);
// Execute
assetService.updateAsset(assetResource, resourceResolver, path);
// Verify
verify(modifiableValueMap).put(newVersionKey, "1.01");
}
@Test
public void testNewAsset() {
// Setup
when(valueMap.get(versionKey, String.class)).thenReturn(version);
when(valueMap.containsKey("newAsset")).thenReturn(true);
// Execute
assetService.updateAsset(assetResource, resourceResolver, path);
// Verify
verify(modifiableValueMap).put(newVersionKey, "1.02");
}
}
Below is the junit code
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class AssetTest {
@Mock
private Resource assetResource;
@Mock
private ResourceResolver resourceResolver;
@Mock
private ValueMap valueMap;
@Mock
private Resource resource;
@Mock
private ModifiableValueMap modifiableValueMap;
@InjectMocks
private AssetService assetService; // Assuming the code belongs to a service class named AssetService
private final String path = "/content/dam/sample";
private final String versionKey = "jcr:lastModifiedVersion";
private final String newVersionKey = "newVersion";
private final String version = "1.0";
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(assetResource.getValueMap()).thenReturn(valueMap);
when(resourceResolver.getResource(path)).thenReturn(resource);
when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(modifiableValueMap);
}
@Test
public void testExistingAsset() {
// Setup
when(valueMap.get(versionKey, String.class)).thenReturn(version);
when(valueMap.containsKey("newAsset")).thenReturn(false);
// Execute
assetService.updateAsset(assetResource, resourceResolver, path);
// Verify
verify(modifiableValueMap).put(newVersionKey, "1.01");
}
@Test
public void testNewAsset() {
// Setup
when(valueMap.get(versionKey, String.class)).thenReturn(version);
when(valueMap.containsKey("newAsset")).thenReturn(true);
// Execute
assetService.updateAsset(assetResource, resourceResolver, path);
// Verify
verify(modifiableValueMap).put(newVersionKey, "1.02");
}
}
Hi @AMANATH_ULLAH Thanks a lot for your time to reply . It is working as expected.
Views
Likes
Replies