Commit b2616d01 authored by Anastasiia Gelmut's avatar Anastasiia Gelmut
Browse files

GONRG-1370 Fixed Sonar Comments

parent 012bec1f
/*
Copyright 2020 Google LLC
Copyright 2020 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.register.provider.gcp;
import org.springframework.boot.SpringApplication;
......
......@@ -36,6 +36,8 @@ import java.util.List;
@Repository
public class DatastoreActionRepo implements IActionRepo {
private static final String CONFLICT_REASON = "Conflict";
@Getter
private static final String NAMESPACE = "DE";
@Getter
......@@ -103,7 +105,7 @@ public class DatastoreActionRepo implements IActionRepo {
if (ex.getMessage().startsWith("too much contention")) {
return throwConflict();
} else {
throw new AppException(409, "Conflict", "ABORTED due to conflict");
throw new AppException(409, CONFLICT_REASON, "ABORTED due to conflict");
}
} else
throw ex;
......@@ -116,7 +118,7 @@ public class DatastoreActionRepo implements IActionRepo {
}
private Action throwConflict() {
throw new AppException(409, "Conflict", "An action already exists with the same message and endpoint combination");
throw new AppException(409, CONFLICT_REASON, "An action already exists with the same message and endpoint combination");
}
private boolean deleteFromDatastore(String id) {
......@@ -135,9 +137,9 @@ public class DatastoreActionRepo implements IActionRepo {
} catch (DatastoreException ex) {
if (ex.getCode() == 10) {
if (ex.getMessage().startsWith("too much contention")) {
throw new AppException(409, "Conflict", "Another request is trying to delete the same DDMS.");
throw new AppException(409, CONFLICT_REASON, "Another request is trying to delete the same DDMS.");
} else {
throw new AppException(409, "Conflict", "ABORTED due to conflict");
throw new AppException(409, CONFLICT_REASON, "ABORTED due to conflict");
}
} else
throw ex;
......@@ -210,17 +212,11 @@ public class DatastoreActionRepo implements IActionRepo {
private FullEntity convertActionToEntity(Action action, Key key) {
List<StringValue> types = new ArrayList<>();
action.getFilter().getEntityType().forEach((t) -> {
types.add(new StringValue(t));
});
action.getFilter().getEntityType().forEach(t -> types.add(new StringValue(t)));
List<StringValue> source = new ArrayList<>();
action.getFilter().getSource().forEach((t) -> {
source.add(new StringValue(t));
});
action.getFilter().getSource().forEach(t -> source.add(new StringValue(t)));
List<StringValue> version = new ArrayList<>();
action.getFilter().getVersion().forEach((t) -> {
version.add(new StringValue(t));
});
action.getFilter().getVersion().forEach(t -> version.add(new StringValue(t)));
FullEntity.Builder entity = FullEntity.newBuilder(key)
.set(NAME, action.getName())
......
......@@ -44,6 +44,7 @@ public class DatastoreDdmsRepository implements IDdmsRepository {
private static final String TYPE = "type";
private static final String SCHEMA = "schema";
private static final String INTERFACE_KIND = "DMS_INTERFACE";
private static final String CONFLICT_REASON = "Conflict";
@Autowired
private DatastoreMultiTenantAccess dataStoreTenants;
......@@ -60,15 +61,15 @@ public class DatastoreDdmsRepository implements IDdmsRepository {
try {
Entity existingEntity = txn.get(entity.getKey());
if (existingEntity != null)
throw new AppException(409, "Conflict", "A DDMS already exists with the same id");
throw new AppException(409, CONFLICT_REASON, "A DDMS already exists with the same id");
txn.put(entity);
txn.commit();
} catch (DatastoreException ex) {
if (ex.getCode() == 10) {
if (ex.getMessage().startsWith("too much contention")) {
throw new AppException(409, "Conflict", "A DDMS already exists with the same id");
throw new AppException(409, CONFLICT_REASON, "A DDMS already exists with the same id");
} else {
throw new AppException(409, "Conflict", "ABORTED due to conflict");
throw new AppException(409, CONFLICT_REASON, "ABORTED due to conflict");
}
} else
throw ex;
......@@ -124,9 +125,9 @@ public class DatastoreDdmsRepository implements IDdmsRepository {
} catch (DatastoreException ex) {
if (ex.getCode() == 10) {
if (ex.getMessage().startsWith("too much contention")) {
throw new AppException(409, "Conflict", "Another request is trying to delete the same DDMS.");
throw new AppException(409, CONFLICT_REASON, "Another request is trying to delete the same DDMS.");
} else {
throw new AppException(409, "Conflict", "ABORTED due to conflict");
throw new AppException(409, CONFLICT_REASON, "ABORTED due to conflict");
}
} else
throw ex;
......
/*
Copyright 2020 Google LLC
Copyright 2020 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.register.provider.gcp.logging;
import org.opengroup.osdu.core.common.logging.ILogger;
......
/*
Copyright 2020 Google LLC
Copyright 2020 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.register.provider.gcp.security;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
......
......@@ -33,6 +33,11 @@ import java.util.List;
@Repository
public class DatastoreAccess implements IDatastoreAccess {
private static final String SERVER_ERROR_REASON = "Server Error";
private static final String CONFLICT_REASON = "Conflict";
private static final String TOO_MUCH_CONTENTION_MSG = "too much contention";
private static final String ABORTED_DUE_TO_CONFLICT_MSG = "ABORTED due to conflict";
@Autowired
private PushSubscription pushSubscription;
@Autowired
......@@ -54,7 +59,7 @@ public class DatastoreAccess implements IDatastoreAccess {
pushSubscription.create(s, tenantInfo, this.config.getGoogleCloudProject(), config.getServiceAccountIdentity());
} catch (Exception e) {
deleteFromDatastore(s.getId(), false);
throw new AppException(500, "Server Error", "Unexpected error creating subscription", e);
throw new AppException(500, SERVER_ERROR_REASON, "Unexpected error creating subscription", e);
}
return s;
}
......@@ -70,7 +75,7 @@ public class DatastoreAccess implements IDatastoreAccess {
try {
return modelEntityHelper.convertEntityToSubscription(entity);
} catch (Exception e) {
throw new AppException(500, "Server Error", "Unexpected error getting subscription", e);
throw new AppException(500, SERVER_ERROR_REASON, "Unexpected error getting subscription", e);
}
}
......@@ -92,7 +97,7 @@ public class DatastoreAccess implements IDatastoreAccess {
}
return output;
} catch (Exception e) {
throw new AppException(500, "Server Error", "Unexpected error querying subscription", e);
throw new AppException(500, SERVER_ERROR_REASON, "Unexpected error querying subscription", e);
}
}
......@@ -134,20 +139,20 @@ public class DatastoreAccess implements IDatastoreAccess {
FullEntity<Key> entity = modelEntityHelper.convertSubscriptionToEntity(s, key);
Entity existingEntity = txn.get(entity.getKey());
if (existingEntity != null)
throw new AppException(409, "Conflict", "A subscriber already exists with the same topic and endpoint combination");
throw new AppException(409, CONFLICT_REASON, "A subscriber already exists with the same topic and endpoint combination");
txn.put(entity);
txn.commit();
} catch (DatastoreException ex) {
if (ex.getCode() == 10) {
if (ex.getMessage().startsWith("too much contention")) {
throw new AppException(409, "Conflict", "A subscriber already exists with the same topic and endpoint combination");
if (ex.getMessage().startsWith(TOO_MUCH_CONTENTION_MSG)) {
throw new AppException(409, CONFLICT_REASON, "A subscriber already exists with the same topic and endpoint combination");
} else {
throw new AppException(409, "Conflict", "ABORTED due to conflict");
throw new AppException(409, CONFLICT_REASON, ABORTED_DUE_TO_CONFLICT_MSG);
}
} else
throw ex;
} catch (IOException ex) {
throw new AppException(500, "Server Error", "Unexpected error creating subscription.", ex);
throw new AppException(500, SERVER_ERROR_REASON, "Unexpected error creating subscription.", ex);
} finally {
if (txn.isActive()) {
txn.rollback();
......@@ -175,15 +180,15 @@ public class DatastoreAccess implements IDatastoreAccess {
}
} catch (DatastoreException ex) {
if (ex.getCode() == 10) {
if (ex.getMessage().startsWith("too much contention")) {
throw new AppException(409, "Conflict", "Another request is trying to delete the same DDMS.");
if (ex.getMessage().startsWith(TOO_MUCH_CONTENTION_MSG)) {
throw new AppException(409, CONFLICT_REASON, "Another request is trying to delete the same DDMS.");
} else {
throw new AppException(409, "Conflict", "ABORTED due to conflict");
throw new AppException(409, CONFLICT_REASON, ABORTED_DUE_TO_CONFLICT_MSG);
}
} else
throw ex;
} catch (IOException ex) {
throw new AppException(500, "Server Error", "Unexpected error deleting subscription.", ex);
throw new AppException(500, SERVER_ERROR_REASON, "Unexpected error deleting subscription.", ex);
} finally {
if (txn.isActive()) {
txn.rollback();
......@@ -208,15 +213,15 @@ public class DatastoreAccess implements IDatastoreAccess {
}
} catch (DatastoreException ex) {
if (ex.getCode() == 10) {
if (ex.getMessage().startsWith("too much contention")) {
throw new AppException(409, "Conflict", "Another request is trying to update the same subscription.");
if (ex.getMessage().startsWith(TOO_MUCH_CONTENTION_MSG)) {
throw new AppException(409, CONFLICT_REASON, "Another request is trying to update the same subscription.");
} else {
throw new AppException(409, "Conflict", "ABORTED due to conflict");
throw new AppException(409, CONFLICT_REASON, ABORTED_DUE_TO_CONFLICT_MSG);
}
} else
throw ex;
} catch (IOException ex) {
throw new AppException(500, "Server Error", "Unexpected error updating subscription.", ex);
throw new AppException(500, SERVER_ERROR_REASON, "Unexpected error updating subscription.", ex);
} finally {
if (txn.isActive()) {
txn.rollback();
......
......@@ -33,9 +33,9 @@ import java.io.IOException;
@Component
public class PushSubscription {
private static final int ackDeadlineSeconds = 60;
private static final int messageRetentionSeconds = 432000;
private static final int subscriptionExpirationSeconds = 31540000;
private static final int ACK_DEADLINE_SECONDS = 60;
private static final int MESSAGE_RETENTION_SECONDS = 432000;
private static final int SUBSCRIPTION_EXPIRATION_SECONDS = 31540000;
@Autowired
private AppServiceConfig serviceConfig;
......@@ -79,9 +79,10 @@ public class PushSubscription {
.setTopic(topicName.toString())
.setPushConfig(config)
.setExpirationPolicy(ExpirationPolicy.newBuilder().setTtl(
Duration.newBuilder().setSeconds(subscriptionExpirationSeconds).build()).build())
.setMessageRetentionDuration(Duration.newBuilder().setSeconds(messageRetentionSeconds).build())
.setAckDeadlineSeconds(ackDeadlineSeconds)
Duration.newBuilder().setSeconds(SUBSCRIPTION_EXPIRATION_SECONDS).build()).build())
.setMessageRetentionDuration(Duration.newBuilder().setSeconds(
MESSAGE_RETENTION_SECONDS).build())
.setAckDeadlineSeconds(ACK_DEADLINE_SECONDS)
.build());
}
}
......
......@@ -81,15 +81,15 @@ public class ModelEntityHelperTest {
when(kmsClient.decryptString(eq("encrypted"))).thenReturn("decrypted");
Subscription subscription = sut.convertEntityToSubscription(testEntity);
assertEquals(subscription.getId(), "entity");
assertEquals(subscription.getCreatedBy(), "test@opengroup.org");
assertEquals(subscription.getCreatedOnEpoch(), now);
assertEquals(subscription.getName(), "testEntity");
assertEquals(subscription.getDescription(), "Description");
assertEquals(subscription.getNotificationId(), "notificationId");
assertEquals(subscription.getPushEndpoint(), "endpoint");
assertEquals(subscription.getSecret().getSecretType(), "HMAC");
assertEquals(((HmacSecret) subscription.getSecret()).getValue(), "decrypted");
assertEquals("entity", subscription.getId());
assertEquals("test@opengroup.org", subscription.getCreatedBy());
assertEquals(now, subscription.getCreatedOnEpoch());
assertEquals("testEntity", subscription.getName());
assertEquals("Description", subscription.getDescription());
assertEquals("notificationId", subscription.getNotificationId());
assertEquals("endpoint", subscription.getPushEndpoint());
assertEquals("HMAC", subscription.getSecret().getSecretType());
assertEquals("decrypted", ((HmacSecret) subscription.getSecret()).getValue());
}
@Test
......@@ -109,16 +109,16 @@ public class ModelEntityHelperTest {
when(kmsClient.decryptString(eq("encrypted"))).thenReturn("audience`key");
Subscription subscription = sut.convertEntityToSubscription(testEntity);
assertEquals(subscription.getId(), "entity");
assertEquals(subscription.getCreatedBy(), "test@opengroup.org");
assertEquals(subscription.getCreatedOnEpoch(), now);
assertEquals(subscription.getName(), "testEntity");
assertEquals(subscription.getDescription(), "Description");
assertEquals(subscription.getNotificationId(), "notificationId");
assertEquals(subscription.getPushEndpoint(), "endpoint");
assertEquals(subscription.getSecret().getSecretType(), "GSA");
assertEquals(((GsaSecret) subscription.getSecret()).getValue().getAudience(), "audience");
assertEquals(((GsaSecret) subscription.getSecret()).getValue().getKey(), "key");
assertEquals("entity", subscription.getId());
assertEquals("test@opengroup.org", subscription.getCreatedBy());
assertEquals(now, subscription.getCreatedOnEpoch());
assertEquals("testEntity", subscription.getName());
assertEquals("Description", subscription.getDescription());
assertEquals("notificationId", subscription.getNotificationId());
assertEquals("endpoint", subscription.getPushEndpoint());
assertEquals("GSA", subscription.getSecret().getSecretType());
assertEquals("audience", ((GsaSecret) subscription.getSecret()).getValue().getAudience());
assertEquals("key", ((GsaSecret) subscription.getSecret()).getValue().getKey());
}
@Test
......@@ -136,13 +136,13 @@ public class ModelEntityHelperTest {
when(kmsClient.encryptString(eq("decrypted"))).thenReturn("encrypted");
FullEntity entity = sut.convertSubscriptionToEntity(subscription, KEY);
assertEquals(entity.getString(NAME), "testEntity");
assertEquals(entity.getString(CREATED_BY), "test@opengroup.org");
assertEquals(entity.getString(DESCRIPTION), "Description");
assertEquals(entity.getString(SUBSCRIPTION), "notificationId");
assertEquals(entity.getString(PUSH_ENDPOINT), "endpoint");
assertEquals(entity.getString(SECRET_TYPE), "HMAC");
assertEquals(entity.getString(SECRET_VALUE), "encrypted");
assertEquals("testEntity", entity.getString(NAME));
assertEquals("test@opengroup.org", entity.getString(CREATED_BY));
assertEquals("Description", entity.getString(DESCRIPTION));
assertEquals("notificationId", entity.getString(SUBSCRIPTION));
assertEquals("endpoint", entity.getString(PUSH_ENDPOINT));
assertEquals("HMAC", entity.getString(SECRET_TYPE));
assertEquals("encrypted", entity.getString(SECRET_VALUE));
assertNotNull(entity.getTimestamp(CREATED_ON));
}
......@@ -161,13 +161,13 @@ public class ModelEntityHelperTest {
when(kmsClient.encryptString(eq("audience`key"))).thenReturn("encrypted");
FullEntity entity = sut.convertSubscriptionToEntity(subscription, KEY);
assertEquals(entity.getString(NAME), "testEntity");
assertEquals(entity.getString(CREATED_BY), "test@opengroup.org");
assertEquals(entity.getString(DESCRIPTION), "Description");
assertEquals(entity.getString(SUBSCRIPTION), "notificationId");
assertEquals(entity.getString(PUSH_ENDPOINT), "endpoint");
assertEquals(entity.getString(SECRET_TYPE), "GSA");
assertEquals(entity.getString(SECRET_VALUE), "encrypted");
assertEquals("testEntity", entity.getString(NAME));
assertEquals("test@opengroup.org", entity.getString(CREATED_BY));
assertEquals("Description", entity.getString(DESCRIPTION));
assertEquals("notificationId", entity.getString(SUBSCRIPTION));
assertEquals("endpoint", entity.getString(PUSH_ENDPOINT));
assertEquals("GSA", entity.getString(SECRET_TYPE));
assertEquals("encrypted", entity.getString(SECRET_VALUE));
assertNotNull(entity.getTimestamp(CREATED_ON));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment