diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f50b4761d8c052c8d7f06833aa44ca776cf3bdc1..30825df0ed200a3dadd94dc2ecca2dfbaaee0a0f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -19,7 +19,6 @@ variables: IBM_HELM_CONFIG_PATH: devops/ibm/ibm-notification-config IBM_HELM_DEPLOY_PATH: devops/ibm/ibm-notification-deploy - CORE_BUILD_SUBDIR: notification-core ACCEPTANCE_TEST_DIR: "notification-acceptance-test" include: diff --git a/notification-acceptance-test/docs/README.md b/notification-acceptance-test/docs/README.md index 92a192ff2ad5dca9d57a3b7fc4dee0958b19e7d5..efb3a21d64252ad2ddb1e98da0668cb0086f429c 100644 --- a/notification-acceptance-test/docs/README.md +++ b/notification-acceptance-test/docs/README.md @@ -2,18 +2,20 @@ You will need to have the following environment variables defined. -| name | value | description | sensitive? | source | -|---------------------------------|------------------------------------------------------------------------------------------------------------------|-------------------------------------------|------------|--------| -| `CLIENT_TENANT` | eg. `osdu` | OSDU tenant used for testing | no | - | -| `ENVIRONMENT` | eg. `dev` | Environment used for testing | no | - | -| `GROUP_ID` | eg. `group` | Group ID used for testing | no | - | -| `TOPIC_ID` | eg. `records-changed` | Topic ID | no | - | -| `HMAC_SECRET` | eg. `123456789` | HMAC Secret | no | - | -| `NOTIFICATION_BASE_URL` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/notification/v1/` | Endpoint of notification service | no | - | -| `REGISTER_BASE_URL` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/register/v1` | Endpoint of register service | no | - | -| `REGISTER_CUSTOM_PUSH_URL_HMAC` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/register/v1/test/challenge/hmac-integration-test` | Endpoint of register custom push URL HMAC | no | - | -| `LEGAL_HOST` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/legal/v1` | Endpoint of legal service | no | - | -| `STORAGE_HOST` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/storage/v2` | Endpoint of storage service | no | - | +| name | value | description | sensitive? | source | +|---------------------------------|------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|------------|--------| +| `CLIENT_TENANT` | eg. `osdu` | Client tenant used for testing | no | - | +| `OSDU_TENANT` | eg. `osdu` | OSDU tenant used for testing | no | - | +| `ENVIRONMENT` | eg. `dev` | Environment used for testing. 'Local' for running locally with services url's predefined as http://localhost , 'Dev' & 'Dev_Gke' is configurable environment | no | - | +| `GROUP_ID` | eg. `group` | Group ID used for testing. Group id, used in storage record ACL. Full group will be "data.default.viewers@{{data-partition-id}}.{{group_id}}" | no | - | +| `TOPIC_ID` | eg. `records-changed` | Topic ID | no | - | +| `HMAC_SECRET` | eg. `123456789` | String in hex , must match pattern ^[a-zA-Z0-9]{8,30}+$ & be in register variable SUBSCRIBER_SECRET | yes | - | +| `NOTIFICATION_BASE_URL` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/notification/v1/` | Endpoint of notification service | no | - | +| `REGISTER_BASE_URL` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/register/v1` | Endpoint of register service | no | - | +| `REGISTER_CUSTOM_PUSH_URL_HMAC` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/register/v1/test/challenge/hmac-integration-test` | Endpoint of register custom push URL HMAC | no | - | +| `LEGAL_HOST` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/legal/v1` | Endpoint of legal service | no | - | +| `STORAGE_HOST` | eg. `https://osdu.core-dev.gcp.gnrg-osdu.projects.epam.com/api/storage/v2` | Endpoint of storage service | no | - | +| `GSA_TEST_ENABLED` | eg. `true` or `false` | Enabler for GSA tests | no | - | Authentication can be provided as OIDC config: | name | value | description | sensitive? | source | @@ -37,9 +39,15 @@ Or tokens can be used directly from env variables: | `DE_OPS_USER_TOKEN` | `********` | DE_OPS_USER Token | yes | - | | `NO_ACCESS_USER_TOKEN` | `********` | NO_ACCESS_USER Token | yes | - | - +**Entitlements configuration for integration accounts** +| DE_OPS_TESTER | DE_ADMIN_TESTER | DE_EDITOR_TESTER | DE_NO_ACCESS_TESTER | +|-----------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------|---------------------------------------------------------------------|------------------------------------------| +| notification.pubsub<br/>service.entitlements.user<br/>users<br/>users.datalake.ops</br> | service.entitlements.user<br/>users<br/>users.datalake.admins</br>service.legal.admin | service.entitlements.user<br/>users<br/>users.datalake.editors</br> | service.entitlements.user<br/>users<br/> | +Above variables should be configured in the release pipeline to run integration tests. You should also replace them with proper values if you wish to run tests locally. + +### Commands to run tests Execute following command to build code and run all the integration tests: ```bash diff --git a/notification-acceptance-test/pom.xml b/notification-acceptance-test/pom.xml index 0399014e1a09e80f240199833cd652feb57f3ac1..bd3ee4ff18ac245e10da466da35e0fdea4903072 100644 --- a/notification-acceptance-test/pom.xml +++ b/notification-acceptance-test/pom.xml @@ -38,11 +38,6 @@ </properties> <dependencies> - <dependency> - <groupId>org.opengroup.osdu.notification</groupId> - <artifactId>notification-test-core</artifactId> - <version>0.28.0-SNAPSHOT</version> - </dependency> <dependency> <groupId>org.opengroup.osdu</groupId> <artifactId>os-core-common</artifactId> @@ -53,11 +48,6 @@ <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> - <dependency> - <groupId>org.opengroup.osdu</groupId> - <artifactId>core-test-lib-gcp</artifactId> - <version>0.0.2</version> - </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/api/TestPushEndpointGsa.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/api/TestPushEndpointGsa.java index 6fb45d9c743cd24f1f77677da8fec3884264adfd..0f38521f8fef9215965a6d10e22faf71340cda13 100644 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/api/TestPushEndpointGsa.java +++ b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/api/TestPushEndpointGsa.java @@ -18,23 +18,24 @@ package org.opengroup.osdu.notification.api; -import static org.opengroup.osdu.config.util.DecodedContentExtractorUtil.NAIVE_JSON_CONTENT_ACCEPTANCE_TESTER; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.opengroup.osdu.notification.util.Constants.GROUP_ID; import com.google.common.base.Strings; import com.sun.jersey.api.client.ClientResponse; -import java.util.Base64; + import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.function.Predicate; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.opengroup.osdu.config.util.DecodedContentExtractor; import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.opengroup.osdu.core.common.model.notification.GsaSecret; import org.opengroup.osdu.core.common.model.notification.GsaSecretValue; @@ -46,7 +47,7 @@ import org.opengroup.osdu.core.common.notification.SubscriptionException; import org.opengroup.osdu.core.common.notification.SubscriptionFactory; import org.opengroup.osdu.notification.util.*; -public class TestPushEndpointGsa extends BaseTestTemplate { +public class TestPushEndpointGsa{ public static final String REGISTER_BASE_URL = "REGISTER_BASE_URL"; public static final String TOPIC_ID = "TOPIC_ID"; public static final String INTEGRATION_AUDIENCE = "INTEGRATION_AUDIENCE"; @@ -54,11 +55,12 @@ public class TestPushEndpointGsa extends BaseTestTemplate { public static final String STORAGE_HOST = "STORAGE_HOST"; public static final String LEGAL_HOST = "LEGAL_HOST"; public static final String DE_OPS_TESTER = "DE_OPS_TESTER"; + private final static Predicate<String> contentAcceptanceTester = s -> s.trim().startsWith("{"); private String subscriptionId = null; private String notificationId = null; private static ISubscriptionService subscriptionService; private static SubscriptionFactory factory; - private TestUtils testUtils = new AnthosTestUtils(); + private TestUtils testUtils = new TokenTestUtils(); private final String suffix = String.valueOf(System.currentTimeMillis()); private String baseRegisterUrl; private String topic; @@ -70,37 +72,10 @@ public class TestPushEndpointGsa extends BaseTestTemplate { private static final String LEGAL_TAG_NAME = "notification-test-gsa"; private ServicesUtils servicesUtils; - @Override - public void setup() throws Exception { - - } - - @Override - public void tearDown() throws Exception { - - } - - public static void classSetup(String token) throws Exception { - descriptor = new PubsubEndpointGSADescriptor(); - //Configure Register Service Client Library - SubscriptionAPIConfig config = SubscriptionAPIConfig.builder().rootUrl(Config.Instance().RegisterServicePath).build(); - SubscriptionFactory factory = new SubscriptionFactory(config); - - Map<String, String> headers = new HashMap<>(); - headers.put(DpsHeaders.DATA_PARTITION_ID, TestUtils.getOsduTenant()); - headers.put(DpsHeaders.AUTHORIZATION, token); - DpsHeaders dpsHeaders = DpsHeaders.createFromMap(headers); - subscriptionService = factory.create(dpsHeaders); - } - - @Override - protected String getArg() { - return null; - } - - @Override - protected String getInvalidArg() { - return null; + @BeforeAll + public static void classSetup() throws Exception { + // Check if GSA tests are enabled + assumeTrue(Config.Instance().GSATestIsEnabled); } @AfterEach @@ -174,7 +149,7 @@ public class TestPushEndpointGsa extends BaseTestTemplate { gsaSecretValue.setAudience(integrationAudience); String opsTester = new DecodedContentExtractor(System.getProperty(DE_OPS_TESTER, System.getenv(DE_OPS_TESTER)), - NAIVE_JSON_CONTENT_ACCEPTANCE_TESTER).getContent(); + contentAcceptanceTester).getContent(); gsaSecretValue.setKey(opsTester); gsaSecret.setSecretType("GSA"); @@ -193,54 +168,4 @@ public class TestPushEndpointGsa extends BaseTestTemplate { private String getPushUrl() { return baseRegisterUrl + "/test-gc/gsa-challenge/" + suffix; } - - @Override - protected int expectedOkResponseCode() { - return 0; - } - - @Override - public void should_return401_when_noAccessOnCustomerTenant() { - // not used in GC - } - - @Override - public void should_return401_when_accessingWithAdminCredentials() { - // not used in GC - } - - @Override - public void should_return401_when_accessingWithEditorCredentials() { - // not used in GC - } - - @Override - public void should_return401_when_accessingWithNoAccessCredentials() { - // not used in GC - } - - @Override - public void should_returnOk_when_makingHttpOptionsRequest() { - // not used in GC - } - - @Override - public void should_return307_when_makingHttpRequest() { - // not used in GC - } - - @Override - public void should_return20XResponseCode_when_makingValidHttpsRequest() { - // not used in GC - } - - @Override - public void should_return400_when_makingHttpRequestWithoutToken() { - // not used in GC - } - - @Override - public void should_return20X_when_usingCredentialsWithOpsPermission() { - // not used in GC - } } diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/api/TestPushEndpointHMAC.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/api/TestPushEndpointHMAC.java index cd573e14d8c396a61d1ce0940550b974105ae21f..0d549459455a31d8b7985f8310a7c33a2ff72477 100644 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/api/TestPushEndpointHMAC.java +++ b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/api/TestPushEndpointHMAC.java @@ -37,7 +37,7 @@ import org.opengroup.osdu.core.common.notification.SubscriptionException; import org.opengroup.osdu.core.common.notification.SubscriptionFactory; import org.opengroup.osdu.notification.util.*; -public class TestPushEndpointHMAC extends BaseTestTemplate { +public class TestPushEndpointHMAC{ public static final String REGISTER_BASE_URL = "REGISTER_BASE_URL"; public static final String TOPIC_ID = "TOPIC_ID"; public static final String HMAC_SECRET = "HMAC_SECRET"; @@ -48,7 +48,7 @@ public class TestPushEndpointHMAC extends BaseTestTemplate { private String notificationId = null; private ISubscriptionService subscriptionService; private static SubscriptionFactory factory; - private TestUtils testUtils = new AnthosTestUtils(); + private TestUtils testUtils = new TokenTestUtils(); private final String suffix = String.valueOf(System.currentTimeMillis()); private String baseRegisterUrl; private String topic; @@ -60,32 +60,6 @@ public class TestPushEndpointHMAC extends BaseTestTemplate { private static final String LEGAL_TAG_NAME = "notification-test-hmac"; private ServicesUtils servicesUtils; - @Override - public void setup() throws Exception { - - } - - @Override - public void tearDown() throws Exception { - - } - - public static void classSetup() throws Exception { - descriptor = new PubsubEndpointHMACDescriptor(); - - //Configure Register Service Client Library - SubscriptionAPIConfig config = SubscriptionAPIConfig.builder().rootUrl(Config.Instance().RegisterServicePath).build(); - factory = new SubscriptionFactory(config); - } - @Override - protected String getArg() { - return notificationId; - } - - @Override - protected String getInvalidArg() { - return null; - } @AfterEach public void deleteResource() throws Exception { if (Objects.isNull(subscriptionService)) { @@ -161,56 +135,7 @@ public class TestPushEndpointHMAC extends BaseTestTemplate { throw e; } } - @Override - protected int expectedOkResponseCode() { - return 200; - } private String getPushUrl() { return baseRegisterUrl + "/test-gc/challenge/" + suffix; } - - @Override - public void should_return401_when_noAccessOnCustomerTenant() { - // not used in GC - } - - @Override - public void should_return401_when_accessingWithAdminCredentials() { - // not used in GC - } - - @Override - public void should_return401_when_accessingWithEditorCredentials() { - // not used in GC - } - - @Override - public void should_return401_when_accessingWithNoAccessCredentials() { - // not used in GC - } - - @Override - public void should_returnOk_when_makingHttpOptionsRequest() { - // not used in GC - } - - @Override - public void should_return307_when_makingHttpRequest() { - // not used in GC - } - - @Override - public void should_return20XResponseCode_when_makingValidHttpsRequest() { - // not used in GC - } - - @Override - public void should_return400_when_makingHttpRequestWithoutToken() { - // not used in GC - } - - @Override - public void should_return20X_when_usingCredentialsWithOpsPermission() { - // not used in GC - } } diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/BaseTestTemplate.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/BaseTestTemplate.java deleted file mode 100644 index 01a0be5de9ef90263eb40f6efd729e3919b72ddb..0000000000000000000000000000000000000000 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/BaseTestTemplate.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * 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.notification.util; - -import com.sun.jersey.api.client.ClientResponse; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public abstract class BaseTestTemplate extends TestBase { - - protected static RestDescriptor descriptor; - - public BaseTestTemplate () { - } - - public BaseTestTemplate(RestDescriptor descriptor) { - this.descriptor = descriptor; - } - - protected abstract String getArg(); - protected abstract String getInvalidArg(); - protected String error(String body){ - return String.format("%s: %s %s %s", descriptor.getHttpMethod(), descriptor.getPath(), descriptor.getQuery(), body); - } - protected void validate20XResponse(ClientResponse response, RestDescriptor descriptor){ - if(response.getStatus() != 204) - System.out.println(response.getEntity(String.class)); - } - protected abstract void deleteResource()throws Exception; - protected abstract void createResource() throws Exception ; - protected abstract int expectedOkResponseCode(); - - protected String getOsduTenantAdminCredentials() throws Exception{ - return testUtils.getAdminToken(); - } - - @Test - public void should_return401_when_noAccessOnCustomerTenant() throws Exception { - ClientResponse response = descriptor.runOnCustomerTenant(getArg(), getOsduTenantAdminCredentials()); - assertEquals(error( response.getEntity(String.class)),401, response.getStatus()); - } - - @Test - public void should_return401_when_accessingWithAdminCredentials() throws Exception{ - ClientResponse response = descriptor.run(getArg(), testUtils.getAdminToken()); - assertEquals(error( response.getEntity(String.class)), 401, response.getStatus()); - } - - @Test - public void should_return401_when_accessingWithEditorCredentials() throws Exception{ - ClientResponse response = descriptor.run(getArg(), testUtils.getEditorToken()); - assertEquals(error( response.getEntity(String.class)), 401, response.getStatus()); - } - - @Test - public void should_return401_when_accessingWithNoAccessCredentials() throws Exception{ - ClientResponse response = descriptor.run(getArg(), testUtils.getNoAccessToken()); - assertEquals(error( response.getEntity(String.class)), 401, response.getStatus()); - } - - @Test - public void should_return20X_when_usingCredentialsWithOpsPermission() throws Exception{ - createResource(); - - try { - ClientResponse response = descriptor.run(getArg(), testUtils.getOpsToken()); - - assertEquals(error(response.getStatus() == 204 ? "" : response.getEntity(String.class)), expectedOkResponseCode(), response.getStatus()); - assertEquals("GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH", response.getHeaders().getFirst("Access-Control-Allow-Methods")); - assertEquals("access-control-allow-origin, origin, content-type, accept, authorization, data-partition-id, correlation-id, appkey", response.getHeaders().getFirst("Access-Control-Allow-Headers")); - assertEquals("*", response.getHeaders().getFirst("Access-Control-Allow-Origin")); - assertEquals("true", response.getHeaders().getFirst("Access-Control-Allow-Credentials")); - assertEquals("DENY", response.getHeaders().getFirst("X-Frame-Options")); - assertEquals("1; mode=block", response.getHeaders().getFirst("X-XSS-Protection")); - assertEquals("nosniff", response.getHeaders().getFirst("X-Content-Type-Options")); - assertEquals("no-cache, no-store, must-revalidate", response.getHeaders().getFirst("Cache-Control")); - assertEquals("default-src 'self'", response.getHeaders().getFirst("Content-Security-Policy")); - assertTrue(response.getHeaders().get("Strict-Transport-Security").get(0).contains("max-age=31536000")); - assertTrue(response.getHeaders().get("Strict-Transport-Security").get(0).contains("includeSubDomains")); - assertEquals("0", response.getHeaders().getFirst("Expires")); - } finally { - deleteResource(); - } - } - - @Test - public void should_returnOk_when_makingHttpOptionsRequest() throws Exception{ - ClientResponse response = descriptor.runOptions(getArg(), testUtils.getEditorToken()); - assertEquals(error( response.getEntity(String.class)), 200, response.getStatus()); - } - @Ignore("Issue logged in GL") - @Test - public void should_return307_when_makingHttpRequest()throws Exception{ - if(Config.Instance().isLocalHost() || Config.isGke()) - return; //localhost and GKE we expect to be able to use http so we shortcut the test - - ClientResponse response = descriptor.runHttp(getArg(), testUtils.getOpsToken()); - assertEquals(error( response.getEntity(String.class)),307, response.getStatus()); - } - @Test - public void should_return20XResponseCode_when_makingValidHttpsRequest()throws Exception{ - createResource(); - try { - ClientResponse response = descriptor.run(getArg(), testUtils.getOpsToken()); - assertEquals(error(""), expectedOkResponseCode(), response.getStatus()); - validate20XResponse(response, descriptor); - }finally { - deleteResource(); - } - } - - @Test - public void should_return400_when_makingHttpRequestWithoutToken()throws Exception{ - ClientResponse response = descriptor.run(getArg(), ""); - assertEquals(error( response.getEntity(String.class)), 302, response.getStatus()); - } - -} diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/Config.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/Config.java index accd832847e95fb6c1d55a3a5807abf6467b3db7..29a39b08909ee45cf609a4445f5810801934c632 100644 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/Config.java +++ b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/Config.java @@ -32,6 +32,7 @@ public class Config { public String DE_OPS_TESTER = System.getProperty("DE_OPS_TESTER", System.getenv("DE_OPS_TESTER")); public String RetryCount; public String TimeOutSeconds; + public Boolean GSATestIsEnabled; private static Config config = new Config(); public static Config Instance() { @@ -43,6 +44,7 @@ public class Config { config.TimeOutSeconds = getEnvironmentVariableOrDefaultValue("TIME_OUT_SECONDS", "60"); config.RetryCount = getEnvironmentVariableOrDefaultValue("RETRY_COUNT", "3"); config.hmacSecretValue = System.getProperty("HMAC_SECRET", System.getenv("HMAC_SECRET")); + config.GSATestIsEnabled = Boolean.parseBoolean(getEnvironmentVariableOrDefaultValue("GSA_TEST_ENABLED", "false").trim().toLowerCase()); if (env.equalsIgnoreCase("LOCAL")) { //make sure to run register service on a different port. You can also choose to point to Register service that is running in cloud String registerUrl = "http://localhost:8081/api/register/v1"; diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/DecodedContentExtractor.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/DecodedContentExtractor.java new file mode 100644 index 0000000000000000000000000000000000000000..106e582fb90cd7c4fc249de76600cd84653d795f --- /dev/null +++ b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/DecodedContentExtractor.java @@ -0,0 +1,126 @@ +/* + * Copyright 2020-2024 Google LLC + * Copyright 2020-2024 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.notification.util; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Base64; +import java.util.function.Predicate; +import lombok.RequiredArgsConstructor; +import lombok.extern.java.Log; + +@Log +@RequiredArgsConstructor +public class DecodedContentExtractor { + private final String inputFilenameOrContent; + private final Predicate<String> contentAcceptanceTester; + private boolean validOutputContentFound; + private String outputContent; + + public String getContent() { + + validOutputContentFound = false; + outputContent = null; + + log.info("Treat value as a content"); + if (inputFilenameOrContent.trim().isEmpty()) { + log.info("provided value is empty. Output as is."); + return setValidOutputContent(inputFilenameOrContent); + } + + if (!treatValueAsAContent(inputFilenameOrContent)) { + log.info("Value is not a valid content. Treat value as a filename"); + if (!treatValueAsAFileName(inputFilenameOrContent)){ + log.info("Value is not a filename with a valid content"); + } + + } + + return getValidOutputContentIfFound(); + } + + private boolean treatValueAsAContent(String input) { + if (contentAcceptanceTester.test(input)) { + log.info("the value is a valid content. Output as is."); + setValidOutputContent(input); + return true; + } + String output; + try { + output = new String(Base64.getDecoder().decode(input)); + log.info("the value is probably Base64 encoded. Just decoded"); + if (contentAcceptanceTester.test(output)) { + log.info("the decoded value is a valid content. Output decoded value."); + setValidOutputContent(output); + } else { + log.info("the decoded value is not a valid content."); + } + } catch (IllegalArgumentException e) { + log.info("the value is not Base64 encoded. "); + } + + return validOutputContentFound; + } + + private boolean treatValueAsAFileName(String filename) { + + if (treatFileContent(filename)) return true; + + try { + filename = new String(Base64.getDecoder().decode(filename)); + log.info("the filename is probably Base64 encoded. Just decoded"); + if (treatFileContent(filename)) return true; + } catch (IllegalArgumentException e) { + log.info("the filename is not Base64 encoded. "); + } + return validOutputContentFound; + } + + private boolean treatFileContent(String filename) { + try { + Path path = Paths.get(filename); + if (Files.exists(path)) { + log.info("the filename is of existing file. Read file."); + try { + String fileContent = new String(Files.readAllBytes(path)); + if (treatValueAsAContent(fileContent)) { + return true; + } + } catch (IOException | SecurityException | OutOfMemoryError ex) { + log.info(() -> ("unable to read the file: " + ex.getClass().getSimpleName())); + } + } + } catch (InvalidPathException ex) { + log.info("the filename is not valid or the file doesn't exist."); + } + return false; + } + + private String setValidOutputContent(String outputContent) { + this.outputContent = outputContent; + this.validOutputContentFound = true; + return getValidOutputContentIfFound(); + } + + public String getValidOutputContentIfFound() { + return validOutputContentFound ? outputContent : null; + } +} diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/FileUtils.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/FileUtils.java index ad945f0de2891e142bb9bb3763f9a8d65311810c..e3af459967382cd0600f8f6a51a6a2135641cda8 100644 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/FileUtils.java +++ b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/FileUtils.java @@ -21,7 +21,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.net.URL; public class FileUtils { public String readFromLocalFilePath(String filePath) throws IOException { diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/HttpClient.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/HttpClient.java index b14839c915deb5235307f3d80a0491beabe5df64..1df1321be243b60b237a710b1d03523e67e25ca6 100644 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/HttpClient.java +++ b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/HttpClient.java @@ -17,7 +17,7 @@ package org.opengroup.osdu.notification.util; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import com.google.gson.Gson; import com.sun.jersey.api.client.Client; diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/ServicesUtils.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/ServicesUtils.java index eabebc585ab5316e311ea07969044766fe9e9fe8..2eaf64b6c072230574047df9ce11c9aa1e450f26 100644 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/ServicesUtils.java +++ b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/ServicesUtils.java @@ -17,8 +17,8 @@ package org.opengroup.osdu.notification.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import com.sun.jersey.api.client.ClientResponse; import java.util.HashMap; diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TestBase.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TestBase.java deleted file mode 100644 index 093ca9b735c2b00d9e72772cd0e397dace32c174..0000000000000000000000000000000000000000 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TestBase.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.opengroup.osdu.notification.util; - -public abstract class TestBase { - protected TestUtils testUtils = null; - - public abstract void setup() throws Exception; - - public abstract void tearDown() throws Exception; -} diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TestUtils.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TestUtils.java index 2f26d0d1b545b9d685d18cb2381f21cd42cc07a8..ee4f21322ae7edff314b1e83a7d12af203f7be44 100644 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TestUtils.java +++ b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TestUtils.java @@ -16,7 +16,7 @@ package org.opengroup.osdu.notification.util; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.net.URL; import java.security.SecureRandom; import java.security.cert.X509Certificate; @@ -29,9 +29,6 @@ import javax.net.ssl.X509TrustManager; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; diff --git a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/AnthosTestUtils.java b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TokenTestUtils.java similarity index 89% rename from notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/AnthosTestUtils.java rename to notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TokenTestUtils.java index 79de4d808f12f10e59a0e76799f6f93439c6deb9..76c0845035573e167123ba019ffed28b29cf4227 100644 --- a/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/AnthosTestUtils.java +++ b/notification-acceptance-test/src/test/java/org/opengroup/osdu/notification/util/TokenTestUtils.java @@ -21,7 +21,7 @@ import com.google.common.base.Strings; import lombok.extern.slf4j.Slf4j; @Slf4j -public class AnthosTestUtils extends TestUtils { +public class TokenTestUtils extends TestUtils { public static final String DE_ADMIN_USER_TOKEN = "DE_ADMIN_USER_TOKEN"; public static final String DE_EDITOR_USER_TOKEN = "DE_EDITOR_USER_TOKEN"; @@ -29,7 +29,7 @@ public class AnthosTestUtils extends TestUtils { public static final String NO_ACCESS_USER_TOKEN = "NO_ACCESS_USER_TOKEN"; private OpenIDTokenProvider tokenProvider; - public AnthosTestUtils() { + public TokenTestUtils() { adminToken = System.getProperty(DE_ADMIN_USER_TOKEN, System.getenv(DE_ADMIN_USER_TOKEN)); editorToken = System.getProperty(DE_EDITOR_USER_TOKEN, System.getenv(DE_EDITOR_USER_TOKEN)); opsToken = System.getProperty(DE_OPS_USER_TOKEN, System.getenv(DE_OPS_USER_TOKEN)); @@ -37,9 +37,10 @@ public class AnthosTestUtils extends TestUtils { if (Strings.isNullOrEmpty(adminToken) || Strings.isNullOrEmpty(editorToken) || Strings.isNullOrEmpty(opsToken) || Strings.isNullOrEmpty(noAccessToken)) { tokenProvider = new OpenIDTokenProvider(); - adminToken = tokenProvider.getAdminAccessToken(); - editorToken = tokenProvider.getEditorAccessToken(); - opsToken = tokenProvider.getOpsAccessToken(); + adminToken = getAdminToken(); + editorToken = getEditorToken(); + opsToken = getOpsToken(); + noAccessToken = getNoAccessToken(); } }