1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.test.dme2;
25 import java.net.URISyntaxException;
26 import java.util.Properties;
28 import org.apache.log4j.Logger;
29 import org.json.JSONObject;
31 import com.att.aft.dme2.api.DME2Client;
32 import com.att.aft.dme2.api.DME2Exception;
33 import com.att.aft.dme2.internal.jackson.map.ObjectMapper;
35 import junit.framework.TestCase;
37 public class DME2ApiKeyTest extends TestCase {
38 private static final Logger LOGGER = Logger.getLogger(DME2ApiKeyTest.class);
42 protected Properties props;
45 protected void setUp() throws Exception {
47 System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS", "SSLv3,TLSv1,TLSv1.1");
48 System.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", "false");
49 System.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", "changeit");
50 this.props = LoadPropertyFile.getPropertyFileDataProducer();
51 String latitude = props.getProperty("Latitude");
52 String longitude = props.getProperty("Longitude");
53 String version = props.getProperty("Version");
54 String serviceName = props.getProperty("ServiceName");
55 String env = props.getProperty("Environment");
56 String partner = props.getProperty("Partner");
57 String protocol = props.getProperty("Protocol");
58 this.url = protocol + "://" + serviceName + "?" + "version=" + version + "&" + "envContext=" + env + "&"
59 + "routeOffer=" + partner + "&partner=BOT_R";
60 LoadPropertyFile.loadAFTProperties(latitude, longitude);
63 public void testCreateKey() {
64 LOGGER.info("Create Key test case initiated");
66 ApiKeyBean apiKeyBean = new ApiKeyBean("user1@us.att.com", "Creating Api Key.m");
68 System.out.println(url);
70 returnKey(apiKeyBean, url, props);
74 public String returnKey(ApiKeyBean apibean, String url, Properties props) {
78 LOGGER.info("Call to return key ");
79 DME2Client sender = new DME2Client(new URI(url), 5000L);
80 sender.setAllowAllHttpReturnCodes(true);
81 sender.setMethod(props.getProperty("MethodTypePost"));
82 sender.setSubContext(props.getProperty("SubContextPathGetCreateKeys"));
83 String jsonStringApiBean = new ObjectMapper().writeValueAsString(apibean);
84 sender.setPayload(jsonStringApiBean);
85 sender.addHeader("content-type", props.getProperty("contenttype"));
86 sender.setCredentials(props.getProperty("user"), props.getProperty("password"));
87 LOGGER.info("creating ApiKey");
88 reply = sender.sendAndWait(5000L);
89 System.out.println("reply: " + reply);
90 assertTrue(LoadPropertyFile.isValidJsonString(reply));
91 LOGGER.info("response =" + reply);
93 } catch (DME2Exception e) {
95 } catch (URISyntaxException e) {
97 } catch (Exception e) {
103 public void testGetAllKey() {
104 LOGGER.info("Test case Get All key initiated....");
106 DME2Client sender = new DME2Client(new URI(this.url), 5000L);
107 sender.setAllowAllHttpReturnCodes(true);
108 sender.setMethod(this.props.getProperty("MethodTypeGet"));
109 String subcontextPath = this.props.getProperty("SubContextPathGetApiKeys");
110 // sender.setSubContext(subcontextPath);
111 sender.setPayload("");
112 sender.addHeader("content-type", props.getProperty("contenttype"));
113 sender.setCredentials(props.getProperty("user"), props.getProperty("password"));
114 LOGGER.info("Fetching all keys");
115 String reply = sender.sendAndWait(5000L);
116 System.out.println(reply);
117 assertTrue(LoadPropertyFile.isValidJsonString(reply));
118 LOGGER.info("response =" + reply);
119 } catch (DME2Exception e) {
121 } catch (URISyntaxException e) {
123 } catch (Exception e) {
128 public void testGetOneKey() {/*
129 LOGGER.info("Test case get one key initiated");
130 ApiKeyBean apiKeyBean = new ApiKeyBean("user1@att.com", "Creating Api Key.m");
131 JSONObject jsonObj = new JSONObject(returnKey(apiKeyBean, url, props));
132 String apiKey = (String) jsonObj.get("key");
134 DME2Client sender = new DME2Client(new URI(url), 5000L);
135 sender.setAllowAllHttpReturnCodes(true);
136 sender.setMethod(props.getProperty("MethodTypeGet"));
137 sender.setSubContext(props.getProperty("SubContextPathGetOneKey") + apiKey);
138 sender.setPayload("");
139 sender.addHeader("content-type", props.getProperty("contenttype"));
140 sender.setCredentials(props.getProperty("user"), props.getProperty("password"));
142 LOGGER.info("Fetching details of api key: " + apiKey);
143 String reply = sender.sendAndWait(5000L);
144 System.out.println(reply);
145 assertTrue(LoadPropertyFile.isValidJsonString(reply));
146 LOGGER.info("response =" + reply);
148 } catch (DME2Exception e) {
150 } catch (URISyntaxException e) {
152 } catch (Exception e) {
157 // ............. test case update key is not applicable in
158 // DME2.................//
159 public void testUpdateKey() {/*
160 ApiKeyBean apiKeyBean = new ApiKeyBean("user1@att.com", "Creating Api Key.m");
162 JSONObject jsonObj = new JSONObject(returnKey(apiKeyBean, url, props));
163 String apiKey = (String) jsonObj.get("key");
165 DME2Client sender = new DME2Client(new URI(url), 5000L);
166 sender.setAllowAllHttpReturnCodes(true);
167 String p = props.getProperty("MethodTypePut");
169 String s = props.getProperty("SubContextPathUpdateKeys") + apiKey;
170 sender.setSubContext(s);
172 String jsonStringApiBean = new ObjectMapper()
173 .writeValueAsString(new ApiKeyBean("user1@att.com", "updating key"));
174 sender.setPayload(jsonStringApiBean);
175 System.out.println(jsonStringApiBean);
176 String c = props.getProperty("contenttype");
177 sender.addHeader("content-type", c);
178 sender.setCredentials(props.getProperty("keyUser"), props.getProperty("keyPass"));
180 System.out.println("creating ApiKey");
181 String reply = sender.sendAndWait(5000L);
182 assertNotNull(reply);
183 System.out.println("response =" + reply);
185 } catch (DME2Exception e) {
187 } catch (URISyntaxException e) {
189 } catch (Exception e) {
194 // ............. test case delete key is not applicable in
195 // DME2.................//
197 public void testDeleteKey() {/*
198 ApiKeyBean apiKeyBean = new ApiKeyBean("user1@att.com", "Creating Api Key.m");
200 JSONObject jsonObj = new JSONObject(returnKey(apiKeyBean, url, props));
201 String apiKey = (String) jsonObj.get("key");
203 DME2Client sender = new DME2Client(new URI(url), 5000L);
204 sender.setAllowAllHttpReturnCodes(true);
205 String p = props.getProperty("MethodTypeDelete");
207 String s = props.getProperty("SubContextPathDeleteteKeys") + apiKey;
208 sender.setSubContext(s);
210 sender.setPayload(""); // System.out.println(jsonStringApiBean);
211 String c = props.getProperty("contenttype");
212 sender.addHeader("content-type", c);
213 sender.setCredentials(props.getProperty("keyUser"), props.getProperty("keyPass"));
215 System.out.println("creating ApiKey");
216 String reply = sender.sendAndWait(5000L);
217 assertNotNull(reply);
218 System.out.println("response =" + reply);
220 } catch (DME2Exception e) {
222 } catch (URISyntaxException e) {
224 } catch (Exception e) {