@djohn98390536
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");
}
}