Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Junit test cases looking for full code coverage.

Avatar

Level 4

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));
}

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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

 

Avatar

Level 4

Hi @AMANATH_ULLAH  Thanks a lot for your time to reply . It is working as expected.