summaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/test/java/org/eclipse/winery/repository/resources/entitytypes/nodetypes/reqandcapdefs/TestRequirementDefinitions.java
blob: 47ee3d05d032587d445a4ee259689d2c2c7da846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 * Copyright (c) 2012-2013 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Oliver Kopp - initial API and implementation
 *******************************************************************************/
package org.eclipse.winery.repository.resources.entitytypes.nodetypes.reqandcapdefs;

import javax.ws.rs.core.MediaType;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.eclipse.winery.repository.PrefsTestEnabledGitBackedRepository;
import org.eclipse.winery.repository.backend.Repository;
import org.eclipse.winery.repository.backend.filebased.GitBasedRepository;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;

//@formatter:off

/*
 * import static com.jayway.restassured.RestAssured.*; import static
 * com.jayway.restassured.matcher.RestAssuredMatchers.*; import static
 * org.hamcrest.Matchers.*; import static
 * com.jayway.restassured.path.json.JsonPath.*;
 */


/**
 * REST-based testing of requirement definitions
 * 
 * We use a fixed method sort order as we create resources in one test and work
 * with them in the next step
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestRequirementDefinitions {
	
	@BeforeClass
	public static void init() throws Exception {
		// enable git-backed repository
		new PrefsTestEnabledGitBackedRepository();
		
		// we use a half-filled repository
		((GitBasedRepository) Repository.INSTANCE).setRevisionTo("97fa997b92965d8bc84e86274b0203f1db7495c5");
		
		// we test on the Amazon EC2 node type
		// could be any other node type without requirement definitions
		//
		// the following URI is already encoded (copied from the browser URL field)
		RestAssured.urlEncodingEnabled = false;
		RestAssured.basePath = "/org.eclipse.winery.repository/nodetypes/http%253A%252F%252Fwww.example.org%252Ftosca%252Fnodetypes/Amazon_EC2/requirementdefinitions";
	}
	
	@Test
	public void test01_NoRequirementDefinitions() throws Exception {
		RestAssured.given()
			.header("Accept", MediaType.APPLICATION_JSON)
		.expect()
			.body(Matchers.equalTo("[]"))
		.when()
			.get("");
	}

	@Test
	public void test02_CreateRequirementDefinition() throws Exception {
		RestAssured.given()
			.parameter("name", "test")
		.expect()
			.statusCode(204)
		.when()
			.post("/");
	}

	@Test
	public void test03_NoConstraints() throws Exception {
		RestAssured.given()
			.header("Accept", MediaType.APPLICATION_JSON)
		.expect()
			.body(Matchers.equalTo("[]"))
		.when()
			.get("test/constraints/");
	}

	@Test
	public void test04_CreateConstraint() throws Exception {
		RestAssured.given()
			.body("<tosca:Constraint xmlns:tosca=\"http://docs.oasis-open.org/tosca/ns/2011/12\" xmlns:winery=\"http://www.opentosca.org/winery/extensions/tosca/2013/02/12\" constraintType=\"http://www.example.org/constrainttype\"/>")
			.contentType(ContentType.XML)
		.expect()
			.statusCode(200)
			.body(Matchers.notNullValue())
		.when()
			.post("test/constraints/");
	}

	@Test
	public void test05_GetConstraint() throws Exception {
		Response response = RestAssured
			.given()
				.header("Accept", MediaType.APPLICATION_JSON)
			.expect()
				.statusCode(200)
			.when()
				.get("test/constraints/");
		
		// extract answer
		JsonPath jsonPath = JsonPath.from(response.asString());
		
		Assert.assertEquals("One id", jsonPath.getList("").size(),  1);
		
		String id = jsonPath.getString("[0]");
		
		// TODO: check content
		RestAssured
				.given()
					.header("Accept", MediaType.TEXT_XML)
				.expect()
					.statusCode(200)
				.when()
					.get("test/constraints/{id}/", id);

		// we also test the sub resource here
		// otherwise we had to transport the id throught the code via a global variable
		RestAssured
		.expect()
			.statusCode(200)
			.body(Matchers.is("http://www.example.org/constrainttype"))
		.when()
			.get("test/constraints/{id}/type", id);
	}

}