From 7b147427450f72bd8ab754ce9016225966bd7713 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Sat, 24 Feb 2024 23:21:04 +0700 Subject: [PATCH 1/5] Fix history displays irrelevant changes --- .../server/internal/admin/service/RepositoryService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryService.java b/server/src/main/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryService.java index e3673b66ea..acbee22b3d 100644 --- a/server/src/main/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryService.java +++ b/server/src/main/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryService.java @@ -177,10 +177,11 @@ public CompletionStage> getHistory(@Param String projectName, @Param String path, @Param @Default("-1") String from, @Param @Default("1") String to) { + final String pathPattern = "/".equals(path) ? "/**" : path + "/**"; return projectApiManager.getProject(projectName).repos().get(repoName) .history(new Revision(from), new Revision(to), - path + "**") + pathPattern) .thenApply(commits -> commits.stream() .map(DtoConverter::convert) .collect(toList())); From 7706522c3205f9c2abb8b1bd9f582f7015ab16f7 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Sat, 24 Feb 2024 23:28:34 +0700 Subject: [PATCH 2/5] Fix typo --- .../server/internal/admin/service/RepositoryService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryService.java b/server/src/main/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryService.java index acbee22b3d..a8c8aed5df 100644 --- a/server/src/main/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryService.java +++ b/server/src/main/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryService.java @@ -181,7 +181,7 @@ public CompletionStage> getHistory(@Param String projectName, return projectApiManager.getProject(projectName).repos().get(repoName) .history(new Revision(from), new Revision(to), - pathPattern) + pathPattern) .thenApply(commits -> commits.stream() .map(DtoConverter::convert) .collect(toList())); From ec25153f233e3a8e4d5871c9a76ead28ecb1bd06 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Wed, 28 Feb 2024 22:10:36 +0700 Subject: [PATCH 3/5] Add draft test --- .../internal/api/v1/HttpApiV1Constants.java | 2 ++ .../admin/service/RepositoryServiceTest.java | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java diff --git a/common/src/main/java/com/linecorp/centraldogma/internal/api/v1/HttpApiV1Constants.java b/common/src/main/java/com/linecorp/centraldogma/internal/api/v1/HttpApiV1Constants.java index 78c2e5c4be..27ddf7b62a 100644 --- a/common/src/main/java/com/linecorp/centraldogma/internal/api/v1/HttpApiV1Constants.java +++ b/common/src/main/java/com/linecorp/centraldogma/internal/api/v1/HttpApiV1Constants.java @@ -30,6 +30,8 @@ public final class HttpApiV1Constants { public static final String PROJECTS_PREFIX = API_V1_PATH_PREFIX + PROJECTS; + public static final String PROJECTS_VO_PREFIX = API_V0_PATH_PREFIX + PROJECTS; + public static final String REPOS = "/repos"; public static final String COMMITS = "/commits"; diff --git a/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java b/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java new file mode 100644 index 0000000000..69ee678300 --- /dev/null +++ b/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java @@ -0,0 +1,36 @@ +package com.linecorp.centraldogma.server.internal.admin.service; + +import com.fasterxml.jackson.databind.JsonNode; +import com.linecorp.armeria.client.WebClient; +import com.linecorp.armeria.client.WebClientBuilder; +import com.linecorp.armeria.common.AggregatedHttpResponse; +import com.linecorp.armeria.common.HttpHeaderNames; +import com.linecorp.centraldogma.internal.Jackson; +import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension; +import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule; +import org.junit.ClassRule; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.*; + +class RepositoryServiceTest { + + @RegisterExtension + static final CentralDogmaExtension dogma = new CentralDogmaExtension() { + @Override + protected void configureHttpClient(WebClientBuilder builder) { + builder.addHeader(HttpHeaderNames.AUTHORIZATION, "Bearer anonymous"); + } + }; + + @Test + void getUsersInfo() throws IOException { + final WebClient client = dogma.httpClient(); + final AggregatedHttpResponse userInfo = client.get("/api/v0/users/me").aggregate().join(); + final JsonNode jsonNode = Jackson.readTree(userInfo.contentUtf8()); + assertThat(jsonNode.get("login").asText()).isEqualTo("admin@localhost.localdomain"); + } +} From b97bc1b760c0941249163a23c934b479c2622260 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Thu, 7 Mar 2024 22:27:53 +0700 Subject: [PATCH 4/5] Add test --- .../internal/api/v1/HttpApiV1Constants.java | 2 + .../admin/service/RepositoryServiceTest.java | 139 ++++++++++++++++-- 2 files changed, 129 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/com/linecorp/centraldogma/internal/api/v1/HttpApiV1Constants.java b/common/src/main/java/com/linecorp/centraldogma/internal/api/v1/HttpApiV1Constants.java index 27ddf7b62a..565d095b04 100644 --- a/common/src/main/java/com/linecorp/centraldogma/internal/api/v1/HttpApiV1Constants.java +++ b/common/src/main/java/com/linecorp/centraldogma/internal/api/v1/HttpApiV1Constants.java @@ -34,6 +34,8 @@ public final class HttpApiV1Constants { public static final String REPOS = "/repos"; + public static final String REPOSITORIES = "/repositories"; + public static final String COMMITS = "/commits"; public static final String COMPARE = "/compare"; diff --git a/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java b/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java index 69ee678300..c5f47ba96f 100644 --- a/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java +++ b/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java @@ -1,36 +1,151 @@ +/* + * Copyright 2024 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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.linecorp.centraldogma.server.internal.admin.service; +import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.PROJECTS_PREFIX; +import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.PROJECTS_VO_PREFIX; +import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.REPOS; +import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.REPOSITORIES; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.util.stream.StreamSupport; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + import com.fasterxml.jackson.databind.JsonNode; + import com.linecorp.armeria.client.WebClient; import com.linecorp.armeria.client.WebClientBuilder; import com.linecorp.armeria.common.AggregatedHttpResponse; import com.linecorp.armeria.common.HttpHeaderNames; +import com.linecorp.armeria.common.HttpMethod; +import com.linecorp.armeria.common.HttpStatus; +import com.linecorp.armeria.common.MediaType; +import com.linecorp.armeria.common.RequestHeaders; +import com.linecorp.armeria.common.ResponseHeaders; import com.linecorp.centraldogma.internal.Jackson; +import com.linecorp.centraldogma.server.CentralDogmaBuilder; import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension; -import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule; -import org.junit.ClassRule; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -import java.io.IOException; - -import static org.assertj.core.api.Assertions.*; class RepositoryServiceTest { @RegisterExtension static final CentralDogmaExtension dogma = new CentralDogmaExtension() { + @Override + protected void configure(CentralDogmaBuilder builder) { + builder.webAppEnabled(true); + } + @Override protected void configureHttpClient(WebClientBuilder builder) { builder.addHeader(HttpHeaderNames.AUTHORIZATION, "Bearer anonymous"); } }; + private static final String REPOS_PREFIX = PROJECTS_PREFIX + "/myPro" + REPOS; + private static final String REPOSITORIES_PREFIX = PROJECTS_VO_PREFIX + "/myPro" + REPOSITORIES; + private static final String FILES_PATH = "/files/revisions/head"; + + @BeforeAll + static void setUp() { + createProject(dogma); + } + @Test - void getUsersInfo() throws IOException { + void getHistory() throws IOException { + final WebClient client = dogma.httpClient(); + final String repoName = "myRepo"; + final AggregatedHttpResponse repoResponse = createRepository(client, repoName); + final ResponseHeaders repoResponseHeaders = ResponseHeaders.of(repoResponse.headers()); + assertThat(repoResponseHeaders.status()).isEqualTo(HttpStatus.CREATED); + + final String repoNamePrefix = "/myRepo"; + final AggregatedHttpResponse fileResponse1 = createFile(client, + repoNamePrefix, + "/foo/bar.txt", + "bar.txt", + "Bar"); + final ResponseHeaders fileResponseHeader1 = ResponseHeaders.of(fileResponse1.headers()); + assertThat(fileResponseHeader1.status()).isEqualTo(HttpStatus.CREATED); + + final AggregatedHttpResponse fileResponse2 = createFile(client, + repoNamePrefix, + "/foo2/bar2.txt", + "bar2.txt", + "Bar 2"); + final ResponseHeaders file2ResponseHeaders = ResponseHeaders.of(fileResponse2.headers()); + assertThat(file2ResponseHeaders.status()).isEqualTo(HttpStatus.CREATED); + + final AggregatedHttpResponse historyResponse1 = getHistory(client, repoNamePrefix, "/foo"); + final JsonNode fooHistory = Jackson.readTree(historyResponse1.contentUtf8()); + assertThat(fooHistory.get(0).get("summary").asText()).isEqualTo("Add /foo/bar.txt"); + final boolean containsUndesiredString = StreamSupport.stream(fooHistory.spliterator(), false) + .anyMatch(node -> "Add /foo2/bar2.txt".equals(node.get("summary").asText())); + assertThat(containsUndesiredString).isFalse(); + } + + private static AggregatedHttpResponse getHistory(WebClient client, String repoName, String childRepoName) { + final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET, + REPOSITORIES_PREFIX + repoName + "/history" + childRepoName + "?from=head&to=1", + HttpHeaderNames.CONTENT_TYPE, MediaType.JSON); + return client.execute(headers).aggregate().join(); + } + + private static AggregatedHttpResponse createRepository(WebClient client, String repoName) { + final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, REPOS_PREFIX, + HttpHeaderNames.CONTENT_TYPE, MediaType.JSON); + final String body = "{\"name\": \"" + repoName + "\"}"; + return client.execute(headers, body).aggregate().join(); + } + + private static AggregatedHttpResponse createFile(WebClient client, + String repoName, + String path, + String fileName, + String content) { + final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, + REPOSITORIES_PREFIX + repoName + FILES_PATH, + HttpHeaderNames.CONTENT_TYPE, MediaType.JSON); + final String body = "{\n" + + " \"file\": {\n" + + " \"name\": \"" + fileName + "\",\n" + + " \"type\": \"TEXT\",\n" + + " \"content\": \"" + content + "\",\n" + + " \"path\": \"" + path + "\"\n" + + " },\n" + + " \"commitMessage\": {\n" + + " \"summary\": \"Add " + path + "\",\n" + + " \"detail\": {\n" + + " \"content\": \"\",\n" + + " \"markup\": \"PLAINTEXT\"\n" + + " }\n" + + " }\n" + + '}'; + return client.execute(headers, body).aggregate().join(); + } + + private static void createProject(CentralDogmaExtension dogma) { + final String body = "{\"name\": \"myPro\"}"; + final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, PROJECTS_PREFIX, + HttpHeaderNames.CONTENT_TYPE, MediaType.JSON); final WebClient client = dogma.httpClient(); - final AggregatedHttpResponse userInfo = client.get("/api/v0/users/me").aggregate().join(); - final JsonNode jsonNode = Jackson.readTree(userInfo.contentUtf8()); - assertThat(jsonNode.get("login").asText()).isEqualTo("admin@localhost.localdomain"); + client.execute(headers, body).aggregate().join(); } } From 0eae756d27f5aa08622b03bed567aad1f55faac0 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Fri, 8 Mar 2024 22:15:41 +0700 Subject: [PATCH 5/5] Update the test --- .../admin/service/RepositoryServiceTest.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java b/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java index c5f47ba96f..8d1aa91d3b 100644 --- a/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java +++ b/server/src/test/java/com/linecorp/centraldogma/server/internal/admin/service/RepositoryServiceTest.java @@ -72,33 +72,44 @@ static void setUp() { void getHistory() throws IOException { final WebClient client = dogma.httpClient(); final String repoName = "myRepo"; - final AggregatedHttpResponse repoResponse = createRepository(client, repoName); - final ResponseHeaders repoResponseHeaders = ResponseHeaders.of(repoResponse.headers()); + final AggregatedHttpResponse myRepoCreatedResponse = createRepository(client, repoName); + final ResponseHeaders repoResponseHeaders = ResponseHeaders.of(myRepoCreatedResponse.headers()); assertThat(repoResponseHeaders.status()).isEqualTo(HttpStatus.CREATED); final String repoNamePrefix = "/myRepo"; - final AggregatedHttpResponse fileResponse1 = createFile(client, + final AggregatedHttpResponse barCreatedResponse = createFile(client, repoNamePrefix, "/foo/bar.txt", "bar.txt", "Bar"); - final ResponseHeaders fileResponseHeader1 = ResponseHeaders.of(fileResponse1.headers()); + final ResponseHeaders fileResponseHeader1 = ResponseHeaders.of(barCreatedResponse.headers()); assertThat(fileResponseHeader1.status()).isEqualTo(HttpStatus.CREATED); - final AggregatedHttpResponse fileResponse2 = createFile(client, + final AggregatedHttpResponse bar2Created = createFile(client, repoNamePrefix, "/foo2/bar2.txt", "bar2.txt", "Bar 2"); - final ResponseHeaders file2ResponseHeaders = ResponseHeaders.of(fileResponse2.headers()); + final ResponseHeaders file2ResponseHeaders = ResponseHeaders.of(bar2Created.headers()); assertThat(file2ResponseHeaders.status()).isEqualTo(HttpStatus.CREATED); - final AggregatedHttpResponse historyResponse1 = getHistory(client, repoNamePrefix, "/foo"); - final JsonNode fooHistory = Jackson.readTree(historyResponse1.contentUtf8()); + final AggregatedHttpResponse myRepoHistoryResponse = getHistory(client, repoNamePrefix, "/"); + final JsonNode myRepoHistory = Jackson.readTree(myRepoHistoryResponse.contentUtf8()); + assertThat(myRepoHistory.size()).isEqualTo(3); + assertThat(myRepoHistory.get(0).get("summary").asText()).isEqualTo("Add /foo2/bar2.txt"); + assertThat(myRepoHistory.get(1).get("summary").asText()).isEqualTo("Add /foo/bar.txt"); + assertThat(myRepoHistory.get(2).get("summary").asText()).isEqualTo("Create a new repository"); + + final AggregatedHttpResponse fooHistoryResponse = getHistory(client, repoNamePrefix, "/foo"); + final JsonNode fooHistory = Jackson.readTree(fooHistoryResponse.contentUtf8()); assertThat(fooHistory.get(0).get("summary").asText()).isEqualTo("Add /foo/bar.txt"); final boolean containsUndesiredString = StreamSupport.stream(fooHistory.spliterator(), false) .anyMatch(node -> "Add /foo2/bar2.txt".equals(node.get("summary").asText())); assertThat(containsUndesiredString).isFalse(); + + final AggregatedHttpResponse foo2HistoryResponse = getHistory(client, repoNamePrefix, "/foo2"); + final JsonNode foo2History = Jackson.readTree(foo2HistoryResponse.contentUtf8()); + assertThat(foo2History.get(0).get("summary").asText()).isEqualTo("Add /foo2/bar2.txt"); } private static AggregatedHttpResponse getHistory(WebClient client, String repoName, String childRepoName) {