clean MR codebase
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / mr / test / dme2 / DME2ApiKeyTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.mr.test.dme2;
23
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.Properties;
27
28 import org.apache.log4j.Logger;
29 import org.json.JSONObject;
30
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;
34
35 import junit.framework.TestCase;
36
37 public class DME2ApiKeyTest extends TestCase {
38         private static final Logger LOGGER = Logger.getLogger(DME2ApiKeyTest.class);
39
40         protected String url;
41
42         protected Properties props;
43
44         @Override
45         protected void setUp() throws Exception {
46                 super.setUp();
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);
61         }
62
63         public void testCreateKey() {
64                 LOGGER.info("Create Key test case initiated");
65
66                 ApiKeyBean apiKeyBean = new ApiKeyBean("user1@onap.com", "Creating Api Key.m");
67
68                 System.out.println(url);
69
70                 returnKey(apiKeyBean, url, props);
71
72         }
73
74         public String returnKey(ApiKeyBean apibean, String url, Properties props) {
75
76                 String reply = null;
77                 try {
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);
92
93                 } catch (DME2Exception e) {
94                         e.printStackTrace();
95                 } catch (URISyntaxException e) {
96                         e.printStackTrace();
97                 } catch (Exception e) {
98                         e.printStackTrace();
99                 }
100                 return reply;
101         }
102
103         public void testGetAllKey() {
104                 LOGGER.info("Test case Get All key initiated....");
105                 try {
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) {
120                         e.printStackTrace();
121                 } catch (URISyntaxException e) {
122                         e.printStackTrace();
123                 } catch (Exception e) {
124                         e.printStackTrace();
125                 }
126         }
127
128
129
130 }