From c04776a7527eceae5b9b71b1174ab7b202ca60c2 Mon Sep 17 00:00:00 2001
From: Anastasiia_Gelmut <anastasiia_gelmut@epam.com>
Date: Fri, 15 Jan 2021 14:17:36 +0400
Subject: [PATCH] GONRG-1537 Finalize Partition Service

-Integration tests are implemented.
---
 testing/partition-test-gcp/pom.xml            | 72 ++++++++++++++
 .../partition/api/TestCreatePartition.java    | 37 +++++++
 .../partition/api/TestDeletePartition.java    | 37 +++++++
 .../partition/api/TestGetPartitionById.java   | 37 +++++++
 .../partition/api/TestListPartitions.java     | 37 +++++++
 .../partition/api/TestUpdatePartition.java    | 37 +++++++
 .../osdu/partition/util/GCPTestUtils.java     | 53 ++++++++++
 .../partition/util/GoogleServiceAccount.java  | 97 +++++++++++++++++++
 testing/pom.xml                               |  3 +-
 9 files changed, 409 insertions(+), 1 deletion(-)
 create mode 100644 testing/partition-test-gcp/pom.xml
 create mode 100644 testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestCreatePartition.java
 create mode 100644 testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestDeletePartition.java
 create mode 100644 testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestGetPartitionById.java
 create mode 100644 testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestListPartitions.java
 create mode 100644 testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestUpdatePartition.java
 create mode 100644 testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/util/GCPTestUtils.java
 create mode 100644 testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/util/GoogleServiceAccount.java

