Commit 1dce01a4 authored by Aman Verma's avatar Aman Verma Committed by Kishore Battula
Browse files

addressig MR comments of previous PR

parent 8c97f21e
......@@ -111,37 +111,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-webapp-maven-plugin</artifactId>
<version>1.7.0</version>
<configuration>
<schemaVersion>V2</schemaVersion>
<authentication>
<serverId>azure-auth</serverId>
</authentication>
<subscriptionId>${azure.appservice.subscription}</subscriptionId>
<resourceGroup>${azure.appservice.resourcegroup}</resourceGroup>
<appServicePlanName>${azure.appservice.plan}</appServicePlanName>
<appName>${azure.appservice.appname}</appName>
<appSettings>
<property>
<name>JAVA_OPTS</name>
<value>-Dserver.port=80</value>
</property>
</appSettings>
<deployment>
<resources>
<resource>
<directory>${project.basedir}/target</directory>
<includes>
<include>*spring-boot.jar</include>
</includes>
</resource>
</resources>
</deployment>
</configuration>
</plugin>
</plugins>
</build>
......
......@@ -34,6 +34,5 @@ public class SchemaApplication {
CosmosContainerConfig.class
};
SpringApplication.run(sources, args);
SpringApplication.run(SchemaApplication.class, args);
}
}
......@@ -187,7 +187,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
pars.add(new SqlParameter("@majorVersion", schemaInfo.getSchemaIdentity().getSchemaVersionMajor()));
FeedOptions options = new FeedOptions();
options.setEnableCrossPartitionQuery(true);
options.setEnableCrossPartitionQuery(false);
List<SchemaInfoDoc> schemaInfoList = cosmosStore.queryItems(headers.getPartitionId(), cosmosDBName,schemaInfoContainer, query, options, SchemaInfoDoc.class);
TreeMap<Long, String> sortedMap = new TreeMap<>(Collections.reverseOrder());
......@@ -316,7 +316,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
}
FeedOptions options = new FeedOptions();
options.setEnableCrossPartitionQuery(true);
options.setEnableCrossPartitionQuery(false);
List<SchemaInfoDoc> schemaInfoList = cosmosStore.queryItems(headers.getPartitionId(), cosmosDBName,schemaInfoContainer, query, options, SchemaInfoDoc.class);
List<SchemaInfo> schemaList = new LinkedList<>();
......
......@@ -67,24 +67,16 @@ public class OSDUAzureBlobStorageImpl {
public String writeToBlob(String filePath, String content) throws ApplicationException {
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
int bytesSize = bytes.length;
BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient();
if (blockBlobClient.exists())
{
// we need to clean up the previous schema content.
log.info("Cleaning up the previously existing blob");
this.deleteFromBlob(filePath);
}
BlockBlobClient newBlockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient();
try (ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes)) {
newBlockBlobClient.upload(dataStream, bytesSize);
blockBlobClient.upload(dataStream, bytesSize, true);
} catch (Exception ex) {
log.warning(ex.getMessage());
throw new ApplicationException("Write to blob failed");
}
// Get the blobname
return newBlockBlobClient.getBlobName();
return blockBlobClient.getBlobName();
}
}
......@@ -59,18 +59,29 @@ public class AzureAuthorityStoreTest {
@Mock
JaxRsDpsLog log;
private static final String dataPartitionId = "testPartitionId";
private static final String authorityId = "testAuthorityId";
@Before
public void init() {
initMocks(this);
Mockito.when(headers.getPartitionId()).thenReturn(dataPartitionId);
Mockito.when(mockAuthority.getAuthorityId()).thenReturn(authorityId);
}
@Test
public void testGetAuthority() throws NotFoundException, ApplicationException, IOException {
String authorityId = "testAuthorityId";
Mockito.when(headers.getPartitionId()).thenReturn("test");
AuthorityDoc authorityDoc = getAuthorityDoc("test", authorityId);
AuthorityDoc authorityDoc = getAuthorityDoc(dataPartitionId, authorityId);
Optional<AuthorityDoc> cosmosItem = Optional.of(authorityDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + authorityId),
eq(dataPartitionId),
any());
assertNotNull(store.get(authorityId));
assertEquals(authorityId, store.get(authorityId).getAuthorityId());
......@@ -78,12 +89,18 @@ public class AzureAuthorityStoreTest {
@Test
public void testGetAuthority_NotFoundException() throws IOException {
String authorityId = "";
Mockito.when(headers.getPartitionId()).thenReturn("test");
Optional<AuthorityDoc> cosmosItem = Optional.empty();
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + ""),
eq(dataPartitionId),
any());
try {
store.get(authorityId);
store.get("");
fail("Should not succeed");
} catch (NotFoundException e) {
assertEquals("bad input parameter", e.getMessage());
......@@ -95,20 +112,24 @@ public class AzureAuthorityStoreTest {
@Test
public void testCreateAuthority() throws ApplicationException, BadRequestException {
Mockito.when(headers.getPartitionId()).thenReturn("test");
Mockito.when(mockAuthority.getAuthorityId()).thenReturn("testAuthorityId");
doNothing().when(cosmosStore).upsertItem(anyString(), any(), any(), any());
doNothing().when(cosmosStore).upsertItem(eq(dataPartitionId), any(), any(), any());
assertNotNull(store.create(mockAuthority));
}
@Test
public void testCreateAuthority_BadRequestException()
throws NotFoundException, ApplicationException, BadRequestException, IOException {
Mockito.when(headers.getPartitionId()).thenReturn("test");
Mockito.when(mockAuthority.getAuthorityId()).thenReturn("testAuthorityId");
AuthorityDoc authorityDoc = getAuthorityDoc("test", "testAuthorityId");
AuthorityDoc authorityDoc = getAuthorityDoc(dataPartitionId, authorityId);
Optional<AuthorityDoc> cosmosItem = Optional.of(authorityDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + authorityId),
eq(dataPartitionId),
any());
try {
store.create(mockAuthority);
......@@ -124,12 +145,17 @@ public class AzureAuthorityStoreTest {
@Test
public void testCreateAuthority_ApplicationException()
throws NotFoundException, ApplicationException, BadRequestException, CosmosClientException {
Mockito.when(headers.getPartitionId()).thenReturn("test");
Mockito.when(mockAuthority.getAuthorityId()).thenReturn("testAuthorityId");
Optional<AuthorityDoc> cosmosItem = Optional.empty();
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doThrow(AppException.class).when(cosmosStore).upsertItem(anyString(), any(), any(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + authorityId),
eq(dataPartitionId),
any());
doThrow(AppException.class).when(cosmosStore).upsertItem(eq(dataPartitionId), any(), any(), any());
try {
store.create(mockAuthority);
fail("Should not succeed");
......
......@@ -59,32 +59,49 @@ public class AzureEntityTypeStoreTest {
@Mock
JaxRsDpsLog log;
private static final String dataPartitionId = "testPartitionId";
private static final String entityTypeId = "testEntityId";
@Before
public void init() {
initMocks(this);
Mockito.when(headers.getPartitionId()).thenReturn(dataPartitionId);
Mockito.when(mockEntityType.getEntityTypeId()).thenReturn(entityTypeId);
}
@Test
public void testGetEntityType() throws NotFoundException, ApplicationException, IOException {
String EntityTypeId = "testEntityId";
Mockito.when(headers.getPartitionId()).thenReturn("test");
EntityTypeDoc entityTypeDoc = getEntityTypeDoc("test", EntityTypeId);
EntityTypeDoc entityTypeDoc = getEntityTypeDoc(dataPartitionId, entityTypeId);
Optional<EntityTypeDoc> cosmosItem = Optional.of(entityTypeDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
assertNotNull(store.get(EntityTypeId));
assertEquals(EntityTypeId, store.get(EntityTypeId).getEntityTypeId());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + entityTypeId),
eq(dataPartitionId),
any());
assertNotNull(store.get(entityTypeId));
assertEquals(entityTypeId, store.get(entityTypeId).getEntityTypeId());
}
@Test
public void testGetEntityType_NotFoundException() throws IOException {
String EntityTypeId = "";
Mockito.when(headers.getPartitionId()).thenReturn("test");
Optional<EntityTypeDoc> cosmosItem = Optional.empty();
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + ""),
eq(dataPartitionId),
any());
try {
store.get(EntityTypeId);
store.get("");
fail("Should not succeed");
} catch (NotFoundException e) {
assertEquals("bad input parameter", e.getMessage());
......@@ -96,8 +113,7 @@ public class AzureEntityTypeStoreTest {
@Test
public void testCreateEntityType() throws ApplicationException, BadRequestException {
Mockito.when(headers.getPartitionId()).thenReturn("test");
Mockito.when(mockEntityType.getEntityTypeId()).thenReturn("testEntityId");
Mockito.when(mockEntityType.getEntityTypeId()).thenReturn(entityTypeId);
doNothing().when(cosmosStore).upsertItem(anyString(), any(), any(), any());
assertNotNull(store.create(mockEntityType));
}
......@@ -105,9 +121,7 @@ public class AzureEntityTypeStoreTest {
@Test
public void testCreateEntityType_BadRequestException()
throws NotFoundException, ApplicationException, BadRequestException, IOException {
Mockito.when(headers.getPartitionId()).thenReturn("test");
Mockito.when(mockEntityType.getEntityTypeId()).thenReturn("testEntityId");
EntityTypeDoc entityTypeDoc = getEntityTypeDoc("test", "testEntityId");
EntityTypeDoc entityTypeDoc = getEntityTypeDoc(dataPartitionId, entityTypeId);
Optional<EntityTypeDoc> cosmosItem = Optional.of(entityTypeDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
......@@ -125,10 +139,16 @@ public class AzureEntityTypeStoreTest {
@Test
public void testCreateEntityType_ApplicationException()
throws NotFoundException, ApplicationException, BadRequestException, CosmosClientException {
Mockito.when(headers.getPartitionId()).thenReturn("test");
Mockito.when(mockEntityType.getEntityTypeId()).thenReturn("testEntityId");
Optional<EntityTypeDoc> cosmosItem = Optional.empty();
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + entityTypeId),
eq(dataPartitionId),
any());
doThrow(AppException.class).when(cosmosStore).upsertItem(anyString(), any(), any(), any());
try {
......
......@@ -102,7 +102,15 @@ public class AzureSchemaInfoStoreTest {
public void testGetLatestMinorVersion_ReturnNull() throws NotFoundException, ApplicationException, IOException {
List<SchemaInfoDoc> cosmosItem = new ArrayList<>();
doReturn(cosmosItem).when(cosmosStore).queryItems(anyString(), anyString(), anyString(), any(), any(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.queryItems(
eq(dataPartitionId),
anyString(),
anyString(),
any(),
any(),
any());
assertEquals("", schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo()));
}
......@@ -110,16 +118,27 @@ public class AzureSchemaInfoStoreTest {
public void testGetLatestMinorVersion_Entity() throws NotFoundException, ApplicationException {
List<SchemaInfoDoc> schemaInfoDocsList = new LinkedList<>();
schemaInfoDocsList.add(getMockSchemaInfoDoc());
doReturn(schemaInfoDocsList).when(cosmosStore).queryItems(anyString(), any(), any(), any(), any(), any());
doReturn(schemaInfoDocsList)
.when(cosmosStore)
.queryItems(eq(dataPartitionId), any(), any(), any(), any(), any());
Mockito.when(schemaStore.getSchema(dataPartitionId, schemaId)).thenReturn(CONTENT);
assertEquals(CONTENT, schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo()));
}
@Test
public void testGetSchemaInfo_NotEmpty() throws NotFoundException, ApplicationException {
Optional<SchemaInfoDoc> cosmosItem = Optional.of(schemaInfoDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
doReturn(getFlattenedSchemaInfo()).when(schemaInfoDoc).getFlattenedSchemaInfo();
SchemaInfo schemaInfo = schemaInfoStore.getSchemaInfo(schemaId);
......@@ -132,14 +151,30 @@ public class AzureSchemaInfoStoreTest {
expectedException.expectMessage(SchemaConstants.SCHEMA_NOT_PRESENT);
Optional<SchemaInfoDoc> cosmosItem = Optional.empty();
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
schemaInfoStore.getSchemaInfo(schemaId);
}
@Test
public void testCreateSchemaInfo_Positive() throws ApplicationException, BadRequestException {
// the schema is not present in schemaInfoStore
doReturn(Optional.empty()).when(cosmosStore).findItem(anyString(), any(), any(), eq(dataPartitionId + ":" + schemaId), anyString(), any());
doReturn(Optional.empty())
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
doReturn(getFlattenedSchemaInfo()).when(schemaInfoDoc).getFlattenedSchemaInfo();
assertNotNull(schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published()));
......@@ -148,10 +183,26 @@ public class AzureSchemaInfoStoreTest {
@Test
public void testCreateSchemaInfo_WithSupersededBy()
throws NotFoundException, ApplicationException, BadRequestException {
doReturn(Optional.empty()).when(cosmosStore).findItem(anyString(), any(), any(), eq(dataPartitionId + ":" + schemaId), anyString(), any());
doReturn(Optional.empty())
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
Optional<SchemaInfoDoc> cosmosItem = Optional.of(schemaInfoDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(any(), any(), any(), eq(dataPartitionId + ":" + supersedingSchemaId), any(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + supersedingSchemaId),
eq(dataPartitionId),
any());
doReturn(getFlattenedSchemaInfo_SupersededBy()).when(schemaInfoDoc).getFlattenedSchemaInfo();
assertNotNull(schemaInfoStore.createSchemaInfo(getMockSchemaObject_SupersededBy()));
......@@ -162,7 +213,15 @@ public class AzureSchemaInfoStoreTest {
throws NotFoundException, ApplicationException, BadRequestException {
Optional<SchemaInfoDoc> cosmosItem = Optional.of(schemaInfoDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), eq(dataPartitionId + ":" + schemaId), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
try {
schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published());
......@@ -179,7 +238,15 @@ public class AzureSchemaInfoStoreTest {
public void testCreateSchemaInfo_ApplicationException()
throws NotFoundException, ApplicationException, BadRequestException {
doReturn(Optional.empty()).when(cosmosStore).findItem(anyString(), any(), any(), eq(dataPartitionId + ":" + schemaId), anyString(), any());
doReturn(Optional.empty())
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
doThrow(AppException.class).when(cosmosStore).upsertItem(anyString(), any(), any(), any());
......@@ -197,14 +264,30 @@ public class AzureSchemaInfoStoreTest {
@Test
public void testIsUnique_True() throws ApplicationException {
doReturn(Optional.empty()).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(Optional.empty())
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
assertTrue(schemaInfoStore.isUnique(schemaId, dataPartitionId));
}
@Test
public void testIsUnique_False() throws ApplicationException {
Optional<SchemaInfoDoc> cosmosItem = Optional.of(schemaInfoDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
assertFalse(schemaInfoStore.isUnique(schemaId, dataPartitionId));
}
......@@ -219,14 +302,30 @@ public class AzureSchemaInfoStoreTest {
Collection<TenantInfo> tenants = Lists.newArrayList(tenant1, tenant2);
when(this.tenantFactory.listTenantInfo()).thenReturn(tenants);
Optional<SchemaInfoDoc> cosmosItem = Optional.of(schemaInfoDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), eq(dataPartitionId + ":" + schemaId), anyString(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
assertFalse(schemaInfoStore.isUnique(schemaId, commonTenantId));
}
@Test
public void testUpdateSchemaInfo() throws NotFoundException, ApplicationException, BadRequestException {
Optional<SchemaInfoDoc> cosmosItem = Optional.of(schemaInfoDoc);
doReturn(cosmosItem).when(cosmosStore).findItem(any(), any(), any(), eq(dataPartitionId + ":" + supersedingSchemaId), any(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + supersedingSchemaId),
eq(dataPartitionId),
any());
doReturn(getFlattenedSchemaInfo()).when(schemaInfoDoc).getFlattenedSchemaInfo();
assertNotNull(schemaInfoStore.updateSchemaInfo(getMockSchemaObject_Published()));
......@@ -236,8 +335,24 @@ public class AzureSchemaInfoStoreTest {
public void testUpdateSchemaInfo_SupersededBy()
throws NotFoundException, ApplicationException, BadRequestException {
Optional<SchemaInfoDoc> cosmosItem = Optional.of(getMockSchemaInfoDoc());
doReturn(cosmosItem).when(cosmosStore).findItem(any(), any(), any(), eq(dataPartitionId + ":" + schemaId), any(), any());
doReturn(Optional.of(schemaInfoDoc)).when(cosmosStore).findItem(any(), any(), any(), eq(dataPartitionId + ":" + supersedingSchemaId), any(), any());
doReturn(cosmosItem)
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + schemaId),
eq(dataPartitionId),
any());
doReturn(Optional.of(schemaInfoDoc))
.when(cosmosStore)
.findItem(
eq(dataPartitionId),
any(),
any(),
eq(dataPartitionId + ":" + supersedingSchemaId),
eq(dataPartitionId),
any());
doReturn(getFlattenedSchemaInfo_SupersededBy()).when(schemaInfoDoc).getFlattenedSchemaInfo();