d6f8c70aa1ab6085e27af73090bbaf6b8a484e35
[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.stubFor;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.apache.http.HttpStatus;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.onap.namingservice.model.Deleteelement;
39 import org.onap.namingservice.model.Element;
40 import org.onap.namingservice.model.NameGenDeleteRequest;
41 import org.onap.namingservice.model.NameGenRequest;
42 import org.onap.so.BaseIntegrationTest;
43 import org.onap.so.client.exception.BadResponseException;
44 import org.springframework.beans.factory.annotation.Autowired;
45
46 import com.fasterxml.jackson.core.JsonProcessingException;
47
48 public class NamingClientTest extends BaseIntegrationTest{
49         @Autowired
50         NamingClient client;
51         @Autowired
52         NamingRequestObjectBuilder requestBuilder;
53         @Rule
54         public ExpectedException thrown = ExpectedException.none();
55         
56         @Test
57         public void assignNameGenRequest() throws BadResponseException, IOException{
58                 stubFor(post(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
59                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
60                                 .withBodyFile("NamingClient/AssignResponse.json")
61                                 .withStatus(HttpStatus.SC_ACCEPTED)));
62                 
63                 NameGenRequest request = assignSetup();
64                 String response = client.postNameGenRequest(request);
65                 assertTrue(response.equals("$vnf-name"));
66         }
67         @Test
68         public void assignNameGenRequestError() throws BadResponseException, IOException{
69                 stubFor(post(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
70                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
71                                 .withBodyFile("NamingClient/ErrorResponse.json")
72                                 .withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
73                 
74                 thrown.expect(BadResponseException.class);
75                 thrown.expectMessage("Error from Naming Service: External Key is required and must be unique");
76                 NameGenRequest request = assignSetup();
77                 client.postNameGenRequest(request);
78         }
79         @Test
80         public void unassignNameGenRequest() throws BadResponseException, IOException{
81                 stubFor(delete(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
82                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
83                                 .withBodyFile("NamingClient/UnassignResponse.json")
84                                 .withStatus(HttpStatus.SC_ACCEPTED)));
85                 
86                 String response = client.deleteNameGenRequest(unassignSetup());
87                 assertTrue(response.equals(""));
88         }
89         @Test
90         public void unassignNameGenRequestError() throws BadResponseException, IOException{
91                 stubFor(delete(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
92                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
93                                 .withBodyFile("NamingClient/ErrorResponse.json")
94                                 .withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
95                 
96                 thrown.expect(BadResponseException.class);
97                 thrown.expectMessage("Error from Naming Service: External Key is required and must be unique");
98                 client.deleteNameGenRequest(unassignSetup());
99         }
100         
101         public NameGenRequest assignSetup() throws JsonProcessingException{
102                 NameGenRequest request = new NameGenRequest();
103                 List<Element> elements = new ArrayList<>();
104                 Element testElement = new Element();
105                 testElement = requestBuilder.elementMapper("SomeUniqueValue", "SDNC_Policy.Config_MS_1806SRIOV_VNATJson.4.xml", "VNF", "nfNamingCode", "vnf_name");
106                 elements.add(testElement);
107                 request = requestBuilder.nameGenRequestMapper(elements);
108                 return request;
109         }
110         public NameGenDeleteRequest unassignSetup() throws JsonProcessingException{
111                 NameGenDeleteRequest request = new NameGenDeleteRequest();
112                 List<Deleteelement> deleteElements = new ArrayList<>();
113                 Deleteelement testElement = new Deleteelement();
114                 testElement = requestBuilder.deleteElementMapper("instanceGroupId");
115                 deleteElements.add(testElement);
116                 request = requestBuilder.nameGenDeleteRequestMapper(deleteElements);
117                 return request;
118         }
119 }