diff --git a/testing/partition-test-gcp/pom.xml b/testing/partition-test-gcp/pom.xml
new file mode 100644
index 000000000..ceffa5abb
--- /dev/null
+++ b/testing/partition-test-gcp/pom.xml
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<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-partition-testing</artifactId>
+    <version>0.0.5-SNAPSHOT</version>
+    <relativePath>../pom.xml</relativePath>
+  </parent>
+
+  <groupId>org.opengroup.osdu.partition</groupId>
+  <artifactId>partition-test-gcp</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <properties>
+    <java.version>1.8</java.version>
+    <maven.compiler.target>1.8</maven.compiler.target>
+    <maven.compiler.source>1.8</maven.compiler.source>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.opengroup.osdu</groupId>
+      <artifactId>core-lib-gcp</artifactId>
+      <version>0.3.25</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.projectlombok</groupId>
+      <artifactId>lombok</artifactId>
+      <version>1.18.8</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-client</artifactId>
+      <version>1.19.4</version>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.12</version>
+    </dependency>
+
+    <dependency>
+      <groupId>com.google.code.gson</groupId>
+      <artifactId>gson</artifactId>
+      <version>2.8.5</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.opengroup.osdu.partition</groupId>
+      <artifactId>partition-test-core</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
+
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava</artifactId>
+      <version>27.1-jre</version>
+    </dependency>
+
+  </dependencies>
+</project>
diff --git a/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestCreatePartition.java b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestCreatePartition.java
new file mode 100644
index 000000000..07090d93b
--- /dev/null
+++ b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestCreatePartition.java
@@ -0,0 +1,37 @@
+/*
+  Copyright 2002-2021 Google LLC
+  Copyright 2002-2021 EPAM Systems, Inc
+
+  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 org.opengroup.osdu.partition.api;
+
+import org.junit.After;
+import org.junit.Before;
+import org.opengroup.osdu.partition.util.GCPTestUtils;
+
+public class TestCreatePartition extends CreatePartitionTest {
+
+  @Override
+  @Before
+  public void setup() throws Exception {
+    this.testUtils = new GCPTestUtils();
+  }
+
+  @Override
+  @After
+  public void tearDown() throws Exception {
+    this.testUtils = null;
+  }
+}
diff --git a/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestDeletePartition.java b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestDeletePartition.java
new file mode 100644
index 000000000..4250d9071
--- /dev/null
+++ b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestDeletePartition.java
@@ -0,0 +1,37 @@
+/*
+  Copyright 2002-2021 Google LLC
+  Copyright 2002-2021 EPAM Systems, Inc
+
+  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 org.opengroup.osdu.partition.api;
+
+import org.junit.After;
+import org.junit.Before;
+import org.opengroup.osdu.partition.util.GCPTestUtils;
+
+public class TestDeletePartition extends DeletePartitionTest {
+
+  @Override
+  @Before
+  public void setup() throws Exception {
+    this.testUtils = new GCPTestUtils();
+  }
+
+  @Override
+  @After
+  public void tearDown() throws Exception {
+    this.testUtils = null;
+  }
+}
diff --git a/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestGetPartitionById.java b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestGetPartitionById.java
new file mode 100644
index 000000000..5446f36e8
--- /dev/null
+++ b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestGetPartitionById.java
@@ -0,0 +1,37 @@
+/*
+  Copyright 2002-2021 Google LLC
+  Copyright 2002-2021 EPAM Systems, Inc
+
+  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 org.opengroup.osdu.partition.api;
+
+import org.junit.After;
+import org.junit.Before;
+import org.opengroup.osdu.partition.util.GCPTestUtils;
+
+public class TestGetPartitionById extends GetPartitionByIdApitTest {
+
+  @Override
+  @Before
+  public void setup() throws Exception {
+    this.testUtils = new GCPTestUtils();
+  }
+
+  @Override
+  @After
+  public void tearDown() throws Exception {
+    this.testUtils = null;
+  }
+}
diff --git a/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestListPartitions.java b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestListPartitions.java
new file mode 100644
index 000000000..940e5bc0f
--- /dev/null
+++ b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestListPartitions.java
@@ -0,0 +1,37 @@
+/*
+  Copyright 2002-2021 Google LLC
+  Copyright 2002-2021 EPAM Systems, Inc
+
+  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 org.opengroup.osdu.partition.api;
+
+import org.junit.After;
+import org.junit.Before;
+import org.opengroup.osdu.partition.util.GCPTestUtils;
+
+public class TestListPartitions extends ListPartitionsApitTest {
+
+  @Override
+  @Before
+  public void setup() throws Exception {
+    this.testUtils = new GCPTestUtils();
+  }
+
+  @Override
+  @After
+  public void tearDown() throws Exception {
+    this.testUtils = null;
+  }
+}
diff --git a/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestUpdatePartition.java b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestUpdatePartition.java
new file mode 100644
index 000000000..df5742718
--- /dev/null
+++ b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/api/TestUpdatePartition.java
@@ -0,0 +1,37 @@
+/*
+  Copyright 2002-2021 Google LLC
+  Copyright 2002-2021 EPAM Systems, Inc
+
+  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 org.opengroup.osdu.partition.api;
+
+import org.junit.After;
+import org.junit.Before;
+import org.opengroup.osdu.partition.util.GCPTestUtils;
+
+public class TestUpdatePartition extends UpdatePartitionTest {
+
+  @Override
+  @Before
+  public void setup() throws Exception {
+    this.testUtils = new GCPTestUtils();
+  }
+
+  @Override
+  @After
+  public void tearDown() throws Exception {
+    this.testUtils = null;
+  }
+}
diff --git a/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/util/GCPTestUtils.java b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/util/GCPTestUtils.java
new file mode 100644
index 000000000..be505489d
--- /dev/null
+++ b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/util/GCPTestUtils.java
@@ -0,0 +1,53 @@
+/*
+  Copyright 2002-2021 Google LLC
+  Copyright 2002-2021 EPAM Systems, Inc
+
+  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 org.opengroup.osdu.partition.util;
+
+import com.google.common.base.Strings;
+
+public class GCPTestUtils extends TestUtils {
+
+  @Override
+  public String getAccessToken() throws Exception {
+    if (Strings.isNullOrEmpty(token)) {
+      String serviceAccountFile = System
+          .getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER"));
+      String audience = System.getProperty("INTEGRATION_TEST_AUDIENCE",
+          System.getenv("INTEGRATION_TEST_AUDIENCE"));
+      if (Strings.isNullOrEmpty(audience)) {
+        audience = "245464679631-ktfdfpl147m1mjpbutl00b3cmffissgq.apps.googleusercontent.com";
+      }
+      token = new GoogleServiceAccount(serviceAccountFile).getAuthToken(audience);
+    }
+    return "Bearer " + token;
+  }
+
+  @Override
+  public String getNoAccessToken() throws Exception {
+    if (Strings.isNullOrEmpty(noAccessToken)) {
+      String serviceAccountFile = System.getProperty("NO_DATA_ACCESS_TESTER",
+          System.getenv("NO_DATA_ACCESS_TESTER"));
+      String audience = System.getProperty("INTEGRATION_TEST_AUDIENCE",
+          System.getenv("INTEGRATION_TEST_AUDIENCE"));
+      if (Strings.isNullOrEmpty(audience)) {
+        audience = "245464679631-ktfdfpl147m1mjpbutl00b3cmffissgq.apps.googleusercontent.com";
+      }
+      noAccessToken = new GoogleServiceAccount(serviceAccountFile).getAuthToken(audience);
+    }
+    return "Bearer " + noAccessToken;
+  }
+}
diff --git a/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/util/GoogleServiceAccount.java b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/util/GoogleServiceAccount.java
new file mode 100644
index 000000000..84e430c01
--- /dev/null
+++ b/testing/partition-test-gcp/src/test/java/org/opengroup/osdu/partition/util/GoogleServiceAccount.java
@@ -0,0 +1,97 @@
+/*
+  Copyright 2002-2021 Google LLC
+  Copyright 2002-2021 EPAM Systems, Inc
+
+  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 org.opengroup.osdu.partition.util;
+
+import com.google.auth.oauth2.ServiceAccountCredentials;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import io.jsonwebtoken.JwtBuilder;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+
+public class GoogleServiceAccount {
+
+  public GoogleServiceAccount(String serviceAccountEncoded) throws IOException {
+    this(Base64.getDecoder().decode(serviceAccountEncoded));
+  }
+
+  public GoogleServiceAccount(byte[] serviceAccountJson) throws IOException {
+    try (InputStream inputStream = new ByteArrayInputStream(serviceAccountJson)) {
+
+      this.serviceAccount = ServiceAccountCredentials.fromStream(inputStream);
+    }
+  }
+
+  final ServiceAccountCredentials serviceAccount;
+
+  public String getEmail() {
+    return this.serviceAccount.getClientEmail();
+  }
+
+  public String getAuthToken(String audience) throws IOException {
+    JwtBuilder jwtBuilder = Jwts.builder();
+
+    Map<String, Object> header = new HashMap<>();
+    header.put("type", "JWT");
+    header.put("alg", "RS256");
+    jwtBuilder.setHeader(header);
+
+    Map<String, Object> claims = new HashMap<>();
+    claims.put("target_audience", audience);
+    claims.put("exp", System.currentTimeMillis() / 1000 + 3600);
+    claims.put("iat", System.currentTimeMillis() / 1000);
+    claims.put("iss", this.getEmail());
+    claims.put("aud", "https://www.googleapis.com/oauth2/v4/token");
+    jwtBuilder.addClaims(claims);
+
+    jwtBuilder.signWith(SignatureAlgorithm.RS256, this.serviceAccount.getPrivateKey());
+    String jwt = jwtBuilder.compact();
+
+    HttpPost httpPost = new HttpPost("https://www.googleapis.com/oauth2/v4/token");
+
+    ArrayList<NameValuePair> postParameters = new ArrayList<>();
+    postParameters
+        .add(new BasicNameValuePair("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"));
+    postParameters.add(new BasicNameValuePair("assertion", jwt));
+
+    HttpClient client = new DefaultHttpClient();
+
+    httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));
+    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
+    HttpResponse response = client.execute(httpPost);
+
+    String responseEntity = EntityUtils.toString(response.getEntity());
+    JsonObject content = new JsonParser().parse(responseEntity).getAsJsonObject();
+    return content.get("id_token").getAsString();
+  }
+}
diff --git a/testing/pom.xml b/testing/pom.xml
index a0600508d..968391ca6 100644
--- a/testing/pom.xml
+++ b/testing/pom.xml
@@ -36,7 +36,8 @@
 		<module>partition-test-azure</module>
 		<module>partition-test-aws</module>
 		<module>partition-test-ibm</module>
-	</modules>
+    <module>partition-test-gcp</module>
+  </modules>
 
 	<repositories>
 		<repository>
-- 
GitLab