Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / test / java / org / eclipse / winery / repository / resources / entitytypes / nodetypes / reqandcapdefs / TestRequirementDefinitions.java
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.resources.entitytypes.nodetypes.reqandcapdefs;
13
14 import javax.ws.rs.core.MediaType;
15
16 import org.hamcrest.Matchers;
17 import org.junit.Assert;
18 import org.junit.BeforeClass;
19 import org.junit.FixMethodOrder;
20 import org.junit.Test;
21 import org.junit.runners.MethodSorters;
22 import org.eclipse.winery.repository.PrefsTestEnabledGitBackedRepository;
23 import org.eclipse.winery.repository.backend.Repository;
24 import org.eclipse.winery.repository.backend.filebased.GitBasedRepository;
25
26 import com.jayway.restassured.RestAssured;
27 import com.jayway.restassured.http.ContentType;
28 import com.jayway.restassured.path.json.JsonPath;
29 import com.jayway.restassured.response.Response;
30
31 //@formatter:off
32
33 /*
34  * import static com.jayway.restassured.RestAssured.*; import static
35  * com.jayway.restassured.matcher.RestAssuredMatchers.*; import static
36  * org.hamcrest.Matchers.*; import static
37  * com.jayway.restassured.path.json.JsonPath.*;
38  */
39
40
41 /**
42  * REST-based testing of requirement definitions
43  * 
44  * We use a fixed method sort order as we create resources in one test and work
45  * with them in the next step
46  */
47 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
48 public class TestRequirementDefinitions {
49         
50         @BeforeClass
51         public static void init() throws Exception {
52                 // enable git-backed repository
53                 new PrefsTestEnabledGitBackedRepository();
54                 
55                 // we use a half-filled repository
56                 ((GitBasedRepository) Repository.INSTANCE).setRevisionTo("97fa997b92965d8bc84e86274b0203f1db7495c5");
57                 
58                 // we test on the Amazon EC2 node type
59                 // could be any other node type without requirement definitions
60                 //
61                 // the following URI is already encoded (copied from the browser URL field)
62                 RestAssured.urlEncodingEnabled = false;
63                 RestAssured.basePath = "/org.eclipse.winery.repository/nodetypes/http%253A%252F%252Fwww.example.org%252Ftosca%252Fnodetypes/Amazon_EC2/requirementdefinitions";
64         }
65         
66         @Test
67         public void test01_NoRequirementDefinitions() throws Exception {
68                 RestAssured.given()
69                         .header("Accept", MediaType.APPLICATION_JSON)
70                 .expect()
71                         .body(Matchers.equalTo("[]"))
72                 .when()
73                         .get("");
74         }
75
76         @Test
77         public void test02_CreateRequirementDefinition() throws Exception {
78                 RestAssured.given()
79                         .parameter("name", "test")
80                 .expect()
81                         .statusCode(204)
82                 .when()
83                         .post("/");
84         }
85
86         @Test
87         public void test03_NoConstraints() throws Exception {
88                 RestAssured.given()
89                         .header("Accept", MediaType.APPLICATION_JSON)
90                 .expect()
91                         .body(Matchers.equalTo("[]"))
92                 .when()
93                         .get("test/constraints/");
94         }
95
96         @Test
97         public void test04_CreateConstraint() throws Exception {
98                 RestAssured.given()
99                         .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\"/>")
100                         .contentType(ContentType.XML)
101                 .expect()
102                         .statusCode(200)
103                         .body(Matchers.notNullValue())
104                 .when()
105                         .post("test/constraints/");
106         }
107
108         @Test
109         public void test05_GetConstraint() throws Exception {
110                 Response response = RestAssured
111                         .given()
112                                 .header("Accept", MediaType.APPLICATION_JSON)
113                         .expect()
114                                 .statusCode(200)
115                         .when()
116                                 .get("test/constraints/");
117                 
118                 // extract answer
119                 JsonPath jsonPath = JsonPath.from(response.asString());
120                 
121                 Assert.assertEquals("One id", jsonPath.getList("").size(),  1);
122                 
123                 String id = jsonPath.getString("[0]");
124                 
125                 // TODO: check content
126                 RestAssured
127                                 .given()
128                                         .header("Accept", MediaType.TEXT_XML)
129                                 .expect()
130                                         .statusCode(200)
131                                 .when()
132                                         .get("test/constraints/{id}/", id);
133
134                 // we also test the sub resource here
135                 // otherwise we had to transport the id throught the code via a global variable
136                 RestAssured
137                 .expect()
138                         .statusCode(200)
139                         .body(Matchers.is("http://www.example.org/constrainttype"))
140                 .when()
141                         .get("test/constraints/{id}/type", id);
142         }
143
144 }