Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.netcracker.cloud.dbaas.controller.error;

import com.netcracker.cloud.dbaas.exceptions.AdapterException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import static com.netcracker.cloud.dbaas.controller.error.Utils.buildDefaultResponse;

@Provider
public class AdapterExceptionMapper implements ExceptionMapper<AdapterException> {

@Context
UriInfo uriInfo;

@Override
public Response toResponse(AdapterException exception) {
return buildDefaultResponse(uriInfo, exception, Response.Status.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.netcracker.cloud.dbaas.exceptions;

import com.netcracker.cloud.core.error.runtime.ErrorCodeException;

public class AdapterException extends ErrorCodeException {
public AdapterException(String detail) {
super(ErrorCodes.CORE_DBAAS_4053, ErrorCodes.CORE_DBAAS_4053.getDetail(detail));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public enum ErrorCodes implements ErrorCode {
"Digest mismatch",
"Digest header mismatch: %s"
),
CORE_DBAAS_4053(
"CORE-DBAAS-4053",
"Failed request to physical adapter",
"An error occurred during the request to adapter: %s"
),
CORE_DBAAS_7002(
"CORE-DBAAS-7002",
"trackingId not found",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.netcracker.cloud.dbaas.monitoring.interceptor;

import com.netcracker.cloud.dbaas.exceptions.AdapterException;
import com.netcracker.cloud.dbaas.monitoring.annotation.TimeMeasure;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
Expand All @@ -8,12 +9,14 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.interceptor.InvocationContext;
import jakarta.ws.rs.ProcessingException;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -86,22 +89,29 @@ public InvocationHandler provideTimeMeasurementInvocationHandler(Object object)
method = object.getClass().getMethod(method.getName(), method.getParameterTypes());
TimeMeasure timeMeasureAnnotation = MethodUtils.getAnnotation(method, TimeMeasure.class, true, true);
Object returnValue;
if (timeMeasureAnnotation != null) {
String metricName = timeMeasureAnnotation.value();
Tags tags = getTags(object, timeMeasureAnnotation);
Timer.Sample sample = Timer.start(meterRegistry);
try {
try {
if (timeMeasureAnnotation != null) {
String metricName = timeMeasureAnnotation.value();
Tags tags = getTags(object, timeMeasureAnnotation);
Timer.Sample sample = Timer.start(meterRegistry);
try {
returnValue = method.invoke(object, args);
increaseSuccessRequestCounter(metricName, tags);
} catch (Throwable throwable) {
increaseFailRequestCounter(metricName, tags);
throw throwable;
} finally {
Timer timer = meterRegistry.timer(metricName, tags);
sample.stop(timer);
}
} else {
returnValue = method.invoke(object, args);
increaseSuccessRequestCounter(metricName, tags);
} catch (Throwable throwable) {
increaseFailRequestCounter(metricName, tags);
throw throwable;
} finally {
Timer timer = meterRegistry.timer(metricName, tags);
sample.stop(timer);
}
} else {
returnValue = method.invoke(object, args);
} catch (InvocationTargetException exception) {
if (exception.getCause() instanceof ProcessingException processingException) {
throw new AdapterException(processingException.getMessage());
}
throw exception.getCause() != null ? exception.getCause() : exception;
}
return returnValue;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.netcracker.cloud.dbaas.rest;

import com.netcracker.cloud.dbaas.exceptions.AdapterException;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;

public class AdapterResponseExceptionMapper implements ResponseExceptionMapper<AdapterException> {

@Override
public AdapterException toThrowable(Response response) {
String errorMsg = response.readEntity(String.class);
return new AdapterException(errorMsg);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.netcracker.cloud.dbaas.service;

import com.netcracker.cloud.dbaas.rest.AdapterResponseExceptionMapper;
import com.netcracker.cloud.dbaas.dto.v3.ApiVersion;
import com.netcracker.cloud.dbaas.monitoring.interceptor.TimeMeasurementManager;
import com.netcracker.cloud.dbaas.rest.BasicAuthFilter;
Expand Down Expand Up @@ -38,6 +39,7 @@ public DbaasAdapter createDbaasAdapterClientV2(String username, String password,
DbaasAdapterRestClientV2 restClient = RestClientBuilder.newBuilder().baseUri(URI.create(adapterAddress))
.register(authFilter)
.register(new DbaasAdapterRestClientLoggingFilter())
.register(new AdapterResponseExceptionMapper())
.connectTimeout(3, TimeUnit.MINUTES)
.readTimeout(3, TimeUnit.MINUTES)
.build(DbaasAdapterRestClientV2.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.netcracker.cloud.dbaas.dto.ClassifierWithRolesRequest;
import com.netcracker.cloud.dbaas.dto.v3.*;
import com.netcracker.cloud.dbaas.entity.pg.*;
import com.netcracker.cloud.dbaas.exceptions.AdapterException;
import com.netcracker.cloud.dbaas.exceptions.ErrorCodes;
import com.netcracker.cloud.dbaas.exceptions.UnregisteredPhysicalDatabaseException;
import com.netcracker.cloud.dbaas.integration.config.PostgresqlContainerResource;
Expand Down Expand Up @@ -123,6 +124,41 @@ void testCreateDatabaseWithRoHost() throws JsonProcessingException {
.body("connectionProperties.roHost", is(TEST_RO_HOST));
}

@Test
void testCreateDatabase_WhenAdapterException_ThenReturns500WithMappedError() throws JsonProcessingException {
when(dBaaService.getConnectionPropertiesService()).thenReturn(processConnectionPropertiesService);
PhysicalDatabase physicalDatabase = new PhysicalDatabase();
physicalDatabase.setPhysicalDatabaseIdentifier(PHYSICAL_DATABASE_ID);
physicalDatabase.setRoHost(TEST_RO_HOST);
when(physicalDatabasesService.searchInPhysicalDatabaseCache(any())).thenReturn(physicalDatabase);

final DatabaseCreateRequestV3 databaseCreateRequest = getDatabaseCreateRequestSample();
when(declarativeConfigRepository.findFirstByClassifierAndType(any(), any())).thenReturn(Optional.empty());
Mockito.when(databaseRolesService.getSupportedRoleFromRequest(any(DatabaseCreateRequestV3.class), any(), any())).thenReturn(ADMIN.toString());

final DatabaseRegistry database = getDatabaseSample();
database.setPhysicalDatabaseId(PHYSICAL_DATABASE_ID);
when(dBaaService.findDatabaseByClassifierAndType(any(), any(), anyBoolean())).thenReturn(database.getDatabaseRegistry().getFirst());
when(dBaaService.isModifiedFields(any(), any())).thenReturn(true);
when(dBaaService.detach(database)).thenReturn(database);
when(dBaaService.isModifiedFields(any(), any())).thenReturn(false);
DatabaseResponseV3 response = new DatabaseResponseV3SingleCP(database.getDatabaseRegistry().getFirst(), PHYSICAL_DATABASE_ID, ADMIN.toString());
when(dBaaService.processConnectionPropertiesV3(any(DatabaseRegistry.class), any())).thenReturn(response);
when(databaseRegistryDbaasRepository.findDatabaseRegistryById(any())).thenReturn(Optional.of(database.getDatabaseRegistry().getFirst()));
when(dBaaService.isAdapterExists(any(), any(), any())).thenReturn(true);
doThrow(new AdapterException("Internal Exception"))
.when(dBaaService).createDatabase(any(), any(), any());
given().auth().preemptive().basic("cluster-dba", "someDefaultPassword")
.pathParam(NAMESPACE_PARAMETER, TEST_NAMESPACE)
.contentType(MediaType.APPLICATION_JSON)
.body(objectMapper.writeValueAsString(databaseCreateRequest))
.when().put()
.then()
.statusCode(INTERNAL_SERVER_ERROR.getStatusCode())
.body("reason", is("Failed request to physical adapter"))
.body("message", is("An error occurred during the request to adapter: Internal Exception"));
}

@Test
void testCreateDatabaseControllerNamespace() throws Exception {
final DatabaseCreateRequestV3 databaseCreateRequest = getDatabaseCreateRequestSample();
Expand Down Expand Up @@ -1324,6 +1360,10 @@ private DatabaseRegistry getDatabaseSample() {
DatabaseRegistry databaseRegistry = new DatabaseRegistry();
databaseRegistry.setId(UUID.randomUUID());
databaseRegistry.setDatabase(database);
SortedMap<String, Object> classifier = new TreeMap<>();
classifier.put(NAMESPACE, NAMESPACE);
classifier.put(MICROSERVICE_NAME, MICROSERVICE_NAME);
databaseRegistry.setClassifier(classifier);
database.setResources(Collections.singletonList(new DbResource("database", TEST_NAME)));
ArrayList<DatabaseRegistry> dbrs = new ArrayList<>();
dbrs.add(databaseRegistry);
Expand Down
Loading