1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\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
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\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
17 package org.oran.otf.api.tests.integration.services;
\r
19 import static org.hamcrest.CoreMatchers.containsString;
\r
20 import static org.hamcrest.CoreMatchers.equalTo;
\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
47 @RunWith(SpringRunner.class)
\r
49 webEnvironment = WebEnvironment.RANDOM_PORT,
\r
50 classes = {Application.class}
\r
52 @TestPropertySource("classpath:application-test.properties")
\r
53 @ActiveProfiles("test")
\r
54 public class InstanceServiceRouteIT {
\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
64 private MongoTemplate mongoTemplate;
\r
67 public static void setup() throws Exception{
\r
68 MemoryDatabase.createAllTables();
\r
69 MemoryDatabase.createAllAdmin();
\r
70 //mechUser = MemoryDatabase.createMechUser();
\r
73 public static void cleanup(){
\r
74 MemoryDatabase.cleanup();
\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
87 public void testFindByIdRespondsWith401OnNoAuth(){
\r
88 RestAssured.given().log().all().header("Accept", "application/json").get("/v1/id/abced").then().assertThat().statusCode(401);
\r
91 public void testFindByProcessKeyRespondsWith401OnNoAuth(){
\r
92 RestAssured.given().log().all().header("Accept", "application/json").get("/v1/processDefinitionKey/abced").then().assertThat().statusCode(401);
\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
101 public void testExecuteRespondsWith401OnNoAuth(){
\r
102 RestAssured.given().log().all().header("Accept", "application/json").get("/execute/v1/id/abced/").then().assertThat().statusCode(401);
\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
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
115 public void testCreateByProcessDefinitionKeyRespondsWith401OnNoAuth(){
\r
116 RestAssured.given().log().all().header("Accept", "application/json").get("/create/v1/processDefinitionKey/abced").then().assertThat().statusCode(401);
\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
123 //With Auth and Wrong id
\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
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
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
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
143 //Successful Get Methods
\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
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
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
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