added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / test / java / org / oran / otf / api / tests / integration / services / InstanceServiceRouteIT.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 package org.oran.otf.api.tests.integration.services;\r
18 \r
19 import static org.hamcrest.CoreMatchers.containsString;\r
20 import static org.hamcrest.CoreMatchers.equalTo;\r
21 \r
22 import org.oran.otf.api.Application;\r
23 import org.oran.otf.api.tests.shared.MemoryDatabase;\r
24 import org.oran.otf.common.model.TestDefinition;\r
25 import org.oran.otf.common.model.TestInstance;\r
26 import org.oran.otf.common.model.User;\r
27 import io.restassured.RestAssured;\r
28 import org.eclipse.jetty.http.QuotedQualityCSV;\r
29 import org.junit.AfterClass;\r
30 import org.junit.Before;\r
31 import org.junit.BeforeClass;\r
32 import org.junit.Test;\r
33 import org.junit.runner.RunWith;\r
34 import org.springframework.beans.factory.annotation.Autowired;\r
35 import org.springframework.beans.factory.annotation.Value;\r
36 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;\r
37 import org.springframework.boot.test.context.SpringBootTest;\r
38 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;\r
39 import org.springframework.boot.web.server.LocalServerPort;\r
40 import org.springframework.data.mongodb.core.MongoTemplate;\r
41 import org.springframework.data.mongodb.core.query.Criteria;\r
42 import org.springframework.data.mongodb.core.query.Query;\r
43 import org.springframework.test.context.ActiveProfiles;\r
44 import org.springframework.test.context.TestPropertySource;\r
45 import org.springframework.test.context.junit4.SpringRunner;\r
46 \r
47 @RunWith(SpringRunner.class)\r
48 @SpringBootTest(\r
49     webEnvironment = WebEnvironment.RANDOM_PORT,\r
50     classes = {Application.class}\r
51 )\r
52 @TestPropertySource("classpath:application-test.properties")\r
53 @ActiveProfiles("test")\r
54 public class InstanceServiceRouteIT {\r
55   @LocalServerPort\r
56   private int port;\r
57   @Value("${otf.mechid}")\r
58   private String username;\r
59   @Value("${otf.mechpass}")\r
60   private String password;\r
61   private static User mechUser;\r
62 \r
63   @Autowired\r
64   private MongoTemplate mongoTemplate;\r
65 \r
66   @BeforeClass\r
67   public static void setup() throws Exception{\r
68     MemoryDatabase.createAllTables();\r
69     MemoryDatabase.createAllAdmin();\r
70     //mechUser = MemoryDatabase.createMechUser();\r
71   }\r
72   @AfterClass\r
73   public static void cleanup(){\r
74     MemoryDatabase.cleanup();\r
75   }\r
76   @Before\r
77   public void setupRestAssured() {\r
78     RestAssured.port = port;\r
79     RestAssured.urlEncodingEnabled = false;\r
80     RestAssured.baseURI = "https://localhost";\r
81     RestAssured.basePath="/otf/api/testInstance";\r
82     RestAssured.useRelaxedHTTPSValidation();\r
83   }\r
84   //NoAuth Tests\r
85 \r
86   @Test\r
87   public void testFindByIdRespondsWith401OnNoAuth(){\r
88     RestAssured.given().log().all().header("Accept", "application/json").get("/v1/id/abced").then().assertThat().statusCode(401);\r
89   }\r
90   @Test\r
91   public void testFindByProcessKeyRespondsWith401OnNoAuth(){\r
92     RestAssured.given().log().all().header("Accept", "application/json").get("/v1/processDefinitionKey/abced").then().assertThat().statusCode(401);\r
93   }\r
94   @Test\r
95   public void testFindByProcessKeyAndVersionRespondsWith401OnNoAuth(){\r
96     RestAssured.given().log().all().header("Accept", "application/json").get("/v1/processDefinitionKey/abced/version/1").then().assertThat().statusCode(401);\r
97   }\r
98 \r
99 \r
100   @Test\r
101   public void testExecuteRespondsWith401OnNoAuth(){\r
102     RestAssured.given().log().all().header("Accept", "application/json").get("/execute/v1/id/abced/").then().assertThat().statusCode(401);\r
103   }\r
104 \r
105 \r
106   @Test\r
107   public void testCreateByTestDefinitionIdRespondsWith401OnNoAuth(){\r
108     RestAssured.given().log().all().header("Accept", "application/json").get("/create/v1/testDefinitionId/abced/").then().assertThat().statusCode(401);\r
109   }\r
110   @Test\r
111   public void testCreateByTestDefinitionIdAndVersionRespondsWith401OnNoAuth(){\r
112     RestAssured.given().log().all().header("Accept", "application/json").get("/create/v1/testDefinitionId/abced/version/2").then().assertThat().statusCode(401);\r
113   }\r
114   @Test\r
115   public void testCreateByProcessDefinitionKeyRespondsWith401OnNoAuth(){\r
116     RestAssured.given().log().all().header("Accept", "application/json").get("/create/v1/processDefinitionKey/abced").then().assertThat().statusCode(401);\r
117   }\r
118   @Test\r
119   public void testCreateByProcessDefinitionKeyAndVersionRespondsWith401OnNoAuth(){\r
120     RestAssured.given().log().all().header("Accept", "application/json").get("/create/v1/processDefinitionKey/abced/version/2").then().assertThat().statusCode(401);\r
121   }\r
122 \r
123   //With Auth and Wrong id\r
124   @Test\r
125   public void testFindByIdRespondsWith400OnWrongId(){\r
126     RestAssured.given().auth().basic(username,password).log().all().header("Accept", "application/json").get("/v1/id/abced").then().assertThat().statusCode(400);\r
127   }\r
128   @Test\r
129   public void testFindByIdRespondsWithMessageOnWrongId(){\r
130     RestAssured.given().auth().basic(username,password).log().all().header("Accept", "application/json").get("/v1/id/abcde").then().assertThat().body("message", containsString("is not a valid ObjectId (BSON)"));\r
131   }\r
132   @Test\r
133   public void testFindByProcessDefinitionRespondsWith400OnWrongProcessDefinition(){\r
134     TestDefinition testDefinition = mongoTemplate.findOne(new Query(Criteria.where("testName").is("testDef1")), TestDefinition.class);\r
135     RestAssured.given().auth().basic(username,password).log().all().header("Accept", "*/*").get("/v1/processDefinitionKey/"+testDefinition.getProcessDefinitionKey()).then().assertThat().statusCode(400);\r
136   }\r
137   @Test\r
138   public void testFindByProcessDefinitionRespondsWithMessageOnWrongProcessDefinition(){\r
139     TestDefinition testDefinition = mongoTemplate.findOne(new Query(Criteria.where("testName").is("testDef1")), TestDefinition.class);\r
140     RestAssured.given().auth().basic(username,password).log().all().header("Accept", "application/json").get("/v1/processDefinitionKey/"+testDefinition.getProcessDefinitionKey()).then().assertThat().body("message", containsString("No test instances found"));\r
141   }\r
142 \r
143   //Successful Get Methods\r
144 \r
145   @Test\r
146   public void testFindByIdRespondsWith200OnSuccess(){\r
147     TestInstance testInstance = mongoTemplate.findOne(new Query(Criteria.where("testInstanceName").is("MechTestInstance")), TestInstance.class);\r
148     RestAssured.given().auth().basic(username,password).log().all().header("Accept", "*/*").get("/v1/id/"+testInstance.get_id()).then().assertThat().statusCode(200);\r
149   }\r
150 \r
151   @Test\r
152   public void testFindByProcessDefinitionKeyRespondsWith200OnSuccess(){\r
153     TestDefinition testDefinition = mongoTemplate.findOne(new Query(Criteria.where("testName").is("MechTestDefinition")), TestDefinition.class);\r
154     RestAssured.given().auth().basic(username, password).log().all().header("Accept", "*/*").get("/v1/processDefinitionKey/"+testDefinition.getProcessDefinitionKey()).then().assertThat().statusCode(200);\r
155   }\r
156 \r
157   @Test\r
158   public void testFindByProcessDefinitionKeyAndVersionRespondsWith200OnSuccess(){\r
159     TestDefinition testDefinition = mongoTemplate.findOne(new Query(Criteria.where("testName").is("MechTestDefinition")), TestDefinition.class);\r
160     RestAssured.given().auth().basic(username, password).log().all().header("Accept", "*/*").get("/v1/processDefinitionKey/"+testDefinition.getProcessDefinitionKey()+"/version/"+1).then().assertThat().statusCode(200);\r
161   }\r
162 \r
163   @Test\r
164   public void testCreateByTestDefinitionIdRespondsWith201OnSuccess(){\r
165     TestDefinition testDefinition = mongoTemplate.findOne(new Query(Criteria.where("testName").is("MechTestDefinition")), TestDefinition.class);\r
166     System.out.println(testDefinition.getBpmnInstances());\r
167     RestAssured.given().contentType("application/json\r\n").auth().basic(username, password).log().all().header("Accept", "*/*").post("/create/v1/testDefinitionId/"+testDefinition.get_id()+"/version/"+1).then().assertThat().statusCode(404);\r
168   }\r
169 \r
170 }\r