Commit a4576176 authored by satish kumar nargani's avatar satish kumar nargani
Browse files

Added integration test cases for azure implementation

parent cacf454c
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2017-2019 Schlumberger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-file-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>org.opengroup.osdu.file.azure</groupId>
<artifactId>file-test-azure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.main.basedir>${project.basedir}</project.main.basedir>
<jackson.version>2.10.1</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>file-test-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.10.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>file-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>${repo.releases.id}</id>
<url>${repo.releases.url}</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>${publish.releases.id}</id>
<url>${publish.releases.url}</url>
</repository>
<snapshotRepository>
<id>${publish.snapshots.id}</id>
<url>${publish.snapshots.url}</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/file</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>
</project>
package org.opengroup.osdu.file.azure;
import org.opengroup.osdu.file.HttpClient;
import java.io.IOException;
import com.google.common.base.Strings;
import util.AzureServicePrincipal;
public class HttpClientAzure extends HttpClient {
@Override
public String getAccessToken() throws IOException {
if (Strings.isNullOrEmpty(accessToken)) {
String sp_id = System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER"));
String sp_secret = System.getProperty("TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("TESTER_SERVICEPRINCIPAL_SECRET"));
String tenant_id = System.getProperty("AZURE_AD_TENANT_ID", System.getenv("AZURE_AD_TENANT_ID"));
String app_resource_id = System.getProperty("AZURE_AD_APP_RESOURCE_ID", System.getenv("AZURE_AD_APP_RESOURCE_ID"));
accessToken = AzureServicePrincipal.getIdToken(sp_id, sp_secret, tenant_id, app_resource_id);
}
return "Bearer " + accessToken;
}
@Override
public String getNoDataAccessToken() throws IOException {
if (Strings.isNullOrEmpty(noDataAccessToken)) {
String sp_id = System.getProperty("NO_DATA_ACCESS_TESTER", System.getenv("NO_DATA_ACCESS_TESTER"));
String sp_secret = System.getProperty("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET"));
String tenant_id = System.getProperty("AZURE_AD_TENANT_ID", System.getenv("AZURE_AD_TENANT_ID"));
String app_resource_id = System.getProperty("AZURE_AD_APP_RESOURCE_ID", System.getenv("AZURE_AD_APP_RESOURCE_ID"));
noDataAccessToken = AzureServicePrincipal.getIdToken(sp_id, sp_secret, tenant_id, app_resource_id);
}
return "Bearer " + noDataAccessToken;
}
}
package org.opengroup.osdu.file.azure.apitest;
import com.sun.jersey.api.client.ClientResponse;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import static org.hamcrest.CoreMatchers.containsString;
import org.junit.jupiter.api.Test;
import org.opengroup.osdu.core.common.model.file.FileLocationResponse;
import org.opengroup.osdu.core.common.model.file.LocationResponse;
import org.opengroup.osdu.file.apitest.Config;
import org.opengroup.osdu.file.apitest.File;
import org.opengroup.osdu.file.azure.HttpClientAzure;
import org.opengroup.osdu.file.util.FileUtils;
import util.DummyRecordsHelper;
import util.FileUtilsAzure;
import util.StorageUtilAzure;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class TestFile extends File {
protected static final DummyRecordsHelper RECORDS_HELPER = new DummyRecordsHelper();
@BeforeAll
public static void setUp() throws IOException {
System.out.println("Inside Setup method ");
client = new HttpClientAzure();
cloudStorageUtil = new StorageUtilAzure();
}
@Test
@Override
public void getLocationShouldReturnForbidden_whenGivenNoDataAccess() throws Exception {
System.out.println("Override1");
ClientResponse getLocationResponse = client.send(
getLocation,
"POST",
getHeaders(Config.getDataPartitionId(), client.getNoDataAccessToken()),
"{}");
assertEquals(HttpStatus.SC_UNAUTHORIZED, getLocationResponse.getStatus());
}
@Test
@Override
public void shouldReturnUnauthorized_whenGivenAnonimus() throws Exception {
System.out.println("Override2");
ClientResponse getLocationResponse = client.send(
getLocation,
"POST",
getHeaders(Config.getDataPartitionId(), null),
"{}");
assertEquals(HttpStatus.SC_FORBIDDEN, getLocationResponse.getStatus());
}
@Test
@Override
public void shouldReturnUnauthorized_whenPartitionIdNotGiven() throws Exception {
System.out.println("Override3");
ClientResponse getLocationResponse = client.send(
getLocation,
"POST",
getHeaders(null, client.getAccessToken()),
"{}");
assertEquals(HttpStatus.SC_FORBIDDEN, getLocationResponse.getStatus());
}
@Test
@Override
public void shouldReturnUnauthorized_whenGivenInvalidPartitionId() throws Exception {
System.out.println("Override4");
ClientResponse getLocationResponse = client.send(
getLocation,
"POST",
getHeaders("invalid_partition", client.getAccessToken()),
"{}");
assertEquals(HttpStatus.SC_FORBIDDEN, getLocationResponse.getStatus());
}
@Test
public void getLocationShouldReturnBadrequest_whenGivenAlreadyExistsFileID() throws Exception {
String fileID = System.getProperty("EXIST_FILE_ID", System.getenv("EXIST_FILE_ID"));
ClientResponse response = client.send(
getLocation,
"POST",
getCommonHeader(),
FileUtils.generateFileRequestBody(fileID)
);
assertNotNull(response);
assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatus());
}
@Test
public void getLocationShouldReturnBadrequest_whenFileIDLengthExceeds() throws Exception {
String fileID = FileUtilsAzure.generateUniqueFileID();
ClientResponse response = client.send(
getLocation,
"POST",
getCommonHeader(),
FileUtils.generateFileRequestBody(fileID)
);
assertNotNull(response);
assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatus());
DummyRecordsHelper.BadRequestMock responseObject = RECORDS_HELPER.getRecordsMockFromBadRequestResponse(response);
String resp = "The maximum filepath length is 1024 characters";
assertThat(responseObject.message, containsString(resp));
}
@Test
public void getFileLocationShouldReturnBadrequest_whenRequestBodyEmpty() throws Exception {
ClientResponse getFileLocationResponse = client.send(
getFileLocation,
"POST",
getCommonHeader(),
"{}");
assertNotNull(getFileLocationResponse);
assertEquals(HttpStatus.SC_BAD_REQUEST, getFileLocationResponse.getStatus());
DummyRecordsHelper.BadRequestMock responseObject = RECORDS_HELPER.getRecordsMockFromBadRequestResponse(getFileLocationResponse);
String resp = "ConstraintViolationException: Invalid FileLocationRequest";
assertThat(responseObject.message, containsString(resp));
}
@Test
public void getFileLocationShouldReturnBadrequest_whenGivenNotValidFileId() throws Exception {
ClientResponse getFileLocationResponse = client.send(
getFileLocation,
"POST",
getCommonHeader(),
DummyRecordsHelper.buildInvalidFileLocationPayload("test"));
assertNotNull(getFileLocationResponse);
assertEquals(HttpStatus.SC_BAD_REQUEST, getFileLocationResponse.getStatus());
DummyRecordsHelper.BadRequestMock responseObject = RECORDS_HELPER.getRecordsMockFromBadRequestResponse(getFileLocationResponse);
String resp = "ConstraintViolationException: Invalid FileLocationRequest";
assertThat(responseObject.message, containsString(resp));
}
@Test
public void getFileListShouldReturnForbidden_whenGivenNoDataAccess() throws Exception {
ClientResponse getLocationResponse = client.send(
getFileList,
"POST",
getHeaders(Config.getDataPartitionId(), client.getNoDataAccessToken()),
"{}");
assertEquals(HttpStatus.SC_UNAUTHORIZED, getLocationResponse.getStatus());
}
@Test
public void getFileListShouldReturnUnauthorized_whenGivenAnonimus() throws Exception {
ClientResponse getLocationResponse = client.send(
getFileList,
"POST",
getHeaders(Config.getDataPartitionId(), null),
"{}");
assertEquals(HttpStatus.SC_FORBIDDEN, getLocationResponse.getStatus());
}
@Test
public void getFileListShouldReturnUnauthorized_whenPartitionIdNotGiven() throws Exception {
ClientResponse getLocationResponse = client.send(
getFileList,
"POST",
getHeaders(null, client.getAccessToken()),
"{}");
assertEquals(HttpStatus.SC_FORBIDDEN, getLocationResponse.getStatus());
}
@Test
public void getFileListShouldReturnUnauthorized_whenGivenInvalidPartitionId() throws Exception {
ClientResponse getLocationResponse = client.send(
getFileList,
"POST",
getHeaders("invalid_partition", client.getAccessToken()),
"{}");
assertEquals(HttpStatus.SC_FORBIDDEN, getLocationResponse.getStatus());
}
@Test
public void getFileList_shouldReturnBadrequest_whenrecordnotFound() throws Exception {
LocalDateTime from = LocalDateTime.now(ZoneId.of(Config.getTimeZone()));
LocalDateTime to = LocalDateTime.now(ZoneId.of(Config.getTimeZone()));
String fileListRequestBody = FileUtilsAzure.generateFileListRequestBody(0,from, to, (short) 1);
ClientResponse fileListResponse = client.send(
getFileList,
"POST",
getCommonHeader(),
fileListRequestBody
);
assertNotNull(fileListResponse);
assertEquals(HttpStatus.SC_BAD_REQUEST, fileListResponse.getStatus());
}
@Test
public void getFileListShouldReturnBadrequest_whenRequestBodyEmpty() throws Exception {
ClientResponse getFileLocationResponse = client.send(
getFileList,
"POST",
getCommonHeader(),
"{}");
assertNotNull(getFileLocationResponse);
assertEquals(HttpStatus.SC_BAD_REQUEST, getFileLocationResponse.getStatus());
DummyRecordsHelper.BadRequestMock responseObject = RECORDS_HELPER.getRecordsMockFromBadRequestResponse(getFileLocationResponse);
String resp = "ConstraintViolationException: Invalid FileListRequest";
assertThat(responseObject.message, containsString(resp));
}
//To Do
// This tear down method should get refactored to clean up the signed file location when there is change in azure main implementation
@AfterAll
public static void tearDown() throws Exception {
if (!locationResponses.isEmpty()) {
for (LocationResponse response : locationResponses) {
ClientResponse getFileLocationResponse = client.send(
getFileLocation,
"POST",
getCommonHeader(),
FileUtils.generateFileRequestBody(response.getFileID()));
FileLocationResponse fileLocationResponse = mapper
.readValue(getFileLocationResponse.getEntity(String.class), FileLocationResponse.class);
String filename = fileLocationResponse.getLocation();
cloudStorageUtil.deleteCloudFile("TestContainer", filename);
}
}
}
}
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AzureBlobService {
private static String storageAccount;
AzureBlobService(String storageAccount) {
this.storageAccount = storageAccount;
}
public static String getStorageAccount() {
return System.getProperty("AZURE_STORAGE_ACCOUNT", System.getenv("AZURE_STORAGE_ACCOUNT"));
}
private static String generateContainerPath(String accountName, String containerName) {
return String.format("https://%s.blob.core.windows.net/%s", accountName, containerName);
}
}
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util;
import lombok.Data;
@Data
public class AzureBlobServiceConfig {
private String storageAccount;
public AzureBlobServiceConfig(String storageAccount) {
this.storageAccount = storageAccount;
}
public AzureBlobService azureBlobService() {
return new AzureBlobService(storageAccount);
}
}
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class AzureServicePrincipal {
public static String getIdToken(String sp_id, String sp_secret, String tenant_id, String app_resource_id) throws IOException {
String aad_endpoint = String.format("https://login.microsoftonline.com/%s/oauth2/token", tenant_id);
URL url = new URL(aad_endpoint);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
Map<String, String> parameters = new HashMap<>();
parameters.put("grant_type", "client_credentials");
parameters.put("client_id", sp_id);
parameters.put("client_secret", sp_secret);
parameters.put("resource", app_resource_id);
con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(getParamsString(parameters));
out.flush();
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
Gson gson = new Gson();
JsonObject jobj = gson.fromJson(content.toString(), JsonObject.class);
String token = jobj.get("access_token").getAsString();
return token;
}
private static String getParamsString(Map<String, String> params)
throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
result.append("&");
}
String resultString = result.toString();
return resultString.length() > 0
? resultString.substring(0, resultString.length() - 1)
: resultString;
}
}