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

Commit

Permalink
Merge pull request #3 from zalando-stups/#2-list-tags-docker-version-…
Browse files Browse the repository at this point in the history
…independent

#2 list tags docker version independent
  • Loading branch information
mrandi committed Nov 3, 2015
2 parents ab18f07 + 7f112b3 commit 22858ed
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 467 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@

target

pom.xml.*Backup
pom.xml.*Backup

*.iml
.idea
11 changes: 10 additions & 1 deletion pierone-client-java-api/pom.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.zalando.stups</groupId>
<artifactId>pierone-client-java</artifactId>
<version>0.5.2-SNAPSHOT</version>
</parent>

<artifactId>pierone-client-java-api</artifactId>
</project>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@

/**
* Defines Operations on PierOne.
*
* @author jbellmann
*/
public interface PieroneOperations {

Map<String, String> listTags(String team, String artifact);
/**
* Returns a mapping between tag names and tag summaries, e.g.
* <code><pre>
* {
* "1.0": {"name": "1.0", "created": "2015-11-01T00:00:00.000Z", "createdBy": "someone"},
* "2.0": {"name": "2.0", "created": "2015-11-02T00:00:00.000Z", "createdBy": "someoneelse"}
* }
* </pre></code>
*
* @param team a team id, must not be empty
* @param artifact an artifact id, muts not be empty
* @return a Map, never null
*/
Map<String, TagSummary> listTags(String team, String artifact);

Map<String, String> getScmSource(String team, String artifact, String version);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2015 Zalando SE (http://tech.zalando.com)
*
* 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 org.zalando.stups.fullstop.clients.pierone;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.ZonedDateTime;

@JsonIgnoreProperties(ignoreUnknown = true)
public class TagSummary {

private final String name;
private final ZonedDateTime created;
private final String createdBy;

@JsonCreator
public TagSummary(
@JsonProperty("name") String name,
@JsonProperty("created") ZonedDateTime created,
@JsonProperty("created_by") String createdBy) {
this.name = name;
this.created = created;
this.createdBy = createdBy;
}

public String getName() {
return name;
}

public ZonedDateTime getCreated() {
return created;
}

public String getCreatedBy() {
return createdBy;
}

@Override
public String toString() {
return "TagSummary{" +
"name='" + name + '\'' +
", created=" + created +
", createdBy='" + createdBy + '\'' +
'}';
}
}
43 changes: 13 additions & 30 deletions pierone-client-java-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,36 @@
<artifactId>spring-web</artifactId>
</dependency>


<dependency>
<groupId>org.zalando.stups</groupId>
<artifactId>stups-spring-oauth2-client</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
<!-- (de-)serialize Java 8 time types, e.g. ZonedDateTime -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -60,22 +59,6 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -86,4 +69,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,30 @@
*/
package org.zalando.stups.fullstop.clients.pierone.spring;


import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.client.RestOperations;
import org.zalando.stups.fullstop.clients.pierone.PieroneOperations;
import org.zalando.stups.fullstop.clients.pierone.TagSummary;

import java.util.HashMap;
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.http.RequestEntity.get;
import static org.springframework.web.util.UriComponentsBuilder.fromHttpUrl;

/**
* Implementation of {@link PieroneOperations} with spring.
*
* @author jbellmann
*/
public class RestTemplatePieroneOperations implements PieroneOperations {

private static final ParameterizedTypeReference<List<TagSummary>> LIST_OF_TAG_SUMMARIES = new ParameterizedTypeReference<List<TagSummary>>() {
};
private static final ParameterizedTypeReference<Map<String, String>> MAP_STRING_TO_STRING = new ParameterizedTypeReference<Map<String, String>>() {
};

private final RestOperations restOperations;

private final String baseUrl;
Expand All @@ -38,26 +49,17 @@ public RestTemplatePieroneOperations(final RestOperations restOperations, final
}

@Override
public Map<String, String> listTags(final String team, final String artifact) {
Map<String, String> uriVariables = new HashMap<>(0);
uriVariables.put("team", team);
uriVariables.put("artifact", artifact);

Map<String, String> result = this.restOperations.getForObject(baseUrl
+ "/v1/repositories/{team}/{artifact}/tags", Map.class, uriVariables);

public Map<String, TagSummary> listTags(final String team, final String artifact) {
final URI uri = fromHttpUrl(baseUrl + "/teams/{team}/artifacts/{artifact}/tags").buildAndExpand(team, artifact).toUri();
final List<TagSummary> tags = restOperations.exchange(get(uri).build(), LIST_OF_TAG_SUMMARIES).getBody();
final Map<String, TagSummary> result = new LinkedHashMap<>(tags.size());
tags.forEach((tag) -> result.put(tag.getName(), tag));
return result;
}

@Override
public Map<String, String> getScmSource(String team, String artifact, String version) {

Map<String, String> uriVariables = new HashMap<>(0);
uriVariables.put("team", team);
uriVariables.put("artifact", artifact);
uriVariables.put("version", version);

return this.restOperations.getForObject(baseUrl
+ "/teams/{team}/artifacts/{artifact}/tags/{version}/scm-source", Map.class, uriVariables);
final URI uri = fromHttpUrl(baseUrl + "/teams/{team}/artifacts/{artifact}/tags/{version}/scm-source").buildAndExpand(team, artifact, version).toUri();
return this.restOperations.exchange(get(uri).build(), MAP_STRING_TO_STRING).getBody();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

class ResourceUtil {
final class ResourceUtil {

static Resource resource(final String resourcename) {
return new ClassPathResource(resourcename + ".json", ResourceUtil.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,57 @@
*/
package org.zalando.stups.fullstop.clients.pierone.spring;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import org.zalando.stups.fullstop.clients.pierone.TagSummary;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Map;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import static org.zalando.stups.fullstop.clients.pierone.spring.ResourceUtil.resource;

/**
* @author jbellmann
*/
public class RestTemplatePieroneOperationsTest {

private final String baseUrl = "http://localhost:8080";

private OAuth2RestTemplate restTemplate;

private MockRestServiceServer mockServer;

private RestTemplatePieroneOperations client;

@Before
public void setUp() {
final ObjectMapper om = new ObjectMapper();
om.findAndRegisterModules();

BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
resource.setClientId("what_here");

restTemplate = new OAuth2RestTemplate(resource);
restTemplate.setAccessTokenProvider(new TestAccessTokenProvider("86c45354-8bc4-44bf-905f-5f34ebe0b599"));
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
final RestTemplate restTemplate = new RestTemplate(singletonList(new MappingJackson2HttpMessageConverter(om)));

client = new RestTemplatePieroneOperations(restTemplate, baseUrl);

mockServer = MockRestServiceServer.createServer(restTemplate);
}

//J-
@Test
public void getRefoles() {
mockServer.expect(requestTo(baseUrl + "/v1/repositories/testTeam/testApplication/tags"))
.andExpect(method(GET))
.andExpect(header("Authorization", "Bearer 86c45354-8bc4-44bf-905f-5f34ebe0b599"))
.andRespond(withSuccess(ResourceUtil.resource("/getTags"), APPLICATION_JSON));
public void getTags() {
mockServer.expect(requestTo(baseUrl + "/teams/testTeam/artifacts/testApplication/tags"))
.andExpect(method(GET))
.andRespond(withSuccess(resource("/getTags"), APPLICATION_JSON));

Map<String,String> resultMap = client.listTags("testTeam", "testApplication");
assertThat(resultMap).isNotNull();
assertThat(resultMap).isNotEmpty();
assertThat(resultMap.size()).isEqualTo(6);
assertThat(resultMap.get("0.7")).isEqualTo("febd155ed19b741f2c87f162ed6183c84039b7c8e43eb0136d475f816a821894");
final Map<String, TagSummary> resultMap = client.listTags("testTeam", "testApplication");
assertThat(resultMap).hasSize(4);
assertThat(resultMap.get("4.0")).isEqualToComparingFieldByField(
new TagSummary("4.0", ZonedDateTime.of(2015, 11, 1, 0, 0, 0, 0, ZoneId.of("GMT")), "some_one"));

mockServer.verify();
}
Expand All @@ -80,8 +75,7 @@ public void getRefoles() {
public void getScmSource() {
mockServer.expect(requestTo(baseUrl + "/teams/testTeam/artifacts/testApplication/tags/testVersion/scm-source"))
.andExpect(method(GET))
.andExpect(header("Authorization", "Bearer 86c45354-8bc4-44bf-905f-5f34ebe0b599"))
.andRespond(withSuccess(ResourceUtil.resource("/getScmSource"), APPLICATION_JSON));
.andRespond(withSuccess(resource("/getScmSource"), APPLICATION_JSON));

Map<String, String> resultMap = client.getScmSource("testTeam", "testApplication", "testVersion");
assertThat(resultMap).isNotNull();
Expand Down
Loading

0 comments on commit 22858ed

Please sign in to comment.