2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.namingservice;
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;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
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;
46 import com.fasterxml.jackson.core.JsonProcessingException;
48 public class NamingClientTest extends BaseIntegrationTest{
52 NamingRequestObjectBuilder requestBuilder;
54 public ExpectedException thrown = ExpectedException.none();
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)));
63 NameGenRequest request = assignSetup();
64 String response = client.postNameGenRequest(request);
65 assertTrue(response.equals("$vnf-name"));
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)));
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);
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)));
86 String response = client.deleteNameGenRequest(unassignSetup());
87 assertTrue(response.equals(""));
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)));
96 thrown.expect(BadResponseException.class);
97 thrown.expectMessage("Error from Naming Service: External Key is required and must be unique");
98 client.deleteNameGenRequest(unassignSetup());
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);
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);