1 package org.onap.vid.api;
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;
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;
23 import static org.testng.AssertJUnit.assertEquals;
26 //This is integration test that require running tomcat
27 public class CategoryParametersApiTest extends BaseApiTest {
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";
33 public static final String NEW_PARAMETER_PROPERTY_NAME = "oren";
34 public static final String UPDATE_PARAMETER_PROPERTY_NAME = "oren2";
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);
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);
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()));
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);
78 private void findPropertyNameInGetResponse(String propertyName) throws IOException{
79 List<CategoryParameterOptionRep> props = getProps();
80 boolean found = false;
81 for (CategoryParameterOptionRep prop :
83 if(prop.getName().equals(propertyName))
86 Assert.assertTrue(found);
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);
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);
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);
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);