Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration test for GrafeasTransport with link submission #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@


package io.jenkins.plugins.intotorecorder.transport;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;

import io.github.intoto.legacy.keys.RSAKey;
import io.github.intoto.legacy.models.Link;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpResponse;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;

public class GrafeasTransportTestIT {

private final String keyFilepath = "src/test/resources/keys/somekey.pem";

private GrafeasTransport transport;
private RSAKey key;
private URI uri;

@Before
public void setUp() throws Exception {
String grafeasServerUrl = System.getenv("GRAFEAS_SERVER_URL");
String noteName = "projects/my-project/notes/my-note";
String resourceUri = "https://example.com/resource-uri";
this.uri = new URI(grafeasServerUrl + "?noteName=" + noteName + "&resourceUri=" + resourceUri);
this.transport = new GrafeasTransport(uri);
this.key = RSAKey.read(keyFilepath);
}

@Test
public void testGrafeasStorage() throws Exception {

Link link = new Link(null, null, "step", null, null, null);
link.sign(this.key);

/* submit the link to the Grafeas server */
this.transport.submit(link);

/* To make sure we got the right thing, we'll get the occurrence
* information as a string and compare it with our local copy.
* Modulo any encoding weirdness, they should match exactly as we sent it
*/
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(this.uri.toString().split("\\?")[0] + "/occurrences");

ResponseHandler<Response> handler = new ResponseHandler<Response>() {
@Override
public Response handleResponse(final HttpResponse response) throws IOException {
if (response.getStatusLine().getStatusCode() >= 300) {
throw new IOException("Server responded with failing status code");
}
if (response.getEntity() == null) {
throw new IOException("Server's response was invalid");
}

Gson gson = new GsonBuilder().create();
Reader r = new InputStreamReader(response.getEntity().getContent());
return gson.fromJson(r, Response.class);
}
};

Response response = client.execute(get, handler);
assertTrue(response.success);
}

class Response {
boolean success;
String error;
}
}