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