Skip to content

Fix editing schema #672

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

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Fix index API by @dennisvang in #637 (backward incompatible)
- Fix listing metadata records after publish by @MarekSuchanek in #658
- Fix saving settings by @MarekSuchanek in #657
- Fix saving metadata schema (draft and publish) by @MarekSuchanek in #672

## [1.17.2]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public MetadataSchemaDraftDTO toDraftDTO(
MetadataSchemaVersion draft
) {
return MetadataSchemaDraftDTO.builder()
.uuid(draft.getUuid())
.uuid(draft.getSchema().getUuid())
.name(draft.getName())
.description(draft.getDescription())
.abstractSchema(draft.isAbstractSchema())
Expand Down Expand Up @@ -223,14 +223,14 @@ public MetadataSchemaVersion fromUpdateDTO(MetadataSchemaVersion schema, Metadat
}

public MetadataSchemaVersion toDraft(MetadataSchemaVersion schema) {
return MetadataSchemaVersion.builder()
.uuid(schema.getUuid())
.name(schema.getName())
.description(schema.getDescription())
.abstractSchema(schema.isAbstractSchema())
.definition(schema.getDefinition())
.suggestedResourceName(schema.getSuggestedResourceName())
.suggestedUrlPrefix(schema.getSuggestedUrlPrefix())
return schema.toBuilder()
.origin(null)
.importedFrom(null)
.previousVersion(schema)
.state(MetadataSchemaState.DRAFT)
.type(MetadataSchemaType.CUSTOM)
.createdAt(Instant.now())
.updatedAt(Instant.now())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,22 @@ public Optional<MetadataSchemaDraftDTO> updateSchemaDraft(UUID uuid, MetadataSch
final MetadataSchemaVersion baseDraft;
// Check if present
if (oDraft.isPresent()) {
// Continue with existing draft
baseDraft = oDraft.get();
}
else {
if (oLatest.isEmpty()) {
return empty();
}
// There is no draft yet, so we create a shallow copy of the
// latest schema version to use as the basis for a new draft
baseDraft = metadataSchemaMapper.toDraft(oLatest.get());
}
// Validate
// Validate schema extensions
metadataSchemaValidator.validateAllExist(reqDto.getExtendsSchemaUuids());
metadataSchemaValidator.validateNoExtendsCycle(uuid, reqDto.getExtendsSchemaUuids());
final List<MetadataSchema> extendedSchemas = getAll(reqDto.getExtendsSchemaUuids());
// Save
// Save draft version
final MetadataSchemaVersion updatedDraft = versionRepository.saveAndFlush(
metadataSchemaMapper.fromChangeDTO(reqDto, baseDraft)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static void compare(MetadataSchemaVersion entity, MetadataSchemaVersionDT
}

public static void compare(MetadataSchemaVersion entity, MetadataSchemaDraftDTO dto) {
assertThat(dto.getUuid(), is(equalTo(entity.getUuid())));
assertThat(dto.getUuid(), is(equalTo(entity.getSchema().getUuid())));
assertThat(dto.getName(), is(equalTo(entity.getName())));
assertThat(dto.getDefinition(), is(equalTo(entity.getDefinition())));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* The MIT License
* Copyright © 2016-2024 FAIR Data Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.fairdatapoint.service.schema;

import org.fairdatapoint.Profiles;
import org.fairdatapoint.acceptance.schema.Common;
import org.fairdatapoint.api.dto.schema.MetadataSchemaChangeDTO;
import org.fairdatapoint.api.dto.schema.MetadataSchemaDraftDTO;
import org.fairdatapoint.database.db.repository.MetadataSchemaVersionRepository;
import org.fairdatapoint.entity.schema.MetadataSchemaVersion;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;


@ActiveProfiles(Profiles.TESTING)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@SpringBootTest(properties = {"spring.main.allow-bean-definition-overriding=true"})
public class MetadataSchemaServiceTests {
@Autowired
private MetadataSchemaService service;

@Autowired
private MetadataSchemaVersionRepository versionRepository;

@Test
@WithMockUser(roles = {"ADMIN"})
public void testChangeExistingSchema() throws Exception {
// reproduces #660

// GIVEN: existing schema version (without existing draft) and changes to be made
UUID schemaUuid = Common.SCHEMA_SIMPLE_UUID;
MetadataSchemaChangeDTO changeDTO = MetadataSchemaChangeDTO.builder()
.abstractSchema(false)
.definition("")
.description("")
.extendsSchemaUuids(List.of())
.name("test")
.suggestedResourceName(null)
.suggestedUrlPrefix(null)
.build();

// WHEN: changes are applied to existing schema
MetadataSchemaDraftDTO updatedDraft = service.updateSchemaDraft(schemaUuid, changeDTO).orElseThrow();

// THEN: draft schema is created with updated properties
MetadataSchemaVersion draftVersion = versionRepository.getDraftBySchemaUuid(schemaUuid).orElseThrow();
assertEquals(updatedDraft.getName(), changeDTO.getName());
assertEquals(updatedDraft.getName(), draftVersion.getName());
}
}
Loading