Commit bb16ef05 authored by neelesh thakur's avatar neelesh thakur
Browse files

remove gcp dependency from unit tests

parent 3300192f
Pipeline #3337 passed with stages
in 18 minutes and 29 seconds
......@@ -130,7 +130,7 @@ public class DatastoreDdmsRepository implements IDdmsRepository {
return output;
}
private Key createkey(Datastore ds, String id) {
Key createkey(Datastore ds, String id) {
return ds.newKeyFactory()
.setNamespace(NAMESPACE)
.setKind(DMS_KIND)
......@@ -138,7 +138,7 @@ public class DatastoreDdmsRepository implements IDdmsRepository {
}
//unchecked
private Ddms convertEntityToDms(Entity entity) {
Ddms convertEntityToDms(Entity entity) {
Ddms ddms = new Ddms();
ddms.setId(entity.getKey().getName());
ddms.setName(entity.getString(NAME));
......
......@@ -17,18 +17,18 @@
package org.opengroup.osdu.register.provider.gcp.ddms.datastore;
import com.google.cloud.Timestamp;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.Spy;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.register.ddms.model.Ddms;
import org.opengroup.osdu.register.ddms.model.RegisteredInterface;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.Collections;
import java.util.List;
......@@ -36,71 +36,54 @@ import java.util.UUID;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
@RunWith(MockitoJUnitRunner.class)
@RunWith(PowerMockRunner.class)
@PrepareForTest({Datastore.class, KeyFactory.class, Key.class})
public class DatastoreDdmsRepositoryTest {
@Mock
DatastoreMultiTenantAccess dsTenants;
private DatastoreMultiTenantAccess dataStoreTenants;
@Mock
private TenantInfo tenantInfo;
@Mock
private Datastore datastore;
@Mock
TenantInfo tenantInfo;
private KeyFactory keyFactory;
@InjectMocks
DatastoreDdmsRepository sut;
Datastore ds = DatastoreOptions.getDefaultInstance().getService();
@Spy
private DatastoreDdmsRepository sut = new DatastoreDdmsRepository();
private final Timestamp createdOn = Timestamp.now();
@Before
public void setup() {
when(dsTenants.get(any())).thenReturn(ds);
initMocks(this);
when(datastore.newKeyFactory()).thenReturn(keyFactory);
when(dataStoreTenants.get(any())).thenReturn(datastore);
}
@Test
public void should_createGetAndDeleteDms_fromDatastore() {
Ddms input = createDms();
try {
Ddms output = sut.create(input);
assertEquals(output, input);
output = sut.get(input.getId());
assertEquals(output, input);
} finally {
assertTrue(sut.delete(input.getId()));
}
try {
sut.get(input.getId());
fail("Expected app exception");
} catch (AppException e) {
assertEquals(404, e.getError().getCode());
}
}
@Test
public void should_retrieveMultipleDms_whenQueryMatchesType() {
RegisteredInterface ri = new RegisteredInterface();
ri.setSchema(Collections.singletonMap("1", "2"));
ri.setEntityType("type2");
Ddms input1 = createDms();
Ddms input2 = createDms();
input2.getInterfaces().add(ri);
Ddms input3 = createDms();
input3.getInterfaces().add(ri);
try {
sut.create(input1);
sut.create(input2);
sut.create(input3);
List<Ddms> result = sut.query("type2");
assertTrue(result.contains(input2));
} finally {
sut.delete(input1.getId());
sut.delete(input2.getId());
sut.delete(input3.getId());
}
Key mockKey = mockKey(1L);
doReturn(mockKey).when(sut).createkey(any(), any());
mockTransaction(Collections.emptyList(), false);
Ddms output = sut.create(input);
assertEquals(output, input);
Entity datastoreEntity = createEntity(mockKey);
when(this.datastore.get(mockKey)).thenReturn(datastoreEntity);
doReturn(input).when(this.sut).convertEntityToDms(any());
output = sut.get(input.getId());
assertEquals(output, input);
Transaction txn = mockTransaction(Collections.singletonList(mockKey), false);
when(txn.get(mockKey)).thenReturn(datastoreEntity);
assertTrue(sut.delete(input.getId()));
}
private Ddms createDms() {
......@@ -109,12 +92,49 @@ public class DatastoreDdmsRepositoryTest {
ri.setEntityType("type");
ri.setSchema(Collections.singletonMap("1", "2"));
input.setContactEmail("ash");
input.setDescription("2");
input.setContactEmail("mock-contactEmail");
input.setDescription("mock-description");
input.setId(UUID.randomUUID().toString());
input.setName("name");
input.setCreatedDateTimeEpoch(Timestamp.now());
input.setName("mock-name");
input.setCreatedDateTimeEpoch(createdOn);
input.getInterfaces().add(ri);
return input;
}
private Key mockKey(Long id) {
Key key = mock(Key.class);
when(key.getKind()).thenReturn("key-name");
when(key.getNamespace()).thenReturn("namespace");
when(key.getId()).thenReturn(id);
when(key.getProjectId()).thenReturn("mock-project");
when(keyFactory.setKind(any())).thenReturn(keyFactory);
when(keyFactory.setNamespace(any())).thenReturn(keyFactory);
when(keyFactory.newKey(anyInt())).thenReturn(key);
return key;
}
private Transaction mockTransaction(List<Key> keys, Boolean isActive) {
Transaction txn = mock(Transaction.class);
when(this.datastore.newTransaction()).thenReturn(txn);
Transaction.Response rsp = mock(Transaction.Response.class);
when(txn.commit()).thenReturn(rsp);
when(txn.isActive()).thenReturn(isActive);
when(rsp.getGeneratedKeys()).thenReturn(keys.isEmpty() ? Collections.emptyList() : keys);
return txn;
}
private Entity createEntity(Key key) {
return getEntityBuilder(key)
.build();
}
private Entity.Builder getEntityBuilder(Key key) {
return Entity.newBuilder(key)
.set("name", "mock-name")
.set("description", "mock-description")
.set("contactEmail", "mock-contactEmail")
.set("createdOn", "createdOn")
.set("kind", "DDMS")
.set("namespace", "DE");
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment