diff --git a/README.md b/README.md index adc5eebb..3a973a39 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ List of permissions: |------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `{"entity": "ADMIN", "action": "ACCESS"}` | Allows user to access and use `leto-modelizer-admin`. | | `{"entity": "PROJECT", "action": "CREATE"}` | Allows user to create a project in `leto-modelizer`. | +| `{"entity": "PROJECT_GIT", "action": "CREATE"}` | Allows user to import a project from git in `leto-modelizer`. | | `{"entity": "PROJECT_TEMPLATE", "action": "CREATE"}` | Allows user to create a project from template in `leto-modelizer`. | | `{"entity": "DIAGRAM", "action": "CREATE"}` | Allows user to create a diagram in `leto-modelizer`. | | `{"entity": "DIAGRAM", "action": "DELETE"}` | Allows user to delete a diagram in `leto-modelizer`. | diff --git a/src/main/java/com/ditrit/letomodelizerapi/persistence/function/JsonNodeToLibraryFunction.java b/src/main/java/com/ditrit/letomodelizerapi/persistence/function/JsonNodeToLibraryFunction.java index 26f5c691..3f34f47d 100644 --- a/src/main/java/com/ditrit/letomodelizerapi/persistence/function/JsonNodeToLibraryFunction.java +++ b/src/main/java/com/ditrit/letomodelizerapi/persistence/function/JsonNodeToLibraryFunction.java @@ -13,9 +13,37 @@ */ public class JsonNodeToLibraryFunction implements Function { + /** + * Library to set. + */ + private Library fromLibrary; + + /** + * Default constructor. + */ + public JsonNodeToLibraryFunction() { + this(null); + } + + /** + * Provide a library to use it as default value. + * All value of this library can be upgraded by values in json. + * @param fromLibrary Library to use as default. + */ + public JsonNodeToLibraryFunction(final Library fromLibrary) { + this.fromLibrary = fromLibrary; + } + @Override public Library apply(final JsonNode json) { - Library library = new Library(); + Library library = null; + + if (fromLibrary != null) { + library = fromLibrary; + } else { + library = new Library(); + } + library.setName(json.get("name").asText()); library.setVersion(json.get("version").asText()); library.setMaintainer(json.get("maintainer").asText()); diff --git a/src/main/java/com/ditrit/letomodelizerapi/service/LibraryServiceImpl.java b/src/main/java/com/ditrit/letomodelizerapi/service/LibraryServiceImpl.java index e5be10cf..52f5c153 100644 --- a/src/main/java/com/ditrit/letomodelizerapi/service/LibraryServiceImpl.java +++ b/src/main/java/com/ditrit/letomodelizerapi/service/LibraryServiceImpl.java @@ -33,7 +33,6 @@ import com.github.erosb.jsonsKema.ValidatorConfig; import jakarta.transaction.Transactional; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; @@ -190,18 +189,16 @@ public Library create(final LibraryRecord libraryRecord, final String login) thr throw new ApiException(ErrorType.UNAUTHORIZED_LIBRARY_URL, "url", libraryRecord.url()); } - AccessControl role = null; - if (StringUtils.isNotBlank(libraryRecord.role())) { - role = accessControlService.create(AccessControlType.ROLE, new AccessControlRecord(libraryRecord.role())); - accessControlService.associateUser(AccessControlType.ROLE, role.getId(), login); - } - String libraryUrl = libraryRecord.url().replace("index.json", ""); if (libraryRepository.existsByUrl(libraryUrl)) { throw new ApiException(ErrorType.ENTITY_ALREADY_EXISTS, "url", libraryUrl); } + AccessControl role = accessControlService.create(AccessControlType.ROLE, + new AccessControlRecord(libraryRecord.role())); + accessControlService.associateUser(AccessControlType.ROLE, role.getId(), login); + Library library = save(libraryUrl, null); permissionService.createLibraryPermissions(library, role); @@ -255,13 +252,15 @@ Library save(final String url, final UUID id) throws JsonProcessingException { // JsonProcessingException can't be thrown because we already validate json before. JsonNode libraryJson = new ObjectMapper().readTree(libraryValue); - Library library = new JsonNodeToLibraryFunction().apply(libraryJson); - library.setUrl(url); + Library library = null; if (id != null) { - library.setId(id); + library = libraryRepository.findById(id).orElse(null); } + library = new JsonNodeToLibraryFunction(library).apply(libraryJson); + library.setUrl(url); + library = libraryRepository.save(library); final UUID libraryId = library.getId(); diff --git a/src/main/resources/db/migration/V1_5_0__new_table_permissions.sql b/src/main/resources/db/migration/V1_5_0__new_table_permissions.sql index 7c10afa3..c89b5f87 100644 --- a/src/main/resources/db/migration/V1_5_0__new_table_permissions.sql +++ b/src/main/resources/db/migration/V1_5_0__new_table_permissions.sql @@ -1,4 +1,4 @@ -CREATE TYPE entity_type AS ENUM ('ADMIN', 'PROJECT', 'PROJECT_TEMPLATE', 'DIAGRAM', 'DIAGRAM_TEMPLATE', 'COMPONENT', 'COMPONENT_TEMPLATE', 'LIBRARY'); +CREATE TYPE entity_type AS ENUM ('ADMIN', 'PROJECT', 'PROJECT_GIT', 'PROJECT_TEMPLATE', 'DIAGRAM', 'DIAGRAM_TEMPLATE', 'COMPONENT', 'COMPONENT_TEMPLATE', 'LIBRARY'); CREATE TYPE action_type AS ENUM ('ACCESS', 'CREATE', 'DELETE', 'UPDATE'); CREATE TABLE IF NOT EXISTS permissions ( @@ -26,6 +26,7 @@ COMMENT ON COLUMN permissions.update_date IS 'Last update date of this row.'; INSERT INTO permissions(entity, action, lib_id) VALUES ('ADMIN', 'ACCESS', NULL), ('PROJECT', 'CREATE', NULL), +('PROJECT_GIT', 'CREATE', NULL), ('PROJECT_TEMPLATE', 'CREATE', NULL), ('DIAGRAM', 'CREATE', NULL), ('DIAGRAM', 'DELETE', NULL), diff --git a/src/main/resources/db/migration/V1_6_0__new_table_access_controls_permissions.sql b/src/main/resources/db/migration/V1_6_0__new_table_access_controls_permissions.sql index 0c0de479..4c0dfe91 100644 --- a/src/main/resources/db/migration/V1_6_0__new_table_access_controls_permissions.sql +++ b/src/main/resources/db/migration/V1_6_0__new_table_access_controls_permissions.sql @@ -24,4 +24,4 @@ SELECT (SELECT aco_id FROM access_controls WHERE name = 'ADMINISTRATOR'), per_id -- Add permission of all actions on leto-modelizer to Developer. INSERT INTO access_controls_permissions(aco_id, per_id) -SELECT (SELECT aco_id FROM access_controls WHERE name = 'DEVELOPER'), per_id FROM permissions WHERE entity in ('PROJECT', 'PROJECT_TEMPLATE', 'DIAGRAM', 'DIAGRAM_TEMPLATE', 'COMPONENT', 'COMPONENT_TEMPLATE'); +SELECT (SELECT aco_id FROM access_controls WHERE name = 'DEVELOPER'), per_id FROM permissions WHERE entity in ('PROJECT', 'PROJECT_GIT', 'PROJECT_TEMPLATE', 'DIAGRAM', 'DIAGRAM_TEMPLATE', 'COMPONENT', 'COMPONENT_TEMPLATE'); diff --git a/src/main/resources/index.html b/src/main/resources/index.html index a27097b9..ea8359cf 100644 --- a/src/main/resources/index.html +++ b/src/main/resources/index.html @@ -53,7 +53,7 @@

Authentication Success

-

You will be redirected in 5 second(s).

+

You will be redirected in 3 second(s).

@@ -62,7 +62,7 @@

Authentication Success

confettiNumber: 2000, }); - var timer = 5; + var timer = 3; var intervalId = setInterval(function () { if (timer <= 0) { clearInterval(intervalId); diff --git a/src/test/java/com/ditrit/letomodelizerapi/persistence/function/JsonNodeToLibraryFunctionTest.java b/src/test/java/com/ditrit/letomodelizerapi/persistence/function/JsonNodeToLibraryFunctionTest.java index 3439fafe..da1e4928 100644 --- a/src/test/java/com/ditrit/letomodelizerapi/persistence/function/JsonNodeToLibraryFunctionTest.java +++ b/src/test/java/com/ditrit/letomodelizerapi/persistence/function/JsonNodeToLibraryFunctionTest.java @@ -7,6 +7,8 @@ import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import java.util.UUID; + import static org.junit.jupiter.api.Assertions.assertEquals; @Tag("unit") @@ -58,4 +60,34 @@ void testApply() { assertEquals(expectedLibrary, mapper.apply(json)); } + + @Test + @DisplayName("Test apply: should keep default if it's not override") + void testApplyKeepValue() { + Library defaultLibrary = new Library(); + defaultLibrary.setId(UUID.randomUUID()); + defaultLibrary.setVersion("version"); + + JsonNodeToLibraryFunction mapper = new JsonNodeToLibraryFunction(defaultLibrary); + + Library expectedLibrary = new Library(); + + expectedLibrary.setId(defaultLibrary.getId()); + expectedLibrary.setName("name"); + expectedLibrary.setVersion("version"); + expectedLibrary.setMaintainer("maintainer"); + expectedLibrary.setDescription("description"); + expectedLibrary.setDocumentationUrl("documentationUrl"); + expectedLibrary.setIcon("icon"); + + ObjectNode json = JsonNodeFactory.instance.objectNode(); + json.put("name", "name"); + json.put("version", "version"); + json.put("maintainer", "maintainer"); + json.put("description", "description"); + json.put("documentationUrl", "documentationUrl"); + json.put("icon", "icon"); + + assertEquals(expectedLibrary, mapper.apply(json)); + } } diff --git a/src/test/java/com/ditrit/letomodelizerapi/service/LibraryServiceImplTest.java b/src/test/java/com/ditrit/letomodelizerapi/service/LibraryServiceImplTest.java index 93a2621d..e7c1e828 100644 --- a/src/test/java/com/ditrit/letomodelizerapi/service/LibraryServiceImplTest.java +++ b/src/test/java/com/ditrit/letomodelizerapi/service/LibraryServiceImplTest.java @@ -452,7 +452,7 @@ void testCreateWithAlreadyExistsUrl() throws JsonProcessingException { ApiException exception = null; try { - service.create(new LibraryRecord("http://localhost:8080/test/index.json", null), "login"); + service.create(new LibraryRecord("http://localhost:8080/test/index.json", "TEST"), "login"); } catch (ApiException e) { exception = e; } diff --git a/src/test/resources/features/Library.feature b/src/test/resources/features/Library.feature index 7beb44c9..408967e2 100644 --- a/src/test/resources/features/Library.feature +++ b/src/test/resources/features/Library.feature @@ -44,10 +44,12 @@ Feature: Library feature Scenario: Should return 400 on unknown library url Given I initialize the admin user + And I clean role "TEST" When I request "/libraries" with method "POST" with json - | key | value | - | url | http://[LIBRARY_HOST]/invalid/unknown/index.json | + | key | value | + | role | TEST | + | url | http://[LIBRARY_HOST]/invalid/unknown/index.json | Then I expect "400" as status code And I expect response field "message" is "Wrong field value." And I expect response field "code" is "206" @@ -65,10 +67,12 @@ Feature: Library feature Scenario: Should return 400 on invalid library json Given I initialize the admin user + And I clean role "TEST" When I request "/libraries" with method "POST" with json - | key | value | - | url | http://[LIBRARY_HOST]/invalid/simple/index.json | + | key | value | + | role | TEST | + | url | http://[LIBRARY_HOST]/invalid/simple/index.json | Then I expect "400" as status code And I expect response field "message" is "Index.json of library is invalid." And I expect response field "code" is "210" @@ -87,15 +91,19 @@ Feature: Library feature Scenario: Should return 400 on already exists library Given I initialize the admin user And I clean library "http://[LIBRARY_HOST]/valid/simple/" + And I clean role "TEST1" + And I clean role "TEST2" When I request "/libraries" with method "POST" with json - | key | value | - | url | http://[LIBRARY_HOST]/valid/simple/index.json | + | key | value | + | role | TEST1 | + | url | http://[LIBRARY_HOST]/valid/simple/index.json | Then I expect "201" as status code When I request "/libraries" with method "POST" with json - | key | value | - | url | http://[LIBRARY_HOST]/valid/simple/index.json | + | key | value | + | role | TEST2 | + | url | http://[LIBRARY_HOST]/valid/simple/index.json | Then I expect "400" as status code And I expect response field "message" is "Entity already exists." And I expect response field "code" is "208" @@ -105,6 +113,7 @@ Feature: Library feature Scenario: Should return 200 on a valid library creation Given I initialize the admin user And I clean library "http://[LIBRARY_HOST]/valid/simple/" + And I clean role "TEST" When I request "/libraries/validate" with method "POST" with body | value | type | @@ -113,8 +122,9 @@ Feature: Library feature # Library creation When I request "/libraries" with method "POST" with json - | key | value | - | url | http://[LIBRARY_HOST]/valid/simple/index.json | + | key | value | + | role | TEST | + | url | http://[LIBRARY_HOST]/valid/simple/index.json | Then I expect "201" as status code And I set response field "id" to context "libraryId"