replace all fixed wiremock ports
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / client / namingservice / NamingClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.namingservice;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
25 import static com.github.tomakehurst.wiremock.client.WireMock.post;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
27 import static org.junit.Assert.assertTrue;
28
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.apache.http.HttpStatus;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.onap.namingservice.model.Deleteelement;
38 import org.onap.namingservice.model.Element;
39 import org.onap.namingservice.model.NameGenDeleteRequest;
40 import org.onap.namingservice.model.NameGenRequest;
41 import org.onap.so.BaseIntegrationTest;
42 import org.onap.so.client.exception.BadResponseException;
43 import org.springframework.beans.factory.annotation.Autowired;
44
45 import com.fasterxml.jackson.core.JsonProcessingException;
46
47 public class NamingClientTest extends BaseIntegrationTest{
48         @Autowired
49         NamingClient client;
50         @Autowired
51         NamingRequestObjectBuilder requestBuilder;
52         @Rule
53         public ExpectedException thrown = ExpectedException.none();
54         
55         @Test
56         public void assignNameGenRequest() throws BadResponseException, IOException{
57                 wireMockServer.stubFor(post(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
58                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
59                                 .withBodyFile("NamingClient/AssignResponse.json")
60                                 .withStatus(HttpStatus.SC_ACCEPTED)));
61                 
62                 NameGenRequest request = assignSetup();
63                 String response = client.postNameGenRequest(request);
64                 assertTrue(response.equals("$vnf-name"));
65         }
66         @Test
67         public void assignNameGenRequestError() throws BadResponseException, IOException{
68                 wireMockServer.stubFor(post(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
69                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
70                                 .withBodyFile("NamingClient/ErrorResponse.json")
71                                 .withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
72                 
73                 thrown.expect(BadResponseException.class);
74                 thrown.expectMessage("Error from Naming Service: External Key is required and must be unique");
75                 NameGenRequest request = assignSetup();
76                 client.postNameGenRequest(request);
77         }
78         @Test
79         public void unassignNameGenRequest() throws BadResponseException, IOException{
80                 wireMockServer.stubFor(delete(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
81                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
82                                 .withBodyFile("NamingClient/UnassignResponse.json")
83                                 .withStatus(HttpStatus.SC_ACCEPTED)));
84                 
85                 String response = client.deleteNameGenRequest(unassignSetup());
86                 assertTrue(response.equals(""));
87         }
88         @Test
89         public void unassignNameGenRequestError() throws BadResponseException, IOException{
90                 wireMockServer.stubFor(delete(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
91                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
92                                 .withBodyFile("NamingClient/ErrorResponse.json")
93                                 .withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
94                 
95                 thrown.expect(BadResponseException.class);
96                 thrown.expectMessage("Error from Naming Service: External Key is required and must be unique");
97                 client.deleteNameGenRequest(unassignSetup());
98         }
99         
100         public NameGenRequest assignSetup() throws JsonProcessingException{
101                 NameGenRequest request = new NameGenRequest();
102                 List<Element> elements = new ArrayList<>();
103                 Element testElement = new Element();
104                 testElement = requestBuilder.elementMapper("SomeUniqueValue", "SDNC_Policy.Config_MS_1806SRIOV_VNATJson.4.xml", "VNF", "nfNamingCode", "vnf_name");
105                 elements.add(testElement);
106                 request = requestBuilder.nameGenRequestMapper(elements);
107                 return request;
108         }
109         public NameGenDeleteRequest unassignSetup() throws JsonProcessingException{
110                 NameGenDeleteRequest request = new NameGenDeleteRequest();
111                 List<Deleteelement> deleteElements = new ArrayList<>();
112                 Deleteelement testElement = new Deleteelement();
113                 testElement = requestBuilder.deleteElementMapper("instanceGroupId");
114                 deleteElements.add(testElement);
115                 request = requestBuilder.nameGenDeleteRequestMapper(deleteElements);
116                 return request;
117         }
118 }