Skip to content

Commit

Permalink
add environment branching (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariobodemann authored Mar 4, 2019
1 parent f33e3bb commit 03ff887
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 20 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Version [3.3.0] - (2019-03-04)
- add environment branching

## Version [3.2.4] - (2019-01-28)
- add marks to rich text nodes parsing

Expand Down Expand Up @@ -161,7 +164,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## Version 0.9.0 - 2014-11-18
Initial release.

[unreleased]: https://github.com/contentful/contentful-management.java/compare/cma-sdk-3.2.4...HEAD
[unreleased]: https://github.com/contentful/contentful-management.java/compare/cma-sdk-3.3.0...HEAD
[3.3.0]: https://github.com/contentful/contentful-management.java/compare/v3.2.4...v3.3.0
[3.2.4]: https://github.com/contentful/contentful-management.java/compare/v3.2.3...v3.2.4
[3.2.3]: https://github.com/contentful/contentful-management.java/compare/v3.2.2...v3.2.3
[3.2.2]: https://github.com/contentful/contentful-management.java/compare/v3.2.1...v3.2.2
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ Install the Contentful dependency:
<dependency>
<groupId>com.contentful.java</groupId>
<artifactId>cma-sdk</artifactId>
<version>3.2.4</version>
<version>3.3.0</version>
</dependency>
```

* _Gradle_
```groovy
compile 'com.contentful.java:cma-sdk:3.2.4'
compile 'com.contentful.java:cma-sdk:3.3.0'
```

This SDK requires Java 8 (or higher version).
Expand Down Expand Up @@ -261,14 +261,14 @@ Snapshots of the development version are available through

```groovy
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
compile 'com.contentful.java:cma-sdk:3.2.4-SNAPSHOT'
compile 'com.contentful.java:cma-sdk:3.3.1-SNAPSHOT'
```

* [jitpack.io](https://jitpack.io/#contentful/contentful-management.java/master-SNAPSHOT):

```groovy
maven { url 'https://jitpack.io' }
compile 'com.github.contentful:contentful.java:cma-sdk-3.2.4-SNAPSHOT'
compile 'com.github.contentful:contentful.java:cma-sdk-3.3.1-SNAPSHOT'
```

Documentation
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.contentful.java</groupId>
<artifactId>cma-sdk</artifactId>
<version>3.2.5-SNAPSHOT</version>
<version>3.3.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>cma-sdk</name>
Expand Down
90 changes: 79 additions & 11 deletions src/main/java/com/contentful/java/cma/ModuleEnvironments.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import com.contentful.java.cma.model.CMAArray;
import com.contentful.java.cma.model.CMAEnvironment;
import com.contentful.java.cma.model.CMASystem;
import retrofit2.Retrofit;

import java.util.concurrent.Executor;

import retrofit2.Retrofit;

/**
* Environments Module.
*/
Expand All @@ -50,7 +49,8 @@ public ModuleEnvironments(
this.async = new Async();
}

@Override protected ServiceEnvironments createService(Retrofit retrofit) {
@Override
protected ServiceEnvironments createService(Retrofit retrofit) {
return retrofit.create(ServiceEnvironments.class);
}

Expand Down Expand Up @@ -98,6 +98,41 @@ public CMAEnvironment create(String spaceId, CMAEnvironment environment) {
}
}

/**
* Create an environment based on the content of another environment.
* <p>
* This method will override the configuration specified through
* {@link CMAClient.Builder#setSpaceId(String)}.
*
* @param sourceEnvironment the environment to be taken as a source of branching
* @param newEnvironment the environment to be created, based on the source.
* @return {@link CMAEnvironment} result instance
* @throws IllegalArgumentException if sourceEnvironment is null.
* @throws IllegalArgumentException if newEnvironment is null.
* @throws IllegalArgumentException if the space of the source environment is not set.
*/
public CMAEnvironment branch(CMAEnvironment sourceEnvironment, CMAEnvironment newEnvironment) {
assertNotNull(sourceEnvironment, "sourceEnvironment");
assertNotNull(newEnvironment, "newEnvironment");
assertNotNull(sourceEnvironment.getSpaceId(), "sourceEnvironment.spaceId");

final String environmentId = newEnvironment.getId();

final CMASystem system = newEnvironment.getSystem();
newEnvironment.setSystem(null);

try {
if (environmentId == null) {
return service.branch(spaceId, sourceEnvironment.getId(), newEnvironment).blockingFirst();
} else {
return service.branch(spaceId, sourceEnvironment.getId(), environmentId, newEnvironment)
.blockingFirst();
}
} finally {
newEnvironment.setSystem(system);
}
}

/**
* Delete an environment.
*
Expand Down Expand Up @@ -234,7 +269,8 @@ public CMACallback<CMAEnvironment> create(
final CMAEnvironment environment,
CMACallback<CMAEnvironment> callback) {
return defer(new DefFunc<CMAEnvironment>() {
@Override CMAEnvironment method() {
@Override
CMAEnvironment method() {
return ModuleEnvironments.this.create(environment);
}
}, callback);
Expand All @@ -258,12 +294,38 @@ public CMACallback<CMAEnvironment> create(
final CMAEnvironment environment,
CMACallback<CMAEnvironment> callback) {
return defer(new DefFunc<CMAEnvironment>() {
@Override CMAEnvironment method() {
@Override
CMAEnvironment method() {
return ModuleEnvironments.this.create(spaceId, environment);
}
}, callback);
}

/**
* Create an environment using a source environment.
* <p>
* This method will override the configuration specified through
* {@link CMAClient.Builder#setSpaceId(String)}.
*
* @param sourceEnvironment base of the created environment.
* @param newEnvironment to be created environment.
* @param callback Callback
* @return the given CMACallback instance
* @throws IllegalArgumentException if space id is null.
* @throws IllegalArgumentException if environment is null.
*/
public CMACallback<CMAEnvironment> branch(
final CMAEnvironment sourceEnvironment,
final CMAEnvironment newEnvironment,
CMACallback<CMAEnvironment> callback) {
return defer(new DefFunc<CMAEnvironment>() {
@Override
CMAEnvironment method() {
return ModuleEnvironments.this.branch(sourceEnvironment, newEnvironment);
}
}, callback);
}

/**
* Delete an environment.
*
Expand All @@ -276,7 +338,8 @@ public CMACallback<CMAEnvironment> create(
public CMACallback<Integer> delete(final CMAEnvironment environment,
CMACallback<Integer> callback) {
return defer(new DefFunc<Integer>() {
@Override Integer method() {
@Override
Integer method() {
return ModuleEnvironments.this.delete(environment);
}
}, callback);
Expand All @@ -295,7 +358,8 @@ public CMACallback<Integer> delete(final CMAEnvironment environment,
public CMACallback<CMAArray<CMAEnvironment>> fetchAll(
CMACallback<CMAArray<CMAEnvironment>> callback) {
return defer(new DefFunc<CMAArray<CMAEnvironment>>() {
@Override CMAArray<CMAEnvironment> method() {
@Override
CMAArray<CMAEnvironment> method() {
return ModuleEnvironments.this.fetchAll();
}
}, callback);
Expand All @@ -318,7 +382,8 @@ public CMACallback<CMAArray<CMAEnvironment>> fetchAll(
final String spaceId,
CMACallback<CMAArray<CMAEnvironment>> callback) {
return defer(new DefFunc<CMAArray<CMAEnvironment>>() {
@Override CMAArray<CMAEnvironment> method() {
@Override
CMAArray<CMAEnvironment> method() {
return ModuleEnvironments.this.fetchAll(spaceId);
}
}, callback);
Expand All @@ -339,7 +404,8 @@ public CMACallback<CMAEnvironment> fetchOne(
final String environmentId,
CMACallback<CMAEnvironment> callback) {
return defer(new DefFunc<CMAEnvironment>() {
@Override CMAEnvironment method() {
@Override
CMAEnvironment method() {
return ModuleEnvironments.this.fetchOne(environmentId);
}
}, callback);
Expand All @@ -363,7 +429,8 @@ public CMACallback<CMAEnvironment> fetchOne(
final String environmentId,
CMACallback<CMAEnvironment> callback) {
return defer(new DefFunc<CMAEnvironment>() {
@Override CMAEnvironment method() {
@Override
CMAEnvironment method() {
return ModuleEnvironments.this.fetchOne(spaceId, environmentId);
}
}, callback);
Expand All @@ -385,7 +452,8 @@ public CMACallback<CMAEnvironment> update(
final CMAEnvironment environment,
CMACallback<CMAEnvironment> callback) {
return defer(new DefFunc<CMAEnvironment>() {
@Override CMAEnvironment method() {
@Override
CMAEnvironment method() {
return ModuleEnvironments.this.update(environment);
}
}, callback);
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/contentful/java/cma/ServiceEnvironments.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.contentful.java.cma.model.CMAArray;
import com.contentful.java.cma.model.CMAEnvironment;

import io.reactivex.Flowable;
import retrofit2.Response;
import retrofit2.http.Body;
Expand All @@ -38,12 +37,25 @@ Flowable<CMAEnvironment> create(
@Path("spaceId") String spaceId,
@Body CMAEnvironment environment);

@POST("/spaces/{spaceId}/environments")
Flowable<CMAEnvironment> branch(
@Path("spaceId") String spaceId,
@Header("X-Contentful-Source-Environment") String sourceEnvironmentId,
@Body CMAEnvironment environment);

@PUT("/spaces/{spaceId}/environments/{environmentId}")
Flowable<CMAEnvironment> create(
@Path("spaceId") String spaceId,
@Path("environmentId") String environmentId,
@Body CMAEnvironment environment);

@PUT("/spaces/{spaceId}/environments/{environmentId}")
Flowable<CMAEnvironment> branch(
@Path("spaceId") String spaceId,
@Header("X-Contentful-Source-Environment") String sourceEnvironmentId,
@Path("environmentId") String environmentId,
@Body CMAEnvironment environment);

@DELETE("/spaces/{spaceId}/environments/{environmentId}")
Flowable<Response<Void>> delete(
@Header("X-Contentful-Version") Integer version,
Expand Down
2 changes: 1 addition & 1 deletion src/test/bash/environments_create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ curl --verbose \
-X POST \
-H 'Content-Type: application/vnd.contentful.management.v1+json' \
-H 'Authorization: Bearer '$CMA_TOKEN \
-d '{"name":"environment_name"}' \
-d '{"name":"environment_name"}' \
"https://api.contentful.com/spaces/$SPACE_ID/environments" \
| sed 's/'${SPACE_ID}'/<space_id>/g' \
| sed 's/'${CMA_TOKEN}'/<access_token>/g' \
Expand Down
15 changes: 15 additions & 0 deletions src/test/bash/environments_create_from_id.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

output="$(basename $(echo $0) | sed 's#sh#json#g' | sed 's#^#../resources/#g')"

curl --verbose \
-X PUT \
-H 'Content-Type: application/vnd.contentful.management.v1+json' \
-H 'Authorization: Bearer '$CMA_TOKEN \
-H 'X-Contentful-Source-Environment: io' \
-d '{"name":"environment branched from io"}' \
"https://api.contentful.com/spaces/$SPACE_ID/environments/branched_from_io" \
| sed 's/'${SPACE_ID}'/<space_id>/g' \
| sed 's/'${CMA_TOKEN}'/<access_token>/g' \
| sed 's/'${USER_ID}'/<user_id>/g' \
| tee ${output}
2 changes: 1 addition & 1 deletion src/test/bash/environments_create_with_id.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ curl --verbose \
-X PUT \
-H 'Content-Type: application/vnd.contentful.management.v1+json' \
-H 'Authorization: Bearer '$CMA_TOKEN \
-d '{"name":"environment_with_id"}' \
-d '{"name":"environment_with_id"}' \
"https://api.contentful.com/spaces/$SPACE_ID/environments/new_id" \
| sed 's/'${SPACE_ID}'/<space_id>/g' \
| sed 's/'${CMA_TOKEN}'/<access_token>/g' \
Expand Down
77 changes: 77 additions & 0 deletions src/test/kotlin/com/contentful/java/cma/EnvironmentsTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.junit.Before
import java.util.logging.LogManager
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
import org.junit.Test as test

class EnvironmentsTests {
Expand Down Expand Up @@ -137,6 +138,82 @@ class EnvironmentsTests {
assertEquals("/spaces/%3Cspace_id%3E/environments/new_id", recordedRequest.path)
}

@test
fun testCreateFromOther() {
val responseBody = TestUtils.fileToString("environments_create_from_id.json")
server!!.enqueue(MockResponse().setResponseCode(200).setBody(responseBody))

// should be fetched from Contentful.
val sourceEnvironment = CMAEnvironment().apply {
id = "source"
name = "source"
setSpaceId<CMAEnvironment>("my_space")
}

val newEnvironment = CMAEnvironment().apply {
name = "environment_from_id"
id = "branched_from_io"
}

val result = assertTestCallback(
client!!
.environments()
.async()
.branch(
sourceEnvironment,
newEnvironment,
TestCallback()
) as TestCallback)!!

assertEquals("branched_from_io", result.id)
assertEquals("<space_id>", result.spaceId)

// Request
val recordedRequest = server!!.takeRequest()
assertEquals("PUT", recordedRequest.method)
assertTrue(recordedRequest.headers.names().contains("X-Contentful-Source-Environment"))
assertEquals("source", recordedRequest.headers["X-Contentful-Source-Environment"])
assertEquals("/spaces/configuredSpaceId/environments/branched_from_io", recordedRequest.path)
}

@test
fun testCreateFromOtherWithoutTargetId() {
val responseBody = TestUtils.fileToString("environments_create_from_id.json")
server!!.enqueue(MockResponse().setResponseCode(200).setBody(responseBody))


// should be fetched from Contentful.
val sourceEnvironment = CMAEnvironment().apply {
id = "source"
name = "source"
setSpaceId<CMAEnvironment>("my_space")
}

val newEnvironment = CMAEnvironment().apply {
name = "environment_from_id"
}

val result = assertTestCallback(
client!!
.environments()
.async()
.branch(
sourceEnvironment,
newEnvironment,
TestCallback()
) as TestCallback)!!

assertEquals("branched_from_io", result.id)
assertEquals("<space_id>", result.spaceId)

// Request
val recordedRequest = server!!.takeRequest()
assertEquals("POST", recordedRequest.method)
assertTrue(recordedRequest.headers.names().contains("X-Contentful-Source-Environment"))
assertEquals("source", recordedRequest.headers["X-Contentful-Source-Environment"])
assertEquals("/spaces/configuredSpaceId/environments", recordedRequest.path)
}

@test
fun testDelete() {
val fetchBody = TestUtils.fileToString("environments_get_one.json")
Expand Down
Loading

0 comments on commit 03ff887

Please sign in to comment.