Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
fix: check on multi subcontexts if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
aelamrani committed Jun 20, 2016
1 parent b519e3b commit f15fb21
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
Expand Down Expand Up @@ -130,17 +130,21 @@ private void checkContextPath(final String newContextPath) throws TechnicalExcep
checkContextPath(newContextPath, null);
}

private void checkContextPath(final String newContextPath, final String apiId) throws TechnicalException {
final int indexOfEndOfNewSubContextPath = newContextPath.indexOf('/', 1);
final String newSubContextPath = newContextPath.substring(0, indexOfEndOfNewSubContextPath == -1 ? newContextPath.length() : indexOfEndOfNewSubContextPath);
private void checkContextPath(String newContextPath, final String apiId) throws TechnicalException {
if (newContextPath.charAt(newContextPath.length() - 1) == '/') {
newContextPath = newContextPath.substring(0, newContextPath.length() - 1);
}

final int indexOfEndOfNewSubContextPath = newContextPath.lastIndexOf('/', 1);
final String newSubContextPath = newContextPath.substring(0, indexOfEndOfNewSubContextPath <= 0 ? newContextPath.length() : indexOfEndOfNewSubContextPath);

final boolean contextPathExists = apiRepository.findAll().stream()
.filter(api -> !api.getId().equals(apiId))
.anyMatch(api -> {
final String contextPath = convert(api).getProxy().getContextPath();
final int indexOfEndOfSubContextPath = contextPath.indexOf('/', 1);
final String subContextPath = contextPath.substring(0, indexOfEndOfSubContextPath == -1 ? contextPath.length() : indexOfEndOfSubContextPath);
return newSubContextPath.equals(subContextPath);
final int indexOfEndOfSubContextPath = contextPath.lastIndexOf('/', 1);
final String subContextPath = contextPath.substring(0, indexOfEndOfSubContextPath <= 0 ? contextPath.length() : indexOfEndOfSubContextPath);
return (subContextPath + '/').startsWith(newSubContextPath + '/') || (newSubContextPath + '/').startsWith(subContextPath + '/');
});
if (contextPathExists) {
throw new ApiContextPathAlreadyExistsException(newSubContextPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ public void shouldNotUpdateForUserBecauseSubContextPathExists2() throws Technica
testUpdateWithContextPath("/context", "/context/toto");
}

@Test
public void shouldNotCreateForUserBecauseSubContextPathExists3() throws TechnicalException {
testCreationWithContextPath("/products/toto/search", "/products/tata/search");
}

private void testUpdateWithContextPath(String existingContextPath, String contextPathToCreate) throws TechnicalException {
when(apiRepository.findById(API_ID)).thenReturn(Optional.of(api));
when(apiRepository.update(any())).thenReturn(api);
Expand Down

0 comments on commit f15fb21

Please sign in to comment.