Skip to content

Commit

Permalink
Merge branch 'backport-4154' into release-2.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Coduz committed Dec 17, 2024
2 parents 7a88c87 + 58b9aab commit 1b9f514
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.rest.errors;

import javax.inject.Inject;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.rest.model.errors.MaxNumberOfItemsReachedExceptionInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Provider
public class KapuaMaxNumberOfItemsReachedExceptionMapper implements ExceptionMapper<KapuaMaxNumberOfItemsReachedException> {

private static final Logger LOG = LoggerFactory.getLogger(KapuaMaxNumberOfItemsReachedExceptionMapper.class);
private final boolean showStackTrace;


@Inject
public KapuaMaxNumberOfItemsReachedExceptionMapper(ExceptionConfigurationProvider exceptionConfigurationProvider) {
this.showStackTrace = exceptionConfigurationProvider.showStackTrace();
}


@Override
public Response toResponse(KapuaMaxNumberOfItemsReachedException kapuaMaxNumberOfItemsReachedException) {
LOG.error(kapuaMaxNumberOfItemsReachedException.getMessage(), kapuaMaxNumberOfItemsReachedException);
return Response
.status(Status.FORBIDDEN)
.entity(new MaxNumberOfItemsReachedExceptionInfo(Status.FORBIDDEN.getStatusCode(), kapuaMaxNumberOfItemsReachedException, showStackTrace))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*******************************************************************************
* Copyright (c) 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.rest.model.errors;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;

@XmlRootElement(name = "maxNumberOfItemsReachedExceptionInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class MaxNumberOfItemsReachedExceptionInfo extends ExceptionInfo {

@XmlElement(name = "entityType")
private String entityType;


/**
* Constructor.
*
* @since 2.0.0
*/
protected MaxNumberOfItemsReachedExceptionInfo() {
super();
}


/**
* Constructor.
*
* @param httpStatusCode The http status code of the response containing this info
* @param kapuaMaxNumberOfItemsReachedException The root exception.
* @since 2.0.0
*/
public MaxNumberOfItemsReachedExceptionInfo(int httpStatusCode, KapuaMaxNumberOfItemsReachedException kapuaMaxNumberOfItemsReachedException, boolean showStackTrace) {
super(httpStatusCode, kapuaMaxNumberOfItemsReachedException, showStackTrace);
this.entityType = kapuaMaxNumberOfItemsReachedException.getEntityType();
}


/**
* Gets the {@link KapuaMaxNumberOfItemsReachedException#getEntityType()}.
*
* @return The {@link KapuaMaxNumberOfItemsReachedException#getEntityType()}.
* @since 2.0.0
*/
public String getEntityType() {
return entityType;
}

}
35 changes: 24 additions & 11 deletions qa/common/src/main/java/org/eclipse/kapua/qa/common/BasicSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@
import java.util.Map;
import javax.inject.Inject;

import com.google.common.base.Strings;
import com.google.inject.Singleton;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.DataTableType;
import io.cucumber.java.ParameterType;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.apache.shiro.SecurityUtils;
import org.eclipse.kapua.commons.crypto.setting.CryptoSettingKeys;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
Expand Down Expand Up @@ -61,6 +50,17 @@
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Strings;
import com.google.inject.Singleton;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.DataTableType;
import io.cucumber.java.ParameterType;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

@Singleton
public class BasicSteps extends TestBase {
Expand Down Expand Up @@ -88,6 +88,7 @@ public class BasicSteps extends TestBase {
private static final String LAST_ACCOUNT_ID = "LastAccountId";
private static final String LAST_USER_ID = "LastUserId";
private static final String EXCEPTION_NAME = "ExceptionName";
private static final String STATUS_CODE = "StatusCode";
private static final String EXCEPTION_CAUGHT = "ExceptionCaught";
private static final String ASSERT_ERROR_NAME = "AssertErrorName";
private static final String ASSERT_ERROR_CAUGHT = "AssertErrorCaught";
Expand Down Expand Up @@ -532,6 +533,12 @@ public void noExceptionCaught() {
Assert.assertFalse("An unexpected exception was raised!", exCaught);
}

@Then("Response status code match")
public void statusCodeMatch() {
final int statusCode = stepData.contains(STATUS_CODE) ? (int) stepData.get(STATUS_CODE) : -1;
Assert.assertEquals(statusCode, stepData.get("StatusCode"));
}

@Then("I count {int}")
public void checkCountResult(int num) {
Assert.assertEquals(num, stepData.getCount());
Expand Down Expand Up @@ -588,6 +595,12 @@ public void iExpectTheException(String name) {
stepData.put(EXCEPTION_NAME, name);
}

@And("I expect the response status code {int}")
public void iExpectTheStatusCode(int statusCode) {
stepData.put("StatusCodeExpected", true);
stepData.put(STATUS_CODE, statusCode);
}

@Then("An assertion error was thrown")
public void anAssertionErrorWasThrown() {
String assertErrorName = stepData.contains(ASSERT_ERROR_NAME) ? (String) stepData.get(ASSERT_ERROR_NAME) : "Unknown";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ Feature: Account Device Registry Service Integration Tests
And I create a device with name "Device3"
Then No exception was thrown
Given I expect the exception "KapuaMaxNumberOfItemsReachedException" with the text "*"
And I expect the response status code 403
When I create a device with name "Device4"
Then An exception was thrown
And Response status code match
And I logout

Scenario: Creating Devices Under Account That Does Not Allow Devices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ Feature: Account Group Service Integration Tests
And I create a group with name "Group2"
And I create a group with name "Group3"
Given I expect the exception "KapuaMaxNumberOfItemsReachedException" with the text "*"
And I expect the response status code 403
And I create a group with name "Group4"
Then An exception was thrown
And Response status code match
And I logout

Scenario: Creating Groups Under Account That Does Not Allow Groups
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ Feature: Account Job Service Integration Tests
And I create a job with the name "job3"
Then No exception was thrown
Given I expect the exception "KapuaMaxNumberOfItemsReachedException" with the text "*"
And I expect the response status code 403
When I create a job with the name "job4"
Then An exception was thrown
And Response status code match
And I logout

Scenario: Creating Jobs Under Account That Does Not Allow Jobs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ Feature: Account Role Service Integration Tests
And I create role "role2" in account "acc1"
And I create role "role3" in account "acc1"
Given I expect the exception "KapuaMaxNumberOfItemsReachedException" with the text "*"
And I expect the response status code 403
When I create role "role4" in account "acc1"
Then An exception was thrown
And Response status code match
And I logout

Scenario: Creating Roles Under Account That Does Not Allow Roles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,10 @@ Feature: Account Service Tests
| integer | maxNumberChildEntities | 0 |
Then I select account "acc1"
Given I expect the exception "KapuaMaxNumberOfItemsReachedException" with the text "*"
And I expect the response status code 403
And I create an account with name "acc11", organization name "acc11" and email address "acc11@org.com"
Then An exception was thrown
And Response status code match
Then I logout

Scenario: Creating Sub-accounts When InfiniteChildAccounts Is Set To True And maxNumberChildAccounts Is Set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ Feature: Account Tag Service Integration Tests
And I create tag with name "tag2" without description
And I create tag with name "tag3" without description
Given I expect the exception "KapuaMaxNumberOfItemsReachedException" with the text "*"
And I expect the response status code 403
When I create tag with name "tag4" without description
Then An exception was thrown
And Response status code match
And I logout

Scenario: Creating Tags Under Account That Does Not Allow Tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ Feature: Account User Service Integration Tests
And I create user with name "user2"
And I create user with name "user3"
Given I expect the exception "KapuaMaxNumberOfItemsReachedException" with the text "*"
And I expect the response status code 403
When I create user with name "user4"
Then An exception was thrown
And Response status code match
And I logout

Scenario: Creating Users Under Account That Does Not Allow Users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.eclipse.kapua.commons.rest.model.errors.JobScopedEngineExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.JobStartingExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.JobStoppingExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.MaxNumberOfItemsReachedExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.MfaRequiredExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.SelfManagedOnlyExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.ServiceConfigurationLimitExceededExceptionInfo;
Expand Down Expand Up @@ -365,6 +366,7 @@ public JAXBContext getJAXBContext() throws KapuaException {
IllegalArgumentExceptionInfo.class,
IllegalNullArgumentExceptionInfo.class,
MfaRequiredExceptionInfo.class,
MaxNumberOfItemsReachedExceptionInfo.class,

// Jobs Exception Info
CleanJobDataExceptionInfo.class,
Expand Down

0 comments on commit 1b9f514

Please sign in to comment.