Skip to content

Commit

Permalink
Change to return CentralDogmaRepository from client.createRepository (#…
Browse files Browse the repository at this point in the history
…662)

Motivation:
We introduced `CentralDogmaRepository` class in #651 and it would be nice if we return it from
`centralDogma.createRepository(...)` so users can chain the next call.
```java
dogma.createRepository("foo", "bar)
     .join()
     .commit(...)
     .push();
```

Modifications:
- Change to return `CentralDogmaRepository` from `centralDogma.createRepository(...)` and `centralDogma.unremoveRepository(...).
- Fix flaky test in `WatchTest`.

Result:
- (Breaking) `createRepository` and `unremoveRepository` from `CentralDogma` now returns `CentralDogmaRepository`.
  • Loading branch information
minwoox authored Jan 6, 2022
1 parent e93bc5a commit 3642633
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.linecorp.armeria.common.thrift.ThriftFuture;
import com.linecorp.armeria.common.util.Exceptions;
import com.linecorp.centraldogma.client.AbstractCentralDogma;
import com.linecorp.centraldogma.client.CentralDogmaRepository;
import com.linecorp.centraldogma.client.RepositoryInfo;
import com.linecorp.centraldogma.common.Author;
import com.linecorp.centraldogma.common.CentralDogmaException;
Expand Down Expand Up @@ -101,34 +102,26 @@ final class LegacyCentralDogma extends AbstractCentralDogma {

@Override
public CompletableFuture<Void> createProject(String projectName) {
return run(callback -> {
validateProjectName(projectName);
client.createProject(projectName, callback);
});
validateProjectName(projectName);
return run(callback -> client.createProject(projectName, callback));
}

@Override
public CompletableFuture<Void> removeProject(String projectName) {
return run(callback -> {
validateProjectName(projectName);
client.removeProject(projectName, callback);
});
validateProjectName(projectName);
return run(callback -> client.removeProject(projectName, callback));
}

@Override
public CompletableFuture<Void> purgeProject(String projectName) {
return run(callback -> {
validateProjectName(projectName);
client.purgeProject(projectName, callback);
});
validateProjectName(projectName);
return run(callback -> client.purgeProject(projectName, callback));
}

@Override
public CompletableFuture<Void> unremoveProject(String projectName) {
return run(callback -> {
validateProjectName(projectName);
client.unremoveProject(projectName, callback);
});
validateProjectName(projectName);
return run(callback -> client.unremoveProject(projectName, callback));
}

@Override
Expand All @@ -143,35 +136,31 @@ public CompletableFuture<Set<String>> listRemovedProjects() {
}

@Override
public CompletableFuture<Void> createRepository(String projectName, String repositoryName) {
return run(callback -> {
validateProjectAndRepositoryName(projectName, repositoryName);
client.createRepository(projectName, repositoryName, callback);
});
public CompletableFuture<CentralDogmaRepository> createRepository(String projectName,
String repositoryName) {
validateProjectAndRepositoryName(projectName, repositoryName);
return run(callback -> client.createRepository(projectName, repositoryName, callback))
.thenApply(unused -> forRepo(projectName, repositoryName));
}

@Override
public CompletableFuture<Void> removeRepository(String projectName, String repositoryName) {
return run(callback -> {
validateProjectAndRepositoryName(projectName, repositoryName);
client.removeRepository(projectName, repositoryName, callback);
});
validateProjectAndRepositoryName(projectName, repositoryName);
return run(callback -> client.removeRepository(projectName, repositoryName, callback));
}

@Override
public CompletableFuture<Void> purgeRepository(String projectName, String repositoryName) {
return run(callback -> {
validateProjectAndRepositoryName(projectName, repositoryName);
client.purgeRepository(projectName, repositoryName, callback);
});
validateProjectAndRepositoryName(projectName, repositoryName);
return run(callback -> client.purgeRepository(projectName, repositoryName, callback));
}

@Override
public CompletableFuture<Void> unremoveRepository(String projectName, String repositoryName) {
public CompletableFuture<CentralDogmaRepository> unremoveRepository(String projectName,
String repositoryName) {
validateProjectAndRepositoryName(projectName, repositoryName);
return run(callback -> {
client.unremoveRepository(projectName, repositoryName, callback);
});
return run(callback -> client.unremoveRepository(projectName, repositoryName, callback))
.thenApply(unused -> forRepo(projectName, repositoryName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ void createRepository() throws Exception {
callback.onComplete(null);
return null;
}).when(iface).createRepository(anyString(), anyString(), any());
assertThat(client.createRepository("project", "repo").get()).isNull();
assertThat(client.createRepository("project", "repo").get())
.isEqualTo(client.forRepo("project", "repo"));
verify(iface).createRepository(eq("project"), eq("repo"), any());
}

Expand Down Expand Up @@ -190,7 +191,8 @@ void unremoveRepository() throws Exception {
callback.onComplete(null);
return null;
}).when(iface).unremoveRepository(anyString(), anyString(), any());
assertThat(client.unremoveRepository("project", "repo").get()).isNull();
assertThat(client.unremoveRepository("project", "repo").get())
.isEqualTo(client.forRepo("project", "repo"));
verify(iface).unremoveRepository(eq("project"), eq("repo"), any());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.common.util.TimeoutMode;
import com.linecorp.centraldogma.client.AbstractCentralDogma;
import com.linecorp.centraldogma.client.CentralDogmaRepository;
import com.linecorp.centraldogma.client.RepositoryInfo;
import com.linecorp.centraldogma.common.Author;
import com.linecorp.centraldogma.common.AuthorizationException;
Expand Down Expand Up @@ -234,7 +235,8 @@ public CompletableFuture<Set<String>> listRemovedProjects() {
}

@Override
public CompletableFuture<Void> createRepository(String projectName, String repositoryName) {
public CompletableFuture<CentralDogmaRepository> createRepository(String projectName,
String repositoryName) {
try {
validateProjectAndRepositoryName(projectName, repositoryName);

Expand All @@ -244,21 +246,19 @@ public CompletableFuture<Void> createRepository(String projectName, String repos

return client.execute(headers(HttpMethod.POST, path), toBytes(root))
.aggregate()
.thenApply(ArmeriaCentralDogma::createRepository);
.thenApply(res -> {
switch (res.status().code()) {
case 200:
case 201:
return forRepo(projectName, repositoryName);
}
return handleErrorResponse(res);
});
} catch (Exception e) {
return exceptionallyCompletedFuture(e);
}
}

private static Void createRepository(AggregatedHttpResponse res) {
switch (res.status().code()) {
case 200:
case 201:
return null;
}
return handleErrorResponse(res);
}

@Override
public CompletableFuture<Void> removeRepository(String projectName, String repositoryName) {
try {
Expand Down Expand Up @@ -304,26 +304,25 @@ private static Void handlePurgeResult(AggregatedHttpResponse res) {
}

@Override
public CompletableFuture<Void> unremoveRepository(String projectName, String repositoryName) {
public CompletableFuture<CentralDogmaRepository> unremoveRepository(String projectName,
String repositoryName) {
try {
validateProjectAndRepositoryName(projectName, repositoryName);
return client.execute(headers(HttpMethod.PATCH,
pathBuilder(projectName, repositoryName).toString()),
UNREMOVE_PATCH)
.aggregate()
.thenApply(ArmeriaCentralDogma::unremoveRepository);
.thenApply(res -> {
if (res.status().code() == 200) {
return forRepo(projectName, repositoryName);
}
return handleErrorResponse(res);
});
} catch (Exception e) {
return exceptionallyCompletedFuture(e);
}
}

private static Void unremoveRepository(AggregatedHttpResponse res) {
if (res.status().code() == 200) {
return null;
}
return handleErrorResponse(res);
}

@Override
public CompletableFuture<Map<String, RepositoryInfo>> listRepositories(String projectName) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class CentralDogmaRepositoryTest {
@Override
protected void scaffold(CentralDogma client) {
client.createProject("foo").join();
client.createRepository("foo", "bar").join();
client.forRepo("foo", "bar")
client.createRepository("foo", "bar")
.join()
.commit("commit2", ImmutableList.of(Change.ofJsonUpsert("/foo.json", "{ \"a\": \"b\" }")))
.push()
.join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class WatcherTest {
@Override
protected void scaffold(CentralDogma client) {
client.createProject("foo").join();
client.createRepository("foo", "bar").join();
client.forRepo("foo", "bar")
client.createRepository("foo", "bar")
.join()
.commit("Add baz.txt", Change.ofTextUpsert("/baz.txt", ""))
.push().join();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public interface CentralDogma {
/**
* Creates a repository.
*/
CompletableFuture<Void> createRepository(String projectName, String repositoryName);
CompletableFuture<CentralDogmaRepository> createRepository(String projectName, String repositoryName);

/**
* Removes a repository. A removed repository can be unremoved using
Expand All @@ -109,7 +109,7 @@ public interface CentralDogma {
/**
* Unremoves a repository.
*/
CompletableFuture<Void> unremoveRepository(String projectName, String repositoryName);
CompletableFuture<CentralDogmaRepository> unremoveRepository(String projectName, String repositoryName);

/**
* Retrieves the list of the repositories.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import static java.util.Objects.requireNonNull;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;

import com.linecorp.centraldogma.common.Change;
Expand Down Expand Up @@ -281,4 +283,33 @@ public WatcherRequest<Revision> watcher(PathPattern pathPattern) {
requireNonNull(pathPattern, "pathPattern");
return new WatcherRequest<>(this, pathPattern, blockingTaskExecutor);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CentralDogmaRepository)) {
return false;
}
final CentralDogmaRepository that = (CentralDogmaRepository) o;
return centralDogma == that.centralDogma &&
projectName.equals(that.projectName) && repositoryName.equals(that.repositoryName) &&
blockingTaskExecutor == that.blockingTaskExecutor;
}

@Override
public int hashCode() {
return Objects.hash(centralDogma, projectName, repositoryName, blockingTaskExecutor);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("centralDogma", centralDogma)
.add("projectName", projectName)
.add("repositoryName", repositoryName)
.add("blockingTaskExecutor", blockingTaskExecutor)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import com.linecorp.centraldogma.client.AbstractCentralDogma;
import com.linecorp.centraldogma.client.CentralDogma;
import com.linecorp.centraldogma.client.CentralDogmaRepository;
import com.linecorp.centraldogma.client.RepositoryInfo;
import com.linecorp.centraldogma.common.Author;
import com.linecorp.centraldogma.common.Change;
Expand Down Expand Up @@ -132,7 +133,8 @@ public CompletableFuture<Set<String>> listRemovedProjects() {
}

@Override
public CompletableFuture<Void> createRepository(String projectName, String repositoryName) {
public CompletableFuture<CentralDogmaRepository> createRepository(String projectName,
String repositoryName) {
return delegate.createRepository(projectName, repositoryName);
}

Expand All @@ -147,7 +149,8 @@ public CompletableFuture<Void> purgeRepository(String projectName, String reposi
}

@Override
public CompletableFuture<Void> unremoveRepository(String projectName, String repositoryName) {
public CompletableFuture<CentralDogmaRepository> unremoveRepository(String projectName,
String repositoryName) {
return delegate.unremoveRepository(projectName, repositoryName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class CentralDogmaEndpointGroupTest {
@Override
protected void scaffold(CentralDogma client) {
client.createProject("directory").join();
client.createRepository("directory", "my-service").join();
client.forRepo("directory", "my-service")
client.createRepository("directory", "my-service")
.join()
.commit("commit", Change.ofJsonUpsert("/endpoint.json", HOST_AND_PORT_LIST_JSON))
.push()
.join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension;

enum ClientType {
@SuppressWarnings("unused")
DEFAULT(CentralDogmaExtension::client),
LEGACY(CentralDogmaExtension::legacyClient);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class MergeFileTest {
@Override
protected void scaffold(CentralDogma client) {
client.createProject("myPro").join();
client.createRepository("myPro", "myRepo").join();
client.forRepo("myPro", "myRepo")
client.createRepository("myPro", "myRepo")
.join()
.commit("Initial files",
Change.ofJsonUpsert("/foo.json", "{ \"a\": \"bar\" }"),
Change.ofJsonUpsert("/foo1.json", "{ \"b\": \"baz\" }"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class PrematureClientFactoryCloseTest {
@Override
protected void scaffold(CentralDogma client) {
client.createProject("foo").join();
client.createRepository("foo", "bar").join();
client.forRepo("foo", "bar")
client.createRepository("foo", "bar")
.join()
.commit("Add baz.txt", Change.ofTextUpsert("/baz.txt", ""))
.push().join();
}
Expand Down
Loading

0 comments on commit 3642633

Please sign in to comment.