diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4ee7216164f3114072544bbeb7676927ce593bc6..4ac98878f699fa77c7124d82c88d7513a7dac500 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -19,6 +19,8 @@ variables: IBM_INT_TEST_SUBDIR: testing/partition-test-ibm IBM_HELM_CONFIG_PATH: devops/ibm/ibm-partition-config IBM_HELM_DEPLOY_PATH: devops/ibm/ibm-partition-deploy + ACCEPTANCE_TEST_DIR: "partition-acceptance-test" + include: - project: "osdu/platform/ci-cd-pipelines" diff --git a/partition-acceptance-test/pom.xml b/partition-acceptance-test/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..53e4307afe0250a3831421faa52f35f58b14466c --- /dev/null +++ b/partition-acceptance-test/pom.xml @@ -0,0 +1,70 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 2017-2020, 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> + + <groupId>org.opengroup.osdu.partition</groupId> + <artifactId>partition-acceptance-test</artifactId> + <version>0.28.0-SNAPSHOT</version> + <packaging>jar</packaging> + + <properties> + <java.version>17</java.version> + <maven.compiler.target>17</maven.compiler.target> + <maven.compiler.source>17</maven.compiler.source> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <version>1.18.24</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.13.2</version> + </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.10.1</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + <version>5.2.7.RELEASE</version> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents.client5</groupId> + <artifactId>httpclient5</artifactId> + <version>5.2.1</version> + </dependency> + <dependency> + <groupId>com.nimbusds</groupId> + <artifactId>oauth2-oidc-sdk</artifactId> + <version>9.15</version> + </dependency> + </dependencies> +</project> diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/CreatePartitionTest.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/CreatePartitionTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3bf113825964dee7dbad419881892c1869a87481 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/CreatePartitionTest.java @@ -0,0 +1,113 @@ +// Copyright 2017-2020, 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. + +package org.opengroup.osdu.partition.api; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.junit.After; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; +import org.opengroup.osdu.partition.api.descriptor.CreatePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.DeletePartitionDescriptor; +import org.opengroup.osdu.partition.api.util.AuthorizationTestUtil; +import org.opengroup.osdu.partition.util.BaseTestTemplate; +import org.opengroup.osdu.partition.util.TestTokenUtils; +import org.springframework.http.HttpStatus; + +import static org.junit.Assert.assertEquals; +import static org.opengroup.osdu.partition.util.Constants.EXECUTE_AUTHORIZATION_DEPENDENT_TESTS; + +public final class CreatePartitionTest extends BaseTestTemplate { + + private String partitionId = getIntegrationTestPrefix() + System.currentTimeMillis(); + + @Override + @Before + public void setup() { + this.testUtils = new TestTokenUtils(); + this.authorizationTestUtil = new AuthorizationTestUtil(this.descriptor, this.testUtils); + } + + @Override + @After + public void tearDown() throws Exception { + deleteResource(); + this.testUtils = null; + this.authorizationTestUtil = null; + } + + @Override + protected String getId() { + return partitionId; + } + + @Override + protected void deleteResource() throws Exception { + DeletePartitionDescriptor deletePartitionDes = new DeletePartitionDescriptor(); + deletePartitionDes.setPartitionId(partitionId); + CloseableHttpResponse response = deletePartitionDes.run(this.getId(), this.testUtils.getAccessToken()); + } + + @Override + protected void createResource() throws Exception { + } + + public CreatePartitionTest() { + super(new CreatePartitionDescriptor()); + } + + @Override + protected int expectedOkResponseCode() { + return HttpStatus.CREATED.value(); + } + + @Test + public void should_return409_when_creatingSamePartitionTwice() throws Exception { + CloseableHttpResponse response = this.descriptor.run(this.getId(), testUtils.getAccessToken()); + assertEquals(this.error(""), this.expectedOkResponseCode(), response.getCode()); + + CloseableHttpResponse response2 = this.descriptor.run(this.getId(), testUtils.getAccessToken()); + assertEquals(this.error(""), HttpStatus.CONFLICT.value(), response2.getCode()); + deleteResource(); + } + + @Test + public void should_return40XResponseCode_when_makingRequest_withInvalidPayload() throws Exception { + String invalidPayload = ""; + CloseableHttpResponse response = descriptor.runWithCustomPayload(getId(), invalidPayload, testUtils.getAccessToken()); + assertEquals(400, response.getCode()); + } + + @Override + @Test + public void should_return401_when_noAccessToken() throws Exception { + Assume.assumeTrue(EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_noAccessToken(getId()); + } + + @Override + @Test + public void should_return401_when_accessingWithCredentialsWithoutPermission() throws Exception { + Assume.assumeTrue(EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_accessingWithCredentialsWithoutPermission(getId()); + } + + @Override + @Test + public void should_return401_when_makingHttpRequestWithoutToken() throws Exception { + Assume.assumeTrue(EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_makingHttpRequestWithoutToken(getId()); + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/DeletePartitionTest.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/DeletePartitionTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3ed57040380195e8d8b90542303938dca49f428f --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/DeletePartitionTest.java @@ -0,0 +1,123 @@ +// Copyright 2017-2020, 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. + +package org.opengroup.osdu.partition.api; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.*; +import org.opengroup.osdu.partition.api.descriptor.CreatePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.DeletePartitionDescriptor; +import org.opengroup.osdu.partition.api.util.AuthorizationTestUtil; +import org.opengroup.osdu.partition.util.BaseTestTemplate; +import org.opengroup.osdu.partition.util.Constants; +import org.opengroup.osdu.partition.util.RestDescriptor; +import org.opengroup.osdu.partition.util.TestTokenUtils; +import org.springframework.http.HttpStatus; + +import static org.opengroup.osdu.partition.util.Constants.EXECUTE_AUTHORIZATION_DEPENDENT_TESTS; + +public final class DeletePartitionTest extends BaseTestTemplate { + + private String partitionId; + + private static String integrationTestPrefix = getIntegrationTestPrefix(); + + public DeletePartitionTest() { + super(createDeleteDescriptor(integrationTestPrefix + System.currentTimeMillis())); + this.partitionId = ((DeletePartitionDescriptor) this.descriptor).getPartitionId(); + } + + @Before + @Override + public void setup() throws Exception { + this.testUtils = new TestTokenUtils(); + this.authorizationTestUtil = new AuthorizationTestUtil(descriptor, testUtils); + } + + @After + @Override + public void tearDown() throws Exception { + deleteResource(); + this.testUtils = null; + this.authorizationTestUtil = null; + } + + private static DeletePartitionDescriptor createDeleteDescriptor(String id) { + DeletePartitionDescriptor deletePartition = new DeletePartitionDescriptor(); + deletePartition.setPartitionId(id); + + return deletePartition; + } + + @Override + protected String getId() { + return partitionId; + } + + @Override + protected void deleteResource() throws Exception { + DeletePartitionDescriptor deletePartitionDes = new DeletePartitionDescriptor(); + deletePartitionDes.setPartitionId(partitionId); + CloseableHttpResponse response = deletePartitionDes.run(this.getId(), this.testUtils.getAccessToken()); + } + + protected void createResource() throws Exception { + CreatePartitionDescriptor createPartition = new CreatePartitionDescriptor(); + + createPartition.setPartitionId(partitionId); + + RestDescriptor oldDescriptor = this.descriptor; + + this.descriptor = createPartition; + + CloseableHttpResponse createResponse = this.descriptor.run(this.getId(), this.testUtils.getAccessToken()); + Assert.assertEquals(this.error(EntityUtils.toString(createResponse.getEntity())), HttpStatus.CREATED.value(), + createResponse.getCode()); + + this.descriptor = oldDescriptor; + } + + @Test + public void should_return404_when_deletingNonExistedPartition() throws Exception { + CloseableHttpResponse response1 = this.descriptor.run(this.getId(), this.testUtils.getAccessToken()); + Assert.assertEquals(this.error(""), HttpStatus.NOT_FOUND.value(), response1.getCode()); + } + + @Override + @Test + public void should_return401_when_noAccessToken() throws Exception { + Assume.assumeTrue(EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_noAccessToken(getId()); + } + + @Override + @Test + public void should_return401_when_accessingWithCredentialsWithoutPermission() throws Exception { + Assume.assumeTrue(EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_accessingWithCredentialsWithoutPermission(getId()); + } + + @Override + @Test + public void should_return401_when_makingHttpRequestWithoutToken() throws Exception { + Assume.assumeTrue(EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_makingHttpRequestWithoutToken(getId()); + } + + @Override + protected int expectedOkResponseCode() { + return HttpStatus.NO_CONTENT.value(); + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/GetPartitionByIdApiTest.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/GetPartitionByIdApiTest.java new file mode 100644 index 0000000000000000000000000000000000000000..141d874258b9c81ccb71c139d7df8e5940f8a332 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/GetPartitionByIdApiTest.java @@ -0,0 +1,105 @@ +// Copyright 2017-2020, 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. + +package org.opengroup.osdu.partition.api; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.After; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; +import org.opengroup.osdu.partition.api.descriptor.CreatePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.DeletePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.GetPartitionDescriptor; +import org.opengroup.osdu.partition.api.util.AuthorizationTestUtil; +import org.opengroup.osdu.partition.util.BaseTestTemplate; +import org.opengroup.osdu.partition.util.Constants; +import org.opengroup.osdu.partition.util.TestTokenUtils; +import org.springframework.http.HttpStatus; + +import static org.junit.Assert.assertEquals; + +public final class GetPartitionByIdApiTest extends BaseTestTemplate { + + private String partitionId = getIntegrationTestPrefix() + System.currentTimeMillis(); + + @Override + @Before + public void setup() throws Exception { + this.testUtils = new TestTokenUtils(); + this.authorizationTestUtil = new AuthorizationTestUtil(this.descriptor, this.testUtils); + } + + @Override + @After + public void tearDown() throws Exception { + deleteResource(); + this.testUtils = null; + this.authorizationTestUtil = null; + } + + @Override + protected String getId() { + return partitionId; + } + + @Override + protected void deleteResource() throws Exception { + DeletePartitionDescriptor deletePartitionDes = new DeletePartitionDescriptor(); + deletePartitionDes.setPartitionId(partitionId); + CloseableHttpResponse response = deletePartitionDes.run(this.getId(), this.testUtils.getAccessToken()); + } + + @Override + protected void createResource() throws Exception { + CreatePartitionDescriptor createPartitionDescriptor = new CreatePartitionDescriptor(); + createPartitionDescriptor.setPartitionId(partitionId); + + CloseableHttpResponse createResponse = createPartitionDescriptor.run(this.getId(), this.testUtils.getAccessToken()); + assertEquals(this.error(EntityUtils.toString(createResponse.getEntity())), HttpStatus.CREATED.value(), + createResponse.getCode()); + } + + public GetPartitionByIdApiTest() { + super(new GetPartitionDescriptor()); + } + + @Override + protected int expectedOkResponseCode() { + return HttpStatus.OK.value(); + } + + @Override + @Test + public void should_return401_when_noAccessToken() throws Exception { + Assume.assumeTrue(Constants.EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_noAccessToken(getId()); + } + + @Override + @Test + public void should_return401_when_accessingWithCredentialsWithoutPermission() throws Exception { + Assume.assumeTrue(Constants.EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_accessingWithCredentialsWithoutPermission(getId()); + } + + @Override + @Test + public void should_return401_when_makingHttpRequestWithoutToken() throws Exception { + Assume.assumeTrue(Constants.EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_makingHttpRequestWithoutToken(getId()); + } + +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/HealthCheckApiTest.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/HealthCheckApiTest.java new file mode 100644 index 0000000000000000000000000000000000000000..df6c481cfedd407a8190c783aa531a71d62c9253 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/HealthCheckApiTest.java @@ -0,0 +1,61 @@ +/* + Copyright 2002-2023 Google LLC + Copyright 2002-2023 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.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.opengroup.osdu.partition.util.TestBase; +import org.opengroup.osdu.partition.util.TestTokenUtils; +import org.opengroup.osdu.partition.util.TestUtils; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; + +import static org.junit.Assert.assertEquals; + +public final class HealthCheckApiTest extends TestBase { + + @Override + @Before + public void setup() throws Exception { + this.testUtils = new TestTokenUtils(); + } + + @Override + @After + public void tearDown() throws Exception { + this.testUtils = new TestTokenUtils(); + } + + @Test + public void should_returnOk() throws Exception { + CloseableHttpResponse response = + TestUtils.send("api/partition/v1/liveness_check", HttpMethod.GET.name(), null, "", "", false); + assertEquals(HttpStatus.OK.value(), response.getCode()); + } + + @Test + public void should_returnTrailingOk() throws Exception { + CloseableHttpResponse response = + TestUtils.send("api/partition/v1/liveness_check/", HttpMethod.GET.name(), this.testUtils.getAccessToken(), "", "", false); + assertEquals(HttpStatus.OK.value(), response.getCode()); + } + + +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/ListPartitionsApiTest.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/ListPartitionsApiTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6c5a58f4695fa1cc58ae8b9340cac580a1599465 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/ListPartitionsApiTest.java @@ -0,0 +1,104 @@ +// Copyright 2017-2020, 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. + +package org.opengroup.osdu.partition.api; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.After; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; +import org.opengroup.osdu.partition.api.descriptor.CreatePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.DeletePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.ListPartitionDescriptor; +import org.opengroup.osdu.partition.api.util.AuthorizationTestUtil; +import org.opengroup.osdu.partition.util.BaseTestTemplate; +import org.opengroup.osdu.partition.util.Constants; +import org.opengroup.osdu.partition.util.TestTokenUtils; +import org.springframework.http.HttpStatus; + +import static org.junit.Assert.assertEquals; + +public final class ListPartitionsApiTest extends BaseTestTemplate { + + private String partitionId = getIntegrationTestPrefix() + System.currentTimeMillis(); + + @Override + @Before + public void setup() { + this.testUtils = new TestTokenUtils(); + this.authorizationTestUtil = new AuthorizationTestUtil(this.descriptor, this.testUtils); + } + + @Override + @After + public void tearDown() throws Exception { + deleteResource(); + this.testUtils = null; + this.authorizationTestUtil = null; + } + + @Override + protected String getId() { + return partitionId; + } + + @Override + protected void deleteResource() throws Exception { + DeletePartitionDescriptor deletePartitionDes = new DeletePartitionDescriptor(); + deletePartitionDes.setPartitionId(partitionId); + CloseableHttpResponse response = deletePartitionDes.run(this.getId(), this.testUtils.getAccessToken()); + } + + @Override + protected void createResource() throws Exception { + CreatePartitionDescriptor createPartitionDescriptor = new CreatePartitionDescriptor(); + createPartitionDescriptor.setPartitionId(partitionId); + + CloseableHttpResponse createResponse = createPartitionDescriptor.run(this.getId(), this.testUtils.getAccessToken()); + assertEquals(this.error(EntityUtils.toString(createResponse.getEntity())), HttpStatus.CREATED.value(), + createResponse.getCode()); + } + + public ListPartitionsApiTest() { + super(new ListPartitionDescriptor()); + } + + @Override + protected int expectedOkResponseCode() { + return HttpStatus.OK.value(); + } + + @Override + @Test + public void should_return401_when_noAccessToken() throws Exception { + Assume.assumeTrue(Constants.EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_noAccessToken(getId()); + } + + @Override + @Test + public void should_return401_when_accessingWithCredentialsWithoutPermission() throws Exception { + Assume.assumeTrue(Constants.EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_accessingWithCredentialsWithoutPermission(getId()); + } + + @Override + @Test + public void should_return401_when_makingHttpRequestWithoutToken() throws Exception { + Assume.assumeTrue(Constants.EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_makingHttpRequestWithoutToken(getId()); + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/SwaggerApiTest.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/SwaggerApiTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f22a1c48bf749d04ddce21637c323f1eeec9148b --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/SwaggerApiTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2021 Google LLC + * Copyright 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 + * + * https://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.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.opengroup.osdu.partition.util.TestBase; +import org.opengroup.osdu.partition.util.TestUtils; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; + +import static org.junit.Assert.assertEquals; + +public final class SwaggerApiTest extends TestBase { + + private static final String CONTEXT_PATH = "api/partition/v1/"; + private static final String SWAGGER_API_PATH = "swagger"; + private static final String SWAGGER_API_DOCS_PATH = "api-docs"; + + @Override + @Before + public void setup() throws Exception { + } + + @Override + @After + public void tearDown() throws Exception { + } + + @Test + public void shouldReturn200_whenSwaggerApiIsCalled() throws Exception { + CloseableHttpResponse response = TestUtils.send(CONTEXT_PATH + SWAGGER_API_PATH, HttpMethod.GET.name(),false); + assertEquals(HttpStatus.OK.value(), response.getCode()); + } + + @Test + public void shouldReturn200_whenSwaggerApiDocsIsCalled() throws Exception { + CloseableHttpResponse response = TestUtils.send(CONTEXT_PATH + SWAGGER_API_DOCS_PATH, HttpMethod.GET.name(),false); + assertEquals(HttpStatus.OK.value(), response.getCode()); + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/UpdatePartitionTest.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/UpdatePartitionTest.java new file mode 100644 index 0000000000000000000000000000000000000000..578728a417fab1d8af5445154529429a98c3e7b7 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/UpdatePartitionTest.java @@ -0,0 +1,151 @@ +package org.opengroup.osdu.partition.api; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.*; +import org.opengroup.osdu.partition.api.descriptor.CreatePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.DeletePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.UpdatePartitionDescriptor; +import org.opengroup.osdu.partition.api.util.AuthorizationTestUtil; +import org.opengroup.osdu.partition.util.BaseTestTemplate; +import org.opengroup.osdu.partition.util.Constants; +import org.opengroup.osdu.partition.util.TestTokenUtils; +import org.springframework.http.HttpStatus; + +import java.lang.constant.Constable; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.opengroup.osdu.partition.util.Constants.EXECUTE_AUTHORIZATION_DEPENDENT_TESTS; + +public final class UpdatePartitionTest extends BaseTestTemplate { + + String partitionId = getIntegrationTestPrefix() + System.currentTimeMillis(); + private String nonExistentPartitionId = "nonexistent-partition"+System.currentTimeMillis(); + + @Override + @Before + public void setup() { + this.testUtils = new TestTokenUtils(); + this.authorizationTestUtil = new AuthorizationTestUtil(this.descriptor, this.testUtils); + } + + @Override + @After + public void tearDown() throws Exception { + deleteResource(); + this.testUtils = null; + this.authorizationTestUtil = null; + } + + public UpdatePartitionTest() { + super(new UpdatePartitionDescriptor()); + } + + @Override + protected String getId() { + return partitionId; + } + + @Override + protected void deleteResource() throws Exception { + DeletePartitionDescriptor deletePartitionDes = new DeletePartitionDescriptor(); + deletePartitionDes.setPartitionId(partitionId); + CloseableHttpResponse response = deletePartitionDes.run(this.getId(), this.testUtils.getAccessToken()); + } + + @Override + protected void createResource() throws Exception { + CreatePartitionDescriptor createPartitionDescriptor = new CreatePartitionDescriptor(); + createPartitionDescriptor.setPartitionId(partitionId); + + CloseableHttpResponse createResponse = createPartitionDescriptor.run(this.getId(), this.testUtils.getAccessToken()); + assertEquals(this.error(EntityUtils.toString(createResponse.getEntity())), HttpStatus.CREATED.value(), + createResponse.getCode()); + } + + @Override + protected int expectedOkResponseCode() { + return HttpStatus.CREATED.value(); + } + + @Test + public void should_return404_when_updatingNonExistentPartition() throws Exception { + CloseableHttpResponse response = this.descriptor.run(nonExistentPartitionId, this.testUtils.getAccessToken()); + assertEquals(this.error(""), HttpStatus.NOT_FOUND.value(), response.getCode()); + } + + @Test + public void should_return400_when_updatingPartitionWithIdField() throws Exception { + createResource(); + CloseableHttpResponse response = this.descriptor.runWithCustomPayload(this.getId(), getInvalidBodyForUpdatePartition(), this.testUtils.getAccessToken()); + assertEquals(this.error(""), HttpStatus.BAD_REQUEST.value(), response.getCode()); + deleteResource(); + } + + @Test + @Override + public void should_return20XResponseCode_when_makingValidHttpsRequest() throws Exception { + createResource(); + CloseableHttpResponse response = this.descriptor.runWithCustomPayload(this.getId(), getValidBodyForUpdatePartition(), this.testUtils.getAccessToken()); + deleteResource(); + assertEquals(HttpStatus.NO_CONTENT.value(), response.getCode()); + assertEquals("GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH", response.getHeader("Access-Control-Allow-Methods").getValue()); + assertEquals("access-control-allow-origin, origin, content-type, accept, authorization, data-partition-id, correlation-id, appkey", response.getHeader("Access-Control-Allow-Headers").getValue()); + assertEquals("*", response.getHeader("Access-Control-Allow-Origin").getValue()); + assertEquals("true", response.getHeader("Access-Control-Allow-Credentials").getValue()); + assertEquals("default-src 'self'", response.getHeader("Content-Security-Policy").getValue()); + assertEquals("max-age=31536000; includeSubDomains", response.getHeader("Strict-Transport-Security").getValue()); + assertEquals("0", response.getHeader("Expires").getValue()); + assertEquals("DENY", response.getHeader("X-Frame-Options").getValue()); + assertEquals("private, max-age=300", response.getHeader("Cache-Control").getValue()); + assertEquals("1; mode=block", response.getHeader("X-XSS-Protection").getValue()); + assertEquals("nosniff", response.getHeader("X-Content-Type-Options").getValue()); + } + + private String getInvalidBodyForUpdatePartition() { + StringBuffer sb = new StringBuffer(); + sb.append("{\n"); + sb.append(" \"properties\": {") + .append("\"elasticPassword\": {\"sensitive\":true,\"value\":\"test-password\"},") + .append("\"serviceBusConnection\": {\"sensitive\":true,\"value\":\"test-service-bus-connection\"},") + .append("\"complianceRuleSet\": {\"value\":\"shared\"},") + .append("\"id\": {\"value\":\"test-id\"}") + .append("}\n") + .append("}"); + return sb.toString(); + } + + protected String getValidBodyForUpdatePartition() { + StringBuffer sb = new StringBuffer(); + sb.append("{\n"); + sb.append(" \"properties\": {") + .append("\"updateElasticPassword\": {\"sensitive\":true,\"value\":\"test-password\"},") + .append("\"serviceBusConnection\": {\"sensitive\":true,\"value\":\"test-service-bus-connection-update\"},") + .append("\"complianceRuleSet\": {\"value\":\"shared\"}") + .append("}\n") + .append("}"); + return sb.toString(); + } + + @Override + @Test + public void should_return401_when_noAccessToken() throws Exception { + Assume.assumeTrue(EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_noAccessToken(getId()); + } + + @Override + @Test + public void should_return401_when_accessingWithCredentialsWithoutPermission() throws Exception { + Assume.assumeTrue(EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_accessingWithCredentialsWithoutPermission(getId()); + } + + @Override + @Test + public void should_return401_when_makingHttpRequestWithoutToken() throws Exception { + Assume.assumeTrue(EXECUTE_AUTHORIZATION_DEPENDENT_TESTS); + authorizationTestUtil.should_return401or403_when_makingHttpRequestWithoutToken(getId()); + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/CreatePartitionDescriptor.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/CreatePartitionDescriptor.java new file mode 100644 index 0000000000000000000000000000000000000000..fee0ffb55efd13fff7b7ee4fb0c12507f20f9804 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/CreatePartitionDescriptor.java @@ -0,0 +1,54 @@ +// Copyright 2017-2020, 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. + +package org.opengroup.osdu.partition.api.descriptor; + +import org.opengroup.osdu.partition.util.RestDescriptor; +import org.springframework.web.bind.annotation.RequestMethod; + +public class CreatePartitionDescriptor extends RestDescriptor { + + private String partitionId; + + @Override + public String getPath() { + return "api/partition/v1/partitions/" + this.arg(); + } + + @Override + public String getHttpMethod() { + return RequestMethod.POST.toString(); + } + + @Override + public String getValidBody() { + StringBuffer sb = new StringBuffer(); + sb.append("{\n"); + sb.append(" \"properties\": {") + .append("\"elasticPassword\": {\"sensitive\":true,\"value\":\"test-password\"},") + .append("\"serviceBusConnection\": {\"sensitive\":true,\"value\":\"test-service-bus-connection\"},") + .append("\"complianceRuleSet\": {\"value\":\"shared\"}") + .append("}\n") + .append("}"); + return sb.toString(); + } + + public String getPartitionId() { + return partitionId; + } + + public void setPartitionId(String partitionId) { + this.partitionId = partitionId; + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/DeletePartitionDescriptor.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/DeletePartitionDescriptor.java new file mode 100644 index 0000000000000000000000000000000000000000..80cd6e904232d26c7e38052ed89017cb08e02b2e --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/DeletePartitionDescriptor.java @@ -0,0 +1,47 @@ +// Copyright 2017-2020, 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. + +package org.opengroup.osdu.partition.api.descriptor; + +import org.opengroup.osdu.partition.util.RestDescriptor; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.RequestMethod; + +public class DeletePartitionDescriptor extends RestDescriptor implements PartitionServiceDescriptor { + + private String partitionId; + + @Override + public String getPath() { + return PartitionServiceDescriptor.getServiceUrl() + "partitions/" + getPartitionId(); + } + + @Override + public String getHttpMethod() { + return RequestMethod.DELETE.toString(); + } + + @Override + public String getValidBody() { + return ""; + } + + public String getPartitionId() { + return partitionId; + } + + public void setPartitionId(String partitionId) { + this.partitionId = partitionId; + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/GetPartitionDescriptor.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/GetPartitionDescriptor.java new file mode 100644 index 0000000000000000000000000000000000000000..f1e5a2646f6f7ee8b3c6a68536dfeef8feb947b4 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/GetPartitionDescriptor.java @@ -0,0 +1,36 @@ +// Copyright 2017-2020, 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. + +package org.opengroup.osdu.partition.api.descriptor; + +import org.opengroup.osdu.partition.util.RestDescriptor; +import org.springframework.web.bind.annotation.RequestMethod; + +public class GetPartitionDescriptor extends RestDescriptor { + + @Override + public String getPath() { + return "api/partition/v1/partitions/" + arg(); + } + + @Override + public String getHttpMethod() { + return RequestMethod.GET.toString(); + } + + @Override + public String getValidBody() { + return ""; + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/ListPartitionDescriptor.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/ListPartitionDescriptor.java new file mode 100644 index 0000000000000000000000000000000000000000..29838f60bb538b888f4ed8816d6d37838c0b7ce4 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/ListPartitionDescriptor.java @@ -0,0 +1,36 @@ +// Copyright 2017-2020, 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. + +package org.opengroup.osdu.partition.api.descriptor; + +import org.opengroup.osdu.partition.util.RestDescriptor; +import org.springframework.web.bind.annotation.RequestMethod; + +public class ListPartitionDescriptor extends RestDescriptor { + + @Override + public String getPath() { + return "api/partition/v1/partitions"; + } + + @Override + public String getHttpMethod() { + return RequestMethod.GET.toString(); + } + + @Override + public String getValidBody() { + return ""; + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/PartitionServiceDescriptor.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/PartitionServiceDescriptor.java new file mode 100644 index 0000000000000000000000000000000000000000..81d3a9aef5ebbedb155d36631046ac401b90a4fd --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/PartitionServiceDescriptor.java @@ -0,0 +1,22 @@ +// Copyright 2017-2020, 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. + +package org.opengroup.osdu.partition.api.descriptor; + +public interface PartitionServiceDescriptor { + + static String getServiceUrl() { + return "api/partition/v1/"; + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/UpdatePartitionDescriptor.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/UpdatePartitionDescriptor.java new file mode 100644 index 0000000000000000000000000000000000000000..3f3861710ad189ed035e313ad2bd3b16baf4e9e9 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/descriptor/UpdatePartitionDescriptor.java @@ -0,0 +1,31 @@ +package org.opengroup.osdu.partition.api.descriptor; + +import org.opengroup.osdu.partition.util.RestDescriptor; +import org.springframework.web.bind.annotation.RequestMethod; + +public class UpdatePartitionDescriptor extends RestDescriptor { + + + @Override + public String getPath() { + return "api/partition/v1/partitions/" + this.arg(); + } + + @Override + public String getHttpMethod() { + return RequestMethod.PATCH.toString(); + } + + @Override + public String getValidBody() { + StringBuffer sb = new StringBuffer(); + sb.append("{\n"); + sb.append(" \"properties\": {") + .append("\"elasticPassword\": {\"sensitive\":true,\"value\":\"test-password\"},") + .append("\"serviceBusConnection\": {\"sensitive\":true,\"value\":\"test-service-bus-connection\"},") + .append("\"complianceRuleSet\": {\"value\":\"shared\"}") + .append("}\n") + .append("}"); + return sb.toString(); + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/util/AuthorizationTestUtil.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/util/AuthorizationTestUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..5b13cfb5a3f5b3ee4a098c98eff18224b406c402 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/api/util/AuthorizationTestUtil.java @@ -0,0 +1,70 @@ +/* + * Copyright 2020-2022 Google LLC + * Copyright 2020-2022 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 + * + * https://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.util; + +import lombok.extern.slf4j.Slf4j; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.opengroup.osdu.partition.util.RestDescriptor; +import org.opengroup.osdu.partition.util.TestUtils; + +@Slf4j +public class AuthorizationTestUtil { + + private RestDescriptor descriptor; + private TestUtils testUtils; + + public AuthorizationTestUtil(RestDescriptor descriptor, TestUtils testUtils) { + this.descriptor = descriptor; + this.testUtils = testUtils; + } + + // Test depends on an infrastructure level. + public void should_return401or403_when_noAccessToken(String partitionId) throws Exception { + CloseableHttpResponse response = descriptor.runOnCustomerTenant(partitionId, testUtils.getNoAccessToken()); + log.info( + "Test should_return401or403_when_noAccessToken has a response code = {}." + + "This test depends on an infrastructure level.", + response.getCode()); + } + + // Test depends on an infrastructure level. + public void should_return401or403_when_accessingWithCredentialsWithoutPermission( + String partitionId) throws Exception { + CloseableHttpResponse response = descriptor.run(partitionId, testUtils.getNoAccessToken()); + log.info( + "Test should_return401or403_when_accessingWithCredentialsWithoutPermission has a response code = {}." + + "This test depends on an infrastructure level.", + response.getCode()); + } + + // Test depends on an infrastructure level. + public void should_return401or403_when_makingHttpRequestWithoutToken(String partitionId) + throws Exception { + CloseableHttpResponse response = descriptor.run(partitionId, ""); + log.info( + "Test should_return401or403_when_makingHttpRequestWithoutToken has a response code = {}." + + "This test depends on an infrastructure level.", + response.getCode()); + } + + protected String error(String body) { + return String.format( + "%s: %s %s %s", + descriptor.getHttpMethod(), descriptor.getPath(), descriptor.getQuery(), body); + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/BaseTestTemplate.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/BaseTestTemplate.java new file mode 100644 index 0000000000000000000000000000000000000000..7941a2d40f4def9ff4d6b9cbd696681e3ac94d8d --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/BaseTestTemplate.java @@ -0,0 +1,102 @@ +/* + * Copyright 2017-2020, 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. + */ + +package org.opengroup.osdu.partition.util; + +import com.sun.jersey.api.client.ClientResponse; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.Test; +import org.opengroup.osdu.partition.api.util.AuthorizationTestUtil; + +import static org.junit.Assert.assertEquals; + +public abstract class BaseTestTemplate extends TestBase { + + protected RestDescriptor descriptor; + protected AuthorizationTestUtil authorizationTestUtil; + + public BaseTestTemplate(RestDescriptor descriptor) { + this.descriptor = descriptor; + } + + protected abstract String getId(); + + protected abstract void deleteResource() throws Exception; + + protected abstract void createResource() throws Exception; + + protected abstract int expectedOkResponseCode(); + + protected String error(String body) { + return String.format("%s: %s %s %s", descriptor.getHttpMethod(), descriptor.getPath(), descriptor.getQuery(), body); + } + + @Test + public void should_return401_when_noAccessToken() throws Exception { + CloseableHttpResponse response = descriptor.runOnCustomerTenant(getId(), testUtils.getNoAccessToken()); + assertEquals(error(EntityUtils.toString(response.getEntity())), 401, response.getCode()); + } + + @Test + public void should_return401_when_accessingWithCredentialsWithoutPermission() throws Exception { + CloseableHttpResponse response = descriptor.run(getId(), testUtils.getNoAccessToken()); + assertEquals(error(EntityUtils.toString(response.getEntity())), 401, response.getCode()); + } + + @Test + public void should_return20XResponseCode_when_makingValidHttpsRequest() throws Exception { + should_return20X_when_usingCredentialsWithPermission(testUtils.getAccessToken()); + } + + public void should_return20X_when_usingCredentialsWithPermission(String token) throws Exception { + createResource(); + CloseableHttpResponse response = descriptor.run(getId(), token); + deleteResource(); + assertEquals(error(response.getCode() == 204 ? "" : EntityUtils.toString(response.getEntity())), expectedOkResponseCode(), response.getCode()); + assertEquals("GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH", response.getHeader("Access-Control-Allow-Methods").getValue()); + assertEquals("access-control-allow-origin, origin, content-type, accept, authorization, data-partition-id, correlation-id, appkey", response.getHeader("Access-Control-Allow-Headers").getValue()); + assertEquals("*", response.getHeader("Access-Control-Allow-Origin").getValue()); + assertEquals("true", response.getHeader("Access-Control-Allow-Credentials").getValue()); + assertEquals("default-src 'self'", response.getHeader("Content-Security-Policy").getValue()); + assertEquals("max-age=31536000; includeSubDomains", response.getHeader("Strict-Transport-Security").getValue()); + assertEquals("0", response.getHeader("Expires").getValue()); + assertEquals("DENY", response.getHeader("X-Frame-Options").getValue()); + assertEquals("private, max-age=300", response.getHeader("Cache-Control").getValue()); + assertEquals("1; mode=block", response.getHeader("X-XSS-Protection").getValue()); + assertEquals("nosniff", response.getHeader("X-Content-Type-Options").getValue()); + } + + @Test + public void should_returnOk_when_makingHttpOptionsRequest() throws Exception { + createResource(); + CloseableHttpResponse response = descriptor.runOptions(getId(), testUtils.getAccessToken()); + assertEquals(error(EntityUtils.toString(response.getEntity())), 200, response.getCode()); + deleteResource(); + } + + @Test + public void should_return401_when_makingHttpRequestWithoutToken() throws Exception { + CloseableHttpResponse response = descriptor.run(getId(), ""); + assertEquals(error(EntityUtils.toString(response.getEntity())), 401, response.getCode()); + } + + @Test + public void should_return400_when_makingHttpRequestWithoutValidUrl() throws Exception { + CloseableHttpResponse response = descriptor.runWithInvalidPath(getId(), testUtils.getAccessToken()); + assertEquals(error(EntityUtils.toString(response.getEntity())), 400, response.getCode()); + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/Config.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/Config.java new file mode 100644 index 0000000000000000000000000000000000000000..8ac714d15188f1c90aaa6c2c8ce2ebf65a8be1d7 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/Config.java @@ -0,0 +1,37 @@ +/* + * Copyright 2017-2020, 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. + */ + +package org.opengroup.osdu.partition.util; + +public class Config { + + public String hostUrl; + public String osduTenant; + public String clientTenant; + public boolean executeAuthorizationDependentTests; + + private static Config config = new Config(); + + public static Config Instance() { + + //Note: PARTITION_BASE_URL has a '/' at the end of it + config.hostUrl = System.getProperty("PARTITION_BASE_URL", System.getenv("PARTITION_BASE_URL")); + config.clientTenant = System.getProperty("CLIENT_TENANT", System.getenv("CLIENT_TENANT")); + config.osduTenant = System.getProperty("MY_TENANT", System.getenv("MY_TENANT")); + config.executeAuthorizationDependentTests = Boolean.parseBoolean(System.getProperty("EXECUTE_AUTHORIZATION_DEPENDENT_TESTS", System.getenv("EXECUTE_AUTHORIZATION_DEPENDENT_TESTS"))); + return config; + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/Constants.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/Constants.java new file mode 100644 index 0000000000000000000000000000000000000000..a4571be51ec999cca10732ff5375eb8e732667af --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/Constants.java @@ -0,0 +1,6 @@ +package org.opengroup.osdu.partition.util; + +public class Constants { + public static final boolean EXECUTE_AUTHORIZATION_DEPENDENT_TESTS = Boolean.parseBoolean(System.getProperty("EXECUTE_AUTHORIZATION_DEPENDENT_TESTS", + System.getenv("EXECUTE_AUTHORIZATION_DEPENDENT_TESTS"))); +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/CustomHttpClientResponseHandler.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/CustomHttpClientResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..348facf9b32217cc5b6ea5c049d6e80e663cb7d3 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/CustomHttpClientResponseHandler.java @@ -0,0 +1,34 @@ +package org.opengroup.osdu.partition.util; + +import lombok.extern.slf4j.Slf4j; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.StringEntity; + +import java.io.IOException; + +@Slf4j +public class CustomHttpClientResponseHandler implements HttpClientResponseHandler<CloseableHttpResponse> { + + @Override + public CloseableHttpResponse handleResponse(ClassicHttpResponse classicHttpResponse) { + HttpEntity entity = classicHttpResponse.getEntity(); + if(classicHttpResponse.getCode() != HttpStatus.SC_NO_CONTENT) { + String body = ""; + try { + body = EntityUtils.toString(entity); + } catch (IOException | ParseException e) { + log.error("unable to parse response"); + } + HttpEntity newEntity = new StringEntity(body, ContentType.parse(entity.getContentType())); + classicHttpResponse.setEntity(newEntity); + } + return (CloseableHttpResponse) classicHttpResponse; + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/RestDescriptor.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/RestDescriptor.java new file mode 100644 index 0000000000000000000000000000000000000000..50d79ec84161a2f5e8afe6d9726a62337b2fe711 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/RestDescriptor.java @@ -0,0 +1,61 @@ +/* + * Copyright 2017-2020, 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. + */ + +package org.opengroup.osdu.partition.util; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; + +public abstract class RestDescriptor { + + public RestDescriptor() { + } + + private String arg = ""; + public String arg(){ + return arg; + } + public abstract String getPath(); + public abstract String getHttpMethod(); + public abstract String getValidBody(); + public String getQuery() { return ""; } + + public CloseableHttpResponse runHttp(String arg, String token) throws Exception{ + this.arg = arg; + return TestUtils.send(getPath(), getHttpMethod(), token, getValidBody(), getQuery(), true); + } + public CloseableHttpResponse run(String arg, String token) throws Exception{ + this.arg = arg; + return TestUtils.send(getPath(), getHttpMethod(), token, getValidBody(), getQuery(), false); + } + public CloseableHttpResponse runOnCustomerTenant(String arg, String token) throws Exception{ + this.arg = arg; + return TestUtils.send(getPath(), getHttpMethod(), token, getValidBody(), getQuery(), TestUtils.getCustomerTenantHeaders(), false); + } + public CloseableHttpResponse runOptions(String arg, String token) throws Exception{ + this.arg = arg; + return TestUtils.send(getPath(), "OPTIONS", token, "", "", false); + } + + public CloseableHttpResponse runWithCustomPayload(String arg, String body, String token) throws Exception { + this.arg = arg; + return TestUtils.send(getPath(), getHttpMethod(), token, body, getQuery(), false); + } + + public CloseableHttpResponse runWithInvalidPath(String arg, String token) throws Exception { + this.arg = arg; + return TestUtils.send(getPath() + "//", getHttpMethod(), token, getValidBody(), getQuery(), false); + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/TestBase.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/TestBase.java new file mode 100644 index 0000000000000000000000000000000000000000..a6d2b4b75202408818c85a58cc64d13e0ddaf1dc --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/TestBase.java @@ -0,0 +1,30 @@ +/* + * Copyright 2017-2020, 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. + */ + +package org.opengroup.osdu.partition.util; + +public abstract class TestBase { + + protected TestUtils testUtils = null; + + public static String getIntegrationTestPrefix() { + return "integrationtest-"; + } + + public abstract void setup() throws Exception; + + public abstract void tearDown() throws Exception; +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/TestTokenUtils.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/TestTokenUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..72f42c17d684536a6487fdcb1c5ee5b2bf113d6f --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/TestTokenUtils.java @@ -0,0 +1,51 @@ +/* + * Copyright 2020-2022 Google LLC + * Copyright 2020-2022 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 + * + * https://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 org.opengroup.osdu.partition.util.conf.OpenIDTokenProvider; +import org.springframework.util.StringUtils; + +public class TestTokenUtils extends TestUtils { + + private OpenIDTokenProvider tokenProvider = null; + + public TestTokenUtils() { + token = System.getProperty("PRIVILEGED_USER_TOKEN", System.getenv("PRIVILEGED_USER_TOKEN")); + noAccessToken = System.getProperty("NO_ACCESS_USER_TOKEN", System.getenv("NO_ACCESS_USER_TOKEN")); + + if (StringUtils.isEmpty(token) || StringUtils.isEmpty(noAccessToken)) { + tokenProvider = new OpenIDTokenProvider(); + } + } + + @Override + public String getAccessToken() { + if (StringUtils.isEmpty(token)) { + token = tokenProvider.getToken(); + } + return "Bearer " + token; + } + + @Override + public String getNoAccessToken() { + if (StringUtils.isEmpty(noAccessToken)) { + noAccessToken = tokenProvider.getNoAccessToken(); + } + return "Bearer " + noAccessToken; + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/TestUtils.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/TestUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..3bad6430ec7cb2df76b7ceeb3a8714aaae0fa760 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/TestUtils.java @@ -0,0 +1,123 @@ +/* + * Copyright 2017-2020, 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. + */ + +package org.opengroup.osdu.partition.util; + +import org.apache.hc.client5.http.config.ConnectionConfig; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; + +import java.io.Console; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +public abstract class TestUtils { + + protected static String token = null; + protected static String noAccessToken = null; + + public static String getApiPath(String api, boolean enforceHttp) throws MalformedURLException { + String baseUrl = Config.Instance().hostUrl; + if(enforceHttp) + baseUrl = baseUrl.replaceFirst("https", "http"); + URL mergedURL = new URL(baseUrl + api); + return mergedURL.toString(); + } + + public abstract String getAccessToken() throws Exception; + + public abstract String getNoAccessToken() throws Exception; + + public static Map<String, String> getOsduTenantHeaders() { + Map<String, String> headers = new HashMap<>(); + headers.put("data-partition-id", Config.Instance().osduTenant); + return headers; + } + + public static Map<String, String> getCustomerTenantHeaders() { + Map<String, String> headers = new HashMap<>(); + headers.put("data-partition-id", Config.Instance().clientTenant); + return headers; + } + + public static CloseableHttpResponse send(String path, String httpMethod, String token, String requestBody, String query, boolean enforceHttp) + throws IOException { + + Map<String, String> headers = getOsduTenantHeaders(); + return send(path, httpMethod, token, requestBody, query, headers, enforceHttp); + } + + public static CloseableHttpResponse send(String path, String httpMethod, String token, String requestBody, String query, + Map<String, String> headers, boolean enforceHttp) + throws IOException { + + BasicHttpClientConnectionManager cm = createBasicHttpClientConnectionManager(); + ClassicHttpRequest httpRequest = createHttpRequest(path, httpMethod, token, requestBody, headers, enforceHttp); + + try (CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(cm).build()) { + return httpClient.execute(httpRequest, new CustomHttpClientResponseHandler()); + } + } + + /** + * Referenced for Test cases where [Token, body] not required. ex [Swagger API] + **/ + public static CloseableHttpResponse send(String path, String httpMethod, boolean enforceHttp) throws IOException { + + BasicHttpClientConnectionManager cm = createBasicHttpClientConnectionManager(); + ClassicHttpRequest httpRequest = createHttpRequest(path, httpMethod, enforceHttp); + + try (CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(cm).build()) { + return httpClient.execute(httpRequest, new CustomHttpClientResponseHandler()); + } + } + + private static ClassicHttpRequest createHttpRequest(String path, String httpMethod, String token, String requestBody, + Map<String, String> headers, boolean enforceHttp) throws MalformedURLException { + String url = getApiPath(path, enforceHttp); + ClassicRequestBuilder classicRequestBuilder = ClassicRequestBuilder.create(httpMethod) + .setUri(url) + .addHeader("Authorization", token) + .setEntity(requestBody, ContentType.APPLICATION_JSON); + headers.forEach(classicRequestBuilder::addHeader); + return classicRequestBuilder.build(); + } + + private static ClassicHttpRequest createHttpRequest(String path, String httpMethod, boolean enforceHttp) throws MalformedURLException { + String url = getApiPath(path, enforceHttp); + return ClassicRequestBuilder.create(httpMethod).setUri(url).build(); + } + + private static BasicHttpClientConnectionManager createBasicHttpClientConnectionManager() { + ConnectionConfig connConfig = ConnectionConfig.custom() + .setConnectTimeout(1500000, TimeUnit.MILLISECONDS) + .setSocketTimeout(1500000, TimeUnit.MILLISECONDS) + .build(); + BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager(); + cm.setConnectionConfig(connConfig); + return cm; + } + +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/conf/OpenIDProviderConfig.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/conf/OpenIDProviderConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..af81a082ceff519c857a4a0ea4d298d8689f0eb5 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/conf/OpenIDProviderConfig.java @@ -0,0 +1,80 @@ +/* + * Copyright 2020-2022 Google LLC + * Copyright 2020-2022 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 + * + * https://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.conf; + +import com.nimbusds.oauth2.sdk.http.HTTPRequest; +import com.nimbusds.oauth2.sdk.http.HTTPResponse; +import com.nimbusds.oauth2.sdk.id.Issuer; +import com.nimbusds.openid.connect.sdk.op.OIDCProviderConfigurationRequest; +import com.nimbusds.openid.connect.sdk.op.OIDCProviderMetadata; + +public class OpenIDProviderConfig { + + private String clientId; + private String clientSecret; + private String noAccessClientId; + private String noAccessClientSecret; + private String url; + private final String[] scopes = {"openid"}; + private static final OpenIDProviderConfig openIDProviderConfig = new OpenIDProviderConfig(); + private static OIDCProviderMetadata providerMetadata; + + public static OpenIDProviderConfig Instance() { + try { + openIDProviderConfig.clientId = System.getProperty("PRIVILEGED_USER_OPENID_PROVIDER_CLIENT_ID", System.getenv("PRIVILEGED_USER_OPENID_PROVIDER_CLIENT_ID")); + openIDProviderConfig.clientSecret = System.getProperty("PRIVILEGED_USER_OPENID_PROVIDER_CLIENT_SECRET", System.getenv("PRIVILEGED_USER_OPENID_PROVIDER_CLIENT_SECRET")); + openIDProviderConfig.noAccessClientId = + System.getProperty("NO_ACCESS_OPENID_PROVIDER_CLIENT_ID", System.getenv("NO_ACCESS_OPENID_PROVIDER_CLIENT_ID")); + openIDProviderConfig.noAccessClientSecret = + System.getProperty("NO_ACCESS_OPENID_PROVIDER_CLIENT_SECRET", System.getenv("NO_ACCESS_OPENID_PROVIDER_CLIENT_SECRET")); + openIDProviderConfig.url = System.getProperty("TEST_OPENID_PROVIDER_URL", System.getenv("TEST_OPENID_PROVIDER_URL")); + Issuer issuer = new Issuer(openIDProviderConfig.url); + OIDCProviderConfigurationRequest request = new OIDCProviderConfigurationRequest(issuer); + HTTPRequest httpRequest = request.toHTTPRequest(); + HTTPResponse httpResponse = httpRequest.send(); + providerMetadata = OIDCProviderMetadata.parse(httpResponse.getContentAsJSONObject()); + } catch (Exception e) { + throw new RuntimeException("Malformed token provider configuration", e); + } + return openIDProviderConfig; + } + + public String getClientId() { + return clientId; + } + + public String getClientSecret() { + return clientSecret; + } + + public String getNoAccessClientId() { + return noAccessClientId; + } + + public String getNoAccessClientSecret() { + return noAccessClientSecret; + } + + public String[] getScopes() { + return scopes; + } + + public OIDCProviderMetadata getProviderMetadata() { + return providerMetadata; + } +} diff --git a/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/conf/OpenIDTokenProvider.java b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/conf/OpenIDTokenProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..73c72549efa3479a5328eded982e54a4106a1271 --- /dev/null +++ b/partition-acceptance-test/src/test/java/org/opengroup/osdu/partition/util/conf/OpenIDTokenProvider.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020-2022 Google LLC + * Copyright 2020-2022 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 + * + * https://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.conf; + +import com.nimbusds.oauth2.sdk.*; +import com.nimbusds.oauth2.sdk.auth.ClientAuthentication; +import com.nimbusds.oauth2.sdk.auth.ClientSecretBasic; +import com.nimbusds.oauth2.sdk.auth.Secret; +import com.nimbusds.oauth2.sdk.id.ClientID; +import com.nimbusds.openid.connect.sdk.OIDCTokenResponseParser; +import net.minidev.json.JSONObject; + +import java.io.IOException; +import java.net.URI; +import java.util.Objects; + +public class OpenIDTokenProvider { + + private static final OpenIDProviderConfig openIDProviderConfig = OpenIDProviderConfig.Instance(); + private static final String ID_TOKEN = "id_token"; + private final AuthorizationGrant clientGrant = new ClientCredentialsGrant(); + private final URI tokenEndpointURI; + private final Scope scope; + private final ClientAuthentication clientAuthentication; + private final ClientAuthentication noAccessClientAuthentication; + + public OpenIDTokenProvider() { + this.tokenEndpointURI = openIDProviderConfig.getProviderMetadata().getTokenEndpointURI(); + this.scope = new Scope(openIDProviderConfig.getScopes()); + this.clientAuthentication = + new ClientSecretBasic( + new ClientID(openIDProviderConfig.getClientId()), + new Secret(openIDProviderConfig.getClientSecret()) + ); + this.noAccessClientAuthentication = + new ClientSecretBasic( + new ClientID(openIDProviderConfig.getNoAccessClientId()), + new Secret(openIDProviderConfig.getNoAccessClientSecret()) + ); + } + + public String getToken() { + try { + TokenRequest request = new TokenRequest(this.tokenEndpointURI, this.clientAuthentication, this.clientGrant, this.scope); + return requestToken(request); + } catch (ParseException | IOException e) { + throw new RuntimeException("Unable get credentials from INTEGRATION_TESTER variables", e); + } + } + + public String getNoAccessToken() { + try { + TokenRequest request = new TokenRequest(this.tokenEndpointURI, this.noAccessClientAuthentication, this.clientGrant, this.scope); + return requestToken(request); + } catch (ParseException | IOException e) { + throw new RuntimeException("Unable get credentials from INTEGRATION_TESTER variables", e); + } + } + + private String requestToken(TokenRequest tokenRequest) throws ParseException, IOException { + + TokenResponse parse = OIDCTokenResponseParser.parse(tokenRequest.toHTTPRequest().send()); + + if (!parse.indicatesSuccess()) { + throw new RuntimeException("Unable get credentials from INTEGRATION_TESTER variables"); + } + + JSONObject jsonObject = parse.toSuccessResponse().toJSONObject(); + String idTokenValue = jsonObject.getAsString(ID_TOKEN); + if (Objects.isNull(idTokenValue) || idTokenValue.isEmpty()) { + throw new RuntimeException("Unable get credentials from INTEGRATION_TESTER variables"); + } + return idTokenValue; + } +} diff --git a/pom.xml b/pom.xml index 47c650642854b9c79b9087b8b1565e886b7f3166..22998977df3964e70845e12ce0badf1c48c8b454 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,8 @@ <module>provider/partition-ibm</module> <module>provider/partition-gc</module> <module>partition-core-plus</module> + <module>testing</module> + <module>partition-acceptance-test</module> </modules> <properties>