AT&T 1712 and 1802 release code
[so.git] / common / src / test / java / org / openecomp / mso / client / grm / GRMClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.mso.client.grm;
22
23 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
24 import static org.junit.Assert.*;
25
26 import java.io.File;
27 import java.nio.file.Files;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import javax.ws.rs.core.MediaType;
32
33 import org.junit.Ignore;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.openecomp.mso.client.grm.exceptions.GRMClientCallFailed;
37 import org.openecomp.mso.client.grm.GRMClient;
38 import org.openecomp.mso.client.grm.beans.ServiceEndPointRequest;
39 import org.openecomp.mso.client.grm.beans.OperationalInfo;
40 import org.openecomp.mso.client.grm.beans.Property;
41 import org.openecomp.mso.client.grm.beans.ServiceEndPoint;
42 import org.openecomp.mso.client.grm.beans.ServiceEndPointList;
43 import org.openecomp.mso.client.grm.beans.ServiceEndPointLookupRequest;
44 import org.openecomp.mso.client.grm.beans.Version;
45
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.github.tomakehurst.wiremock.junit.WireMockRule;
48 import static com.github.tomakehurst.wiremock.client.WireMock.post;
49 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
50 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
51
52 public class GRMClientTest {
53         
54         @Rule
55         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(28090));
56         
57         private ObjectMapper mapper = new ObjectMapper();
58         
59         @Test
60         public void testFind() throws Exception {
61                 String endpoints = getFileContentsAsString("__files/grm/endpoints.json");
62                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
63                         .willReturn(aResponse()
64                                 .withStatus(200)
65                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
66                                 .withBody(endpoints)));
67
68                 
69                 GRMClient client = new GRMClient();
70                 ServiceEndPointList sel = client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
71                 List<ServiceEndPoint> list = sel.getServiceEndPointList();
72                 assertEquals(3, list.size());
73         }
74         
75         @Test(expected = GRMClientCallFailed.class) 
76         public void testFindFail() throws Exception {
77                 
78                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
79                         .willReturn(aResponse()
80                                 .withStatus(400)
81                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
82                                 .withBody("")));
83                 
84                 GRMClient client = new GRMClient();
85                 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
86         }
87         
88         @Ignore
89         @Test
90         public void testAdd() throws Exception {
91                 
92                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
93                         .willReturn(aResponse()
94                                 .withStatus(202)
95                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
96                                 .withBody("test")));
97                 wireMockRule.addMockServiceRequestListener((request, response) -> {
98                         System.out.println("URL Requested => " + request.getAbsoluteUrl());
99                         System.out.println("Request Body => " + request.getBodyAsString());
100                         System.out.println("Request Headers => " + request.getHeaders().toString());
101                         System.out.println("Response Status => " + response.getStatus());
102                         System.out.println("Response Body => " + response.getBodyAsString());
103                 });     
104                 
105                 Version ver = new Version();
106                 ver.setMajor(1);
107                 ver.setMinor(0);
108                 ver.setPatch("0");
109
110                 ServiceEndPoint sep = new ServiceEndPoint();
111                 sep.setName("TEST.ECOMP_PSL.Inventory");
112                 sep.setVersion(ver);
113                 sep.setHostAddress("127.0.0.1");
114                 sep.setListenPort("8080");
115                 sep.setLatitude("37.7022");
116                 sep.setLongitude("121.9358");
117                 sep.setContextPath("/");
118                 sep.setRouteOffer("TEST");
119                 
120                 OperationalInfo operInfo = new OperationalInfo();
121                 operInfo.setCreatedBy("edge");
122                 operInfo.setUpdatedBy("edge");
123                 
124                 sep.setOperationalInfo(operInfo);
125                 
126                 Property prop1 = new Property();
127                 prop1.setName("Environment");
128                 prop1.setValue("TEST");
129                 
130                 Property prop2 = new Property();
131                 prop2.setName("cpfrun_cluster_name");
132                 prop2.setValue("testcase_cluster_no_cluster");
133                 
134                 List<Property> props = new ArrayList<Property>();
135                 props.add(prop1);
136                 props.add(prop2);
137                 
138                 sep.setProperties(props);
139
140                 ServiceEndPointRequest request = new ServiceEndPointRequest();
141                 request.setEnv("DEV");
142                 request.setServiceEndPoint(sep);
143                 
144                 System.out.println("Request in JSON: " + mapper.writeValueAsString(request));
145                 
146                 GRMClient client = new GRMClient();
147                 client.addServiceEndPoint(request);
148         }
149         
150         @Test(expected = GRMClientCallFailed.class)
151         public void testAddFail() throws Exception {
152                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
153                                 .willReturn(aResponse()
154                                         .withStatus(404)
155                                         .withHeader("Content-Type", MediaType.APPLICATION_JSON)
156                                         .withBody("test")));
157                 ServiceEndPointRequest request = new ServiceEndPointRequest();
158                 GRMClient client = new GRMClient();
159                 client.addServiceEndPoint(request);
160         }
161
162         @Test
163         public void testBuildServiceEndPointLookupRequest() {
164                 GRMClient client = new GRMClient();
165                 ServiceEndPointLookupRequest request = client.buildServiceEndPointlookupRequest("TEST.ECOMP_PSL.Inventory", 1, "DEV");
166                 assertEquals("TEST.ECOMP_PSL.Inventory", request.getServiceEndPoint().getName());
167                 assertEquals(Integer.valueOf(1), Integer.valueOf(request.getServiceEndPoint().getVersion().getMajor()));
168                 assertEquals("DEV", request.getEnv());
169                 
170         }
171         
172         protected String getFileContentsAsString(String fileName) {
173                 String content = "";
174                 try {
175                         ClassLoader classLoader = this.getClass().getClassLoader();
176                         File file = new File(classLoader.getResource(fileName).getFile());
177                         content = new String(Files.readAllBytes(file.toPath()));
178                 }
179                 catch(Exception e) {
180                         e.printStackTrace();
181                         System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());
182                 }
183                 return content;
184         }
185 }