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

SO-5406 freeze retired resources #1007

Merged
merged 5 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -108,14 +108,6 @@ public void dot() throws Exception {
.isEqualTo(branchName);
}

@Test
public void tilde() throws Exception {
String branchName = "~1.0";
assertBranchCreate(branchName)
.extracting(RevisionBranch::getName)
.isEqualTo(branchName);
}

@Test(expected = BadRequestException.class)
public void percent() throws Exception {
String branchName = "%xy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static enum BranchState {
/**
* Allowed set of characters for a branch name.
*/
public static final String DEFAULT_ALLOWED_BRANCH_NAME_CHARACTER_SET = "a-zA-Z0-9.~_-";
public static final String DEFAULT_ALLOWED_BRANCH_NAME_CHARACTER_SET = "a-zA-Z0-9._-";

/**
* The maximum length of a branch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -40,6 +39,7 @@
import com.b2international.commons.exceptions.NotFoundException;
import com.b2international.commons.http.ExtendedLocale;
import com.b2international.commons.json.Json;
import com.b2international.snowowl.core.Resource;
import com.b2international.snowowl.core.ResourceURI;
import com.b2international.snowowl.core.branch.Branch;
import com.b2international.snowowl.core.codesystem.CodeSystem;
Expand Down Expand Up @@ -536,7 +536,7 @@ public void codesystem27_GetWithTimestamp() {
}

@Test
public void codeSystem28_SearchWithTimestamp() throws Exception {
public void codesystem28_SearchWithTimestamp() throws Exception {
assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs28_1"));
assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs28_2"));
assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs28_3"));
Expand All @@ -555,7 +555,7 @@ public void codeSystem28_SearchWithTimestamp() throws Exception {
}

@Test
public void codeSystem29_VersionWithReservedBranchName() throws Exception {
public void codesystem29_VersionWithReservedBranchName() throws Exception {
String codeSystemId = assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs29_1"));
assertVersionCreated(prepareVersionCreateRequestBody(CodeSystem.uri(codeSystemId), ResourceURI.HEAD, EffectiveTimes.today()))
.statusCode(400)
Expand All @@ -567,7 +567,35 @@ public void codeSystem29_VersionWithReservedBranchName() throws Exception {
.statusCode(400)
.body("message", containsString("Version 'NEXT' is a reserved alias or branch name."));
}


@Test
public void codesystem30_AllowMetadataUpdatesOnRetiredResources() throws Exception {
String codeSystemId = assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs30"));
assertCodeSystemUpdated(codeSystemId, Map.of(
"status", Resource.RETIRED_STATUS
));
assertCodeSystemGet(codeSystemId)
.statusCode(200)
.body("status", equalTo(Resource.RETIRED_STATUS));
assertCodeSystemUpdated(codeSystemId, Map.of(
"copyright", "MIT License"
));
assertCodeSystemGet(codeSystemId)
.statusCode(200)
.body("copyright", equalTo("MIT License"));
}

@Test
public void codesystem31_DisallowVersioningOfRetiredResources() throws Exception {
String codeSystemId = assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs30"));
assertCodeSystemUpdated(codeSystemId, Map.of(
"status", Resource.RETIRED_STATUS
));
assertVersionCreated(prepareVersionCreateRequestBody(CodeSystem.uri(codeSystemId), "v1", EffectiveTimes.today()))
.statusCode(400)
.body("message", containsString("Resource 'Code System cs30' cannot be versioned in its current status 'retired'."));
}

private long getCodeSystemCreatedAt(final String id) {
return assertCodeSystemGet(id)
.statusCode(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public abstract class Resource implements Serializable {

private static final long serialVersionUID = 1L;

// known retired status value from FHIR, TODO make it configurable when needed
public static final String RETIRED_STATUS = "retired";

/**
* @since 8.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 B2i Healthcare Pte Ltd, http://b2i.sg
* Copyright 2021-2022 B2i Healthcare Pte Ltd, http://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package com.b2international.snowowl.core.context;

import com.b2international.snowowl.core.Resource;
import com.b2international.snowowl.core.ResourceURI;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.TerminologyResource;
Expand All @@ -28,6 +29,7 @@ public final class DefaultTerminologyResourceContext extends DelegatingContext i
public DefaultTerminologyResourceContext(final ServiceProvider delegate, ResourceURI resourceUri, TerminologyResource resource) {
super(delegate);
bind(ResourceURI.class, resourceUri);
bind(Resource.class, resource);
bind(TerminologyResource.class, resource);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 B2i Healthcare Pte Ltd, http://b2i.sg
* Copyright 2021-2022 B2i Healthcare Pte Ltd, http://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,7 +53,8 @@ public TerminologyResourceRequest(final String toolingId, final String resourceP
this.toolingId = toolingId;
this.resourcePath = resourcePath;
if (resourcePath.startsWith(Branch.MAIN_PATH) && Strings.isNullOrEmpty(toolingId)) {
throw new BadRequestException("Reflective access ('repositoryId/path') to terminology resource content is not supported in this request builder.");
throw new BadRequestException("Reflective access ('repositoryId/path') to terminology resource content is not supported in this API.")
.withDeveloperMessage("No toolingId is specified on API level to ensure the correct reflective access to underlying terminology.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 B2i Healthcare Pte Ltd, http://b2i.sg
* Copyright 2021-2022 B2i Healthcare Pte Ltd, http://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,12 @@
*/
package com.b2international.snowowl.core.request;

import java.util.Set;

import com.b2international.snowowl.core.Resource;
import com.b2international.snowowl.core.context.TerminologyResourceContentRequestBuilder;
import com.b2international.snowowl.core.domain.BranchContext;
import com.b2international.snowowl.core.events.Request;

/**
* @since 8.0
Expand All @@ -24,9 +29,16 @@ public class TerminologyResourceCommitRequestBuilder
extends RepositoryCommitRequestBuilder<TerminologyResourceCommitRequestBuilder>
implements TerminologyResourceContentRequestBuilder<CommitResult> {

public static final Set<String> READ_ONLY_STATUSES = Set.of(Resource.RETIRED_STATUS);

@Override
public boolean snapshot() {
return false;
}

@Override
public Request<BranchContext, CommitResult> wrap(Request<BranchContext, CommitResult> req) {
return new TerminologyResourceStatusCheckRequest<>(TerminologyResourceContentRequestBuilder.super.wrap(req), READ_ONLY_STATUSES);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 B2i Healthcare Pte Ltd, http://b2i.sg
*
* Licensed 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 com.b2international.snowowl.core.request;

import java.util.Collection;
import java.util.SortedSet;

import com.b2international.commons.exceptions.BadRequestException;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.TerminologyResource;
import com.b2international.snowowl.core.events.DelegatingRequest;
import com.b2international.snowowl.core.events.Request;
import com.google.common.collect.ImmutableSortedSet;

/**
* @since 8.2
*
* @param <C>
* @param <T>
* @param <R>
*/
public final class TerminologyResourceStatusCheckRequest<C extends ServiceProvider, R> extends DelegatingRequest<C, C, R> {

private static final long serialVersionUID = 1L;
private final SortedSet<String> forbiddenStatuses;

public TerminologyResourceStatusCheckRequest(Request<C, R> next, Collection<String> forbiddenStatuses) {
super(next);
this.forbiddenStatuses = forbiddenStatuses == null ? ImmutableSortedSet.of() : ImmutableSortedSet.copyOf(forbiddenStatuses);
}

@Override
public R execute(C context) {
TerminologyResource resource = context.service(TerminologyResource.class);

if (forbiddenStatuses.contains(resource.getStatus())) {
throw new BadRequestException("Executing this request is forbidden on resources with one of the following states '%s'. Resource '%s' is in '%s' status.", this.forbiddenStatuses, resource.getTitle(), resource.getStatus());
} else {
return next(context);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public Boolean execute(RepositoryContext context) {
context.service(BranchNameValidator.class).checkName(version);

TerminologyResource resourceToVersion = resourcesById.get(resource);

if (TerminologyResourceCommitRequestBuilder.READ_ONLY_STATUSES.contains(resourceToVersion.getStatus())) {
throw new BadRequestException("Resource '%s' cannot be versioned in its current status '%s'.", resourceToVersion.getTitle(), resourceToVersion.getStatus());
}

// TODO resurrect or eliminate tooling dependencies
final List<TerminologyResource> resourcesToVersion = List.of(resourcesById.get(resource));
Expand Down