Skip to content

Commit

Permalink
rest-api: cleanup in BackendSnapshotsResource
Browse files Browse the repository at this point in the history
Drop hacks that compensated the lack of GetSnapshotBySnapshotId

Change-Id: I721eb1f9ef936135cb01f6f012722020dba465ff
Signed-off-by: Arik Hadas <ahadas@redhat.com>
  • Loading branch information
ahadas committed Nov 9, 2021
1 parent ab54240 commit cee41a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected Response doAdd(Snapshot snapshot, boolean block) {
}
return performCreate(ActionType.CreateSnapshotForVm,
snapshotParams,
new SnapshotIdResolver(),
new QueryIdResolver<Guid>(QueryType.GetSnapshotBySnapshotId, IdQueryParameters.class),
block);
}

Expand Down Expand Up @@ -152,14 +152,10 @@ protected org.ovirt.engine.core.common.businessentities.VM getVmPreview(Snapshot
}

protected org.ovirt.engine.core.common.businessentities.Snapshot getSnapshotById(Guid id) {
//TODO: move to 'GetSnapshotBySnapshotId' once Backend supplies it.
for (org.ovirt.engine.core.common.businessentities.Snapshot snapshot : getBackendCollection(QueryType.GetAllVmSnapshotsByVmId,
new IdQueryParameters(parentId))) {
if (snapshot.getId().equals(id)) {
return snapshot;
}
}
return null;
return getEntity(org.ovirt.engine.core.common.businessentities.Snapshot.class,
QueryType.GetSnapshotBySnapshotId,
new IdQueryParameters(id),
String.format("GetSnapshotBySnapshotId: snapshot id=%s", id));
}

@Override
Expand All @@ -169,17 +165,6 @@ protected Snapshot addParents(Snapshot snapshot) {
return snapshot;
}

protected class SnapshotIdResolver extends EntityIdResolver<Guid> {

SnapshotIdResolver() {}

@Override
public org.ovirt.engine.core.common.businessentities.Snapshot lookupEntity(
Guid id) throws BackendFailureException {
return getSnapshotById(id);
}
}

@Override
protected Snapshot doPopulate(Snapshot model, org.ovirt.engine.core.common.businessentities.Snapshot entity) {
return populateSnapshotConfiguration(model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class BackendSnapshotResourceTest

private static final Guid VM_ID = GUIDS[0];
private static final Guid SNAPSHOT_ID = GUIDS[1];
private static final String SNAPSHOT_DESCRIPTION = DESCRIPTIONS[1];

public BackendSnapshotResourceTest() {
super(new BackendSnapshotResource(SNAPSHOT_ID.toString(), VM_ID, null));
Expand All @@ -53,7 +54,7 @@ public void initParentResource() {
@Test
public void testGet() {
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(asList(getEntity(1)));
setUpGetEntityExpectations(getEntity(1));
verifyModel(resource.get(), 1);
}

Expand All @@ -62,17 +63,13 @@ public void testGetWithPopulate() {
List<String> populates = new ArrayList<>();
populates.add("true");
String ovfData = "data";
org.ovirt.engine.core.common.businessentities.Snapshot resultSnapshot = new org.ovirt.engine.core.common.businessentities.Snapshot();
org.ovirt.engine.core.common.businessentities.Snapshot resultSnapshot = new org.ovirt.engine.core.common.businessentities.Snapshot(false);
resultSnapshot.setVmConfiguration(ovfData);
resultSnapshot.setId(SNAPSHOT_ID);
resultSnapshot.setDescription(SNAPSHOT_DESCRIPTION);
when(httpHeaders.getRequestHeader(BackendResource.ALL_CONTENT_HEADER)).thenReturn(populates);
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(asList(getEntity(1)));
setUpEntityQueryExpectations(QueryType.GetSnapshotBySnapshotId,
IdQueryParameters.class,
new String[]{"Id"},
new Object[]{SNAPSHOT_ID},
resultSnapshot);
setUpGetEntityExpectations(resultSnapshot);
Snapshot snapshot = resource.get();
verifyModel(snapshot, 1);
assertNotNull(snapshot.getInitialization());
Expand All @@ -82,19 +79,9 @@ public void testGetWithPopulate() {
}

@Test
//empty list of snapshots returned from Backend.
public void testGetNotFound1() {
public void testGetNotFound() {
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(new ArrayList<>());
verifyNotFoundException(assertThrows(WebApplicationException.class, resource::get));
}

@Test
//non-empty list of snapshots returned from Backend,
//but this specific snapshot is not there.
public void testGetNotFound2() {
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(asList(getEntity(2)));
setUpGetEntityExpectations(null);
verifyNotFoundException(assertThrows(WebApplicationException.class, resource::get));
}

Expand All @@ -109,7 +96,7 @@ public void testRestore() {
@Test
public void testRemove() {
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(asList(getEntity(1)));
setUpGetEntityExpectations(getEntity(1));
setUpActionExpectations(
ActionType.RemoveSnapshot,
RemoveSnapshotParameters.class,
Expand All @@ -132,7 +119,7 @@ public void testRemoveFailed() {
}

protected void doTestBadRemove(boolean valid, boolean success, String detail) {
setUpGetEntityExpectations(asList(getEntity(1)));
setUpGetEntityExpectations(getEntity(1));
setUriInfo(
setUpActionExpectations(
ActionType.RemoveSnapshot,
Expand Down Expand Up @@ -170,11 +157,11 @@ protected UriInfo setUpRestoreExpectations() {
null);
}

protected void setUpGetEntityExpectations(List<org.ovirt.engine.core.common.businessentities.Snapshot> result) {
setUpGetEntityExpectations(QueryType.GetAllVmSnapshotsByVmId,
protected void setUpGetEntityExpectations(org.ovirt.engine.core.common.businessentities.Snapshot result) {
setUpGetEntityExpectations(QueryType.GetSnapshotBySnapshotId,
IdQueryParameters.class,
new String[] { "Id" },
new Object[] { VM_ID },
new Object[] { SNAPSHOT_ID },
result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,10 @@ public void testListCrashClientLocale() {
private void doTestAddAsync(AsyncTaskStatusEnum asyncStatus, CreationStatus creationStatus) {
setUriInfo(setUpBasicUriExpectations());
String ovfData = "data";
org.ovirt.engine.core.common.businessentities.Snapshot resultSnapshot0 = new org.ovirt.engine.core.common.businessentities.Snapshot();
org.ovirt.engine.core.common.businessentities.Snapshot resultSnapshot0 = new org.ovirt.engine.core.common.businessentities.Snapshot(false);
resultSnapshot0.setVmConfiguration(ovfData);
resultSnapshot0.setId(SNAPSHOT_IDS[0]);
setUpEntityQueryExpectations(QueryType.GetSnapshotBySnapshotId,
IdQueryParameters.class,
new String[]{"Id"},
new Object[]{SNAPSHOT_IDS[0]},
resultSnapshot0);
resultSnapshot0.setDescription(DESCRIPTIONS[0]);
setUpCreationExpectations(ActionType.CreateSnapshotForVm,
CreateSnapshotForVmParameters.class,
new String[] { "Description", "VmId" },
Expand All @@ -174,11 +170,11 @@ private void doTestAddAsync(AsyncTaskStatusEnum asyncStatus, CreationStatus crea
GUIDS[0],
asList(TASK_ID),
asList(new AsyncTaskStatus(asyncStatus)),
QueryType.GetAllVmSnapshotsByVmId,
QueryType.GetSnapshotBySnapshotId,
IdQueryParameters.class,
new String[] { "Id" },
new Object[] { VM_ID },
getEntity(0));
new Object[] { SNAPSHOT_IDS[0] },
resultSnapshot0);
Snapshot snapshot = new Snapshot();
snapshot.setDescription(DESCRIPTIONS[0]);

Expand All @@ -188,9 +184,8 @@ private void doTestAddAsync(AsyncTaskStatusEnum asyncStatus, CreationStatus crea
Snapshot responseSnapshot = (Snapshot)response.getEntity();
verifyModel(responseSnapshot, 0);
verifyAllContent(responseSnapshot, ConfigurationType.OVF, ovfData);
Snapshot created = (Snapshot)response.getEntity();
assertNotNull(created.getCreationStatus());
assertEquals(creationStatus.value(), created.getCreationStatus());
assertNotNull(responseSnapshot.getCreationStatus());
assertEquals(creationStatus.value(), responseSnapshot.getCreationStatus());
}

protected void setUpGetEntityExpectations(int times) {
Expand Down

0 comments on commit cee41a5

Please sign in to comment.