vid-automation selenium tests
[vid.git] / vid-automation / src / test / java / org / opencomp / vid / api / CategoryParametersApiTest.java
1 package org.opencomp.vid.api;
2
3 import org.opencomp.vid.model.category.AddCategoryOptionsRequest;
4 import org.opencomp.vid.model.category.CategoryParameterOption;
5 import org.opencomp.vid.model.category.CategoryParameterOptionRep;
6 import org.opencomp.vid.model.category.CategoryParametersResponse;
7 import org.springframework.http.HttpStatus;
8 import org.springframework.web.util.UriComponentsBuilder;
9 import org.testng.Assert;
10 import org.testng.annotations.Test;
11 import vid.automation.test.services.CategoryParamsService;
12
13 import javax.ws.rs.HttpMethod;
14 import javax.ws.rs.client.Entity;
15 import javax.ws.rs.client.WebTarget;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.Response;
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.Random;
21 import java.util.UUID;
22 import java.util.stream.Collectors;
23
24 import static org.testng.AssertJUnit.assertEquals;
25
26
27 //This is integration test that require running tomcat
28 public class CategoryParametersApiTest extends BaseApiTest {
29
30     public static final String GET_CATEGORY_PARAMETER_PROPERTIES = "maintenance/category_parameter";
31     public static final String DELETE_CATEGORY_PARAMETER_PROPERTIES = "maintenance/delete_category_parameter";
32     public static final String PROPERTY_NAME = "owningEntity";
33     public static final String PROPERTY_FAMILY = "PARAMETER_STANDARDIZATION";
34
35     public static final String NEW_PARAMETER_PROPERTY_NAME = "oren";
36     public static final String UPDATE_PARAMETER_PROPERTY_NAME = "oren2";
37
38
39     @Test(groups = { "worksOnlyWithLocalhostVID" })
40     public void addCPProperties() throws IOException {
41         AddCategoryOptionsRequest request = new AddCategoryOptionsRequest();
42         String newParameter = UUID.randomUUID().toString();
43         request.options.add(newParameter);
44         addCPPropertiesRequest(HttpMethod.POST, request, HttpStatus.OK);
45         findPropertyNameInGetResponse(newParameter);
46
47     }
48
49     @Test(groups = { "worksOnlyWithLocalhostVID" })
50     public void updateCPProperties() throws IOException {
51         List<CategoryParameterOptionRep> props = getProps();
52         CategoryParameterOptionRep updateReq = new CategoryParameterOptionRep();
53         updateReq.setName(UPDATE_PARAMETER_PROPERTY_NAME);
54         updateReq.setId(props.get(props.size()-1).getId());
55         updateCPPropertiesRequest(HttpMethod.PUT, updateReq, HttpStatus.OK);
56         findPropertyNameInGetResponse(UPDATE_PARAMETER_PROPERTY_NAME);
57         CategoryParameterOption deleteReq = new CategoryParameterOption();
58         deleteReq.setName(UPDATE_PARAMETER_PROPERTY_NAME);
59         deleteCPPropertiesRequest(HttpMethod.POST, deleteReq, HttpStatus.OK);
60     }
61
62     @Test(groups = { "worksOnlyWithLocalhostVID" })
63     //this test call to MaintenanceController which is restricted to localhost, so it can not run on jenkins pipeline
64     public void getOrderedCPProperties() throws IOException {
65         // Ensure there is some initial data when checking that the list is sorted
66         CategoryParamsService categoryParamsService = new CategoryParamsService();
67         List<CategoryParameterOptionRep> props = getProps();
68         final List<String> propsNames = props.stream().map(CategoryParameterOptionRep::getName).collect(Collectors.toList());
69         assertEquals("The list isn't sorted", propsNames, propsNames.stream().sorted(String::compareToIgnoreCase).collect(Collectors.toList()));
70     }
71
72     private List<CategoryParameterOptionRep> getProps() throws IOException {
73         Response response = getCPPropertiesRequest(HttpMethod.GET, HttpStatus.OK);
74         String expectedJsonAsString = response.readEntity(String.class);
75         CategoryParametersResponse categoryParameterResponse =  objectMapper.readValue(expectedJsonAsString, CategoryParametersResponse.class);
76         List<CategoryParameterOptionRep> props = categoryParameterResponse.getCategoryParameters().get(PROPERTY_NAME);
77         return props;
78     }
79
80     private void findPropertyNameInGetResponse(String propertyName) throws IOException{
81         List<CategoryParameterOptionRep> props = getProps();
82         boolean found = false;
83         for (CategoryParameterOptionRep prop :
84                 props) {
85             if(prop.getName().equals(propertyName))
86                 found = true;
87         }
88         Assert.assertTrue(found);
89     }
90
91     private Response getCPPropertiesRequest(String method, HttpStatus exceptedHttpStatus) throws IOException {
92         UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromUri(uri).path("/"+GET_CATEGORY_PARAMETER_PROPERTIES)
93                 .queryParam("familyName", PROPERTY_FAMILY);
94         WebTarget webTarget = client.target(urlBuilder.toUriString());
95         Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).method(method, Entity.json(null));
96         TestUtils.assertHttpStatus(null, webTarget, response, exceptedHttpStatus);
97         return response;
98     }
99
100     private Response addCPPropertiesRequest(String method, AddCategoryOptionsRequest request, HttpStatus exceptedHttpStatus) throws IOException {
101         WebTarget webTarget = client.target(uri).path(GET_CATEGORY_PARAMETER_PROPERTIES+"/"+PROPERTY_NAME);
102         Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).method(method, Entity.json(request));
103         TestUtils.assertHttpStatus(request, webTarget, response, exceptedHttpStatus);
104         return response;
105     }
106
107     private Response updateCPPropertiesRequest(String method, CategoryParameterOptionRep request, HttpStatus exceptedHttpStatus) throws IOException {
108         WebTarget webTarget = client.target(uri).path(GET_CATEGORY_PARAMETER_PROPERTIES+"/"+PROPERTY_NAME);
109         Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).method(method, Entity.json(request));
110         TestUtils.assertHttpStatus(request, webTarget, response, exceptedHttpStatus);
111         return response;
112     }
113
114     private Response deleteCPPropertiesRequest(String method, CategoryParameterOption request, HttpStatus exceptedHttpStatus) throws IOException {
115         WebTarget webTarget = client.target(uri).path(DELETE_CATEGORY_PARAMETER_PROPERTIES+"/"+PROPERTY_NAME);
116         Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).method(method, Entity.json(request));
117         TestUtils.assertHttpStatus(request, webTarget, response, exceptedHttpStatus);
118         return response;
119     }
120 }