Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip returning API when context is null #12172

Merged
merged 3 commits into from
Oct 19, 2023
Merged
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,33 @@
/*
* Copyright (c) 2023, WSO2 LLC (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC licenses this file to you 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.wso2.carbon.apimgt.gateway.exception;

/**
* Exception class to handle data not found cases.
*/
public class DataNotFoundException extends Exception {

public DataNotFoundException(String message) {
super(message);
}

Check warning on line 28 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java#L27-L28

Added lines #L27 - L28 were not covered by tests

public DataNotFoundException(String message, Throwable cause) {
super(message, cause);
}

Check warning on line 32 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java#L31-L32

Added lines #L31 - L32 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsDataProvider;
import org.wso2.carbon.apimgt.common.analytics.collectors.impl.GenericRequestDataCollector;
import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants;
import org.wso2.carbon.apimgt.gateway.exception.DataNotFoundException;
import org.wso2.carbon.apimgt.gateway.handlers.analytics.Constants;
import org.wso2.carbon.apimgt.gateway.utils.WebhooksUtils;
import org.wso2.carbon.apimgt.impl.APIConstants;
Expand Down Expand Up @@ -73,7 +74,7 @@
WebhooksUtils.publishAnalyticsData(messageContext);
}
WebhooksUtils.persistData(requestBody, deliveryDataPersisRetries, APIConstants.Webhooks.DELIVERY_EVENT_TYPE);
} catch (InterruptedException | IOException e) {
} catch (InterruptedException | IOException | DataNotFoundException e) {

Check warning on line 77 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/DeliveryStatusUpdater.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/DeliveryStatusUpdater.java#L77

Added line #L77 was not covered by tests
log.error("Error while persisting delivery status", e);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.synapse.MessageContext;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import org.wso2.carbon.apimgt.gateway.exception.DataNotFoundException;
import org.wso2.carbon.apimgt.gateway.handlers.analytics.Constants;
import org.wso2.carbon.apimgt.gateway.utils.WebhooksUtils;
import org.wso2.carbon.apimgt.impl.APIConstants;
Expand Down Expand Up @@ -48,7 +49,7 @@
} else {
messageContext.setProperty(APIConstants.Webhooks.SUBSCRIBERS_COUNT_PROPERTY, 0);
}
} catch (URISyntaxException e) {
} catch (URISyntaxException | DataNotFoundException e) {

Check warning on line 52 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersLoader.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersLoader.java#L52

Added line #L52 was not covered by tests
handleException("Error while getting subscribers count", e, messageContext);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.synapse.rest.RESTConstants;
import org.apache.synapse.transport.passthru.PassThroughConstants;
import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants;
import org.wso2.carbon.apimgt.gateway.exception.DataNotFoundException;
import org.wso2.carbon.apimgt.gateway.handlers.analytics.Constants;
import org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityConstants;
import org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityUtils;
Expand Down Expand Up @@ -88,7 +89,7 @@
HttpResponse httpResponse = WebhooksUtils.persistData(jsonString, subscriptionDataPersisRetries,
APIConstants.Webhooks.SUBSCRIPTION_EVENT_TYPE);
handleResponse(httpResponse, messageContext);
} catch (InterruptedException | IOException e) {
} catch (InterruptedException | IOException | DataNotFoundException e) {

Check warning on line 92 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersPersistMediator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersPersistMediator.java#L92

Added line #L92 was not covered by tests
messageContext.setProperty(SynapseConstants.ERROR_CODE, HttpStatus.SC_INTERNAL_SERVER_ERROR);
messageContext.setProperty(SynapseConstants.ERROR_MESSAGE, "Error while persisting request");
messageContext.setProperty(SynapseConstants.ERROR_DETAIL, "Error while persisting request");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.wso2.carbon.apimgt.common.analytics.collectors.impl.GenericRequestDataCollector;
import org.wso2.carbon.apimgt.common.analytics.exceptions.AnalyticsException;
import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants;
import org.wso2.carbon.apimgt.gateway.exception.DataNotFoundException;
import org.wso2.carbon.apimgt.gateway.handlers.Utils;
import org.wso2.carbon.apimgt.gateway.handlers.streaming.webhook.WebhooksAnalyticsDataProvider;
import org.wso2.carbon.apimgt.gateway.handlers.throttling.APIThrottleConstants;
Expand Down Expand Up @@ -121,11 +122,15 @@
* @param messageContext the message context.
* @return the generated API Key.
*/
public static String generateAPIKey(MessageContext messageContext, String tenantDomain) {
public static String generateAPIKey(MessageContext messageContext, String tenantDomain)
throws DataNotFoundException {
String context = (String) messageContext.getProperty(RESTConstants.REST_API_CONTEXT);
String apiVersion = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
API api = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain).
getApiByContextAndVersion(context, apiVersion);
if (api == null) {
throw new DataNotFoundException("Error occurred when getting API information");

Check warning on line 132 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/WebhooksUtils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/WebhooksUtils.java#L132

Added line #L132 was not covered by tests
}
return api.getUuid();
}

Expand All @@ -136,7 +141,7 @@
* @return the list of subscribers.
*/
public static List<WebhooksDTO> getSubscribersListFromInMemoryMap(MessageContext messageContext)
throws URISyntaxException {
throws URISyntaxException, DataNotFoundException {
String tenantDomain = (String) messageContext.getProperty(APIConstants.TENANT_DOMAIN_INFO_PROPERTY);
String apiKey = WebhooksUtils.generateAPIKey(messageContext, tenantDomain);
String urlQueryParams = (String) ((Axis2MessageContext) messageContext).getAxis2MessageContext().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ public void testGetAPIasNull() {
Mockito.verify(subscriptionDataStore, Mockito.times(3)).getApiByContextAndVersion("/abc1", "1.0.0");
}

@Test
public void testGetAPIWithNullContext() {
API api = new API();
api.setApiName("api1");
api.setApiVersion("1.0.0");
api.setApiProvider("admin");
SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class);
Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
SubscriptionDataStore subscriptionDataStore = Mockito.mock(SubscriptionDataStore.class);
Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore("carbon.super")).thenReturn(
subscriptionDataStore);
Mockito.when(subscriptionDataStore.getApiByContextAndVersion(null, "1.0.0")).thenReturn(null);
}

@Test
public void testGetAPIasNullWhenTenantSubscriptionStoreNull() {
MessageContext messageContext = Mockito.mock(MessageContext.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@
@Override
public API getApiByContextAndVersion(String context, String version) {

if (context == null) {
if (log.isDebugEnabled()) {
log.debug("Cannot retrieve API information with null context");

Check warning on line 187 in components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java#L187

Added line #L187 was not covered by tests
}
return null;

Check warning on line 189 in components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java#L189

Added line #L189 was not covered by tests
}
String key = context + DELEM_PERIOD + version;
String synchronizeKey = "SubscriptionDataStoreImpl-API-" + key;
API api = apiMap.get(key);
Expand Down
Loading