DMAAP-MR - Merge MR repos
[dmaap/messagerouter/messageservice.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 com.att.aft.dme2.api.DME2Client;
25 import com.att.aft.dme2.api.DME2Exception;
26 import com.att.aft.dme2.internal.jackson.map.ObjectMapper;
27 import junit.framework.TestCase;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.util.Properties;
34
35 public class DME2ApiKeyTest extends TestCase {
36
37     private static final Logger LOGGER = LogManager.getLogger(DME2ApiKeyTest.class);
38
39     protected String url;
40
41     protected Properties props;
42
43     @Override
44     protected void setUp() throws Exception {
45         super.setUp();
46         System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS", "SSLv3,TLSv1,TLSv1.1");
47         System.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", "false");
48         System.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", "changeit");
49         this.props = LoadPropertyFile.getPropertyFileDataProducer();
50         String latitude = props.getProperty("Latitude");
51         String longitude = props.getProperty("Longitude");
52         String version = props.getProperty("Version");
53         String serviceName = props.getProperty("ServiceName");
54         String env = props.getProperty("Environment");
55         String partner = props.getProperty("Partner");
56         String protocol = props.getProperty("Protocol");
57         this.url =
58             protocol + "://" + serviceName + "?" + "version=" + version + "&" + "envContext=" + env
59                 + "&"
60                 + "routeOffer=" + partner + "&partner=BOT_R";
61         LoadPropertyFile.loadAFTProperties(latitude, longitude);
62     }
63
64     public void testCreateKey() {
65         LOGGER.info("Create Key test case initiated");
66         ApiKeyBean apiKeyBean = new ApiKeyBean("user1@onap.com", "Creating Api Key.m");
67         System.out.println(url);
68         returnKey(apiKeyBean, url, props);
69     }
70
71     public String returnKey(ApiKeyBean apibean, String url, Properties props) {
72         String reply = null;
73         try {
74             LOGGER.info("Call to return key ");
75             DME2Client sender = new DME2Client(new URI(url), 5000L);
76             sender.setAllowAllHttpReturnCodes(true);
77             sender.setMethod(props.getProperty("MethodTypePost"));
78             sender.setSubContext(props.getProperty("SubContextPathGetCreateKeys"));
79             String jsonStringApiBean = new ObjectMapper().writeValueAsString(apibean);
80             sender.setPayload(jsonStringApiBean);
81             sender.addHeader("content-type", props.getProperty("contenttype"));
82             sender.setCredentials(props.getProperty("user"), props.getProperty("password"));
83             LOGGER.info("creating ApiKey");
84             reply = sender.sendAndWait(5000L);
85             System.out.println("reply: " + reply);
86             assertTrue(LoadPropertyFile.isValidJsonString(reply));
87             LOGGER.info("response =" + reply);
88         } catch (DME2Exception e) {
89             e.printStackTrace();
90         } catch (URISyntaxException e) {
91             e.printStackTrace();
92         } catch (Exception e) {
93             e.printStackTrace();
94         }
95         return reply;
96     }
97
98     public void testGetAllKey() {
99         LOGGER.info("Test case Get All key initiated....");
100         try {
101             DME2Client sender = new DME2Client(new URI(this.url), 5000L);
102             sender.setAllowAllHttpReturnCodes(true);
103             sender.setMethod(this.props.getProperty("MethodTypeGet"));
104             String subcontextPath = this.props.getProperty("SubContextPathGetApiKeys");
105             // sender.setSubContext(subcontextPath);
106             sender.setPayload("");
107             sender.addHeader("content-type", props.getProperty("contenttype"));
108             sender.setCredentials(props.getProperty("user"), props.getProperty("password"));
109             LOGGER.info("Fetching all keys");
110             String reply = sender.sendAndWait(5000L);
111             System.out.println(reply);
112             assertTrue(LoadPropertyFile.isValidJsonString(reply));
113             LOGGER.info("response =" + reply);
114         } catch (DME2Exception e) {
115             e.printStackTrace();
116         } catch (URISyntaxException e) {
117             e.printStackTrace();
118         } catch (Exception e) {
119             e.printStackTrace();
120         }
121     }
122 }