summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/sparky/dal/proxy/processor/AaiUiProxyProcessorTest.java
blob: 82713f5eab45b279bada56d5f94947d496efe526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
 * ============LICENSE_START=======================================================
 * SPARKY (AAI UI service)
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property.
 * Copyright © 2017 Amdocs
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP and OpenECOMP are trademarks
 * and service marks of AT&T Intellectual Property.
 */
package org.onap.aai.sparky.dal.proxy.processor;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.codehaus.groovy.grails.web.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.AdditionalMatchers;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.onap.aai.restclient.client.OperationResult;
import org.onap.aai.restclient.client.RestClient;
import org.onap.aai.restclient.enums.RestAuthenticationMode;
import org.onap.aai.sparky.dal.rest.RestClientConstructionException;
import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
import org.restlet.data.Status;

public class AaiUiProxyProcessorTest {

  private RestClient client = null;
  private OperationResult successResult = null;
  OperationResult failureResult = null;
  private Exchange mockExchange;
  private Message mockRequestMessage;
  private Message mockResponseMessage;

  private HttpServletRequest mockHttpServletRequest;
  
  private AaiUiProxyProcessor aaiUiProxyProcessor;

  private String goodBeTargetUrl = "https://0.0.0.0:8000/services/routerService/servicegraph";
  private String badBeTargetUrl = "https://0.0.0.0:8000/aservicegraph";
  private String goodDrTargetUrl = "https://0.0.0.0:9502/ui-request/servicegraph";

  String successResponsePayload = "good-payload";
  String failureResponsePayload = "Server Error";

  @Before
  public void init()throws RestClientConstructionException {
    client = Mockito.mock(RestClient.class);
    mockExchange = Mockito.mock(Exchange.class);
    mockRequestMessage = Mockito.mock(Message.class);
    mockResponseMessage = Mockito.mock(Message.class);
    mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);


    RestEndpointConfig config = new RestEndpointConfig(); 
    config.setRestAuthenticationMode(RestAuthenticationMode.SSL_BASIC);
    aaiUiProxyProcessor = new AaiUiProxyProcessor(config,"ui-request");

    initializeMocks(getProxyRequestJson("someHashValue"));
    aaiUiProxyProcessor.setClient(client);
  }

  @Test
  public void testProxyMessage_successPath() {
    OperationResult successResultSpy = Mockito.spy(successResult);
    Mockito.when(client.post(Mockito.eq(goodDrTargetUrl), Mockito.anyString(), Mockito.anyMap(),
        Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
        .thenReturn(successResultSpy);

    Mockito.when(mockExchange.getIn().getHeader(Exchange.HTTP_URI)).thenReturn(goodBeTargetUrl);
    Mockito.when(mockExchange.getIn().getBody(HttpServletRequest.class)).thenReturn(mockHttpServletRequest);
    aaiUiProxyProcessor.proxyMessage(mockExchange);

    //Mockito.verify(successResultSpy).getResult();
    //assertEquals(Status.SUCCESS_OK.getCode(), aaiUiProxyProcessor.getOperationResult().getResultCode());
  }

  @Test
  public void testProxyMessage_failurePath() {
    OperationResult failureResultSpy = Mockito.spy(failureResult);
    Mockito.when(client.post(AdditionalMatchers.not(Matchers.eq(goodDrTargetUrl)),
        Mockito.anyString(), Mockito.anyMap(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE),
        Mockito.eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(failureResultSpy);

    Mockito.when(mockExchange.getIn().getHeader(Exchange.HTTP_URI)).thenReturn(badBeTargetUrl);
    Mockito.when(mockExchange.getIn().getBody(HttpServletRequest.class)).thenReturn(mockHttpServletRequest);
    aaiUiProxyProcessor.proxyMessage(mockExchange);

    Mockito.verify(failureResultSpy).getFailureCause();
    assertEquals(Status.SERVER_ERROR_INTERNAL.getCode(), aaiUiProxyProcessor.getOperationResult().getResultCode());
  }

  private String getProxyRequestJson(String hashId) {
    JSONObject root = new JSONObject();
    root.put("hashId", hashId);
    return root.toString();

  }

  @SuppressWarnings("unchecked")
  private void initializeMocks(String requestPayload) {

    client = Mockito.mock(RestClient.class);
    successResult = new OperationResult(200, successResponsePayload);
    failureResult = new OperationResult(500, failureResponsePayload);
    failureResult.setFailureCause(failureResponsePayload);

    Mockito.when(client.post(Mockito.eq(goodDrTargetUrl), Mockito.anyString(), Mockito.anyMap(),
        Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
        .thenReturn(successResult);

    Mockito.when(client.post(AdditionalMatchers.not(Matchers.eq(goodDrTargetUrl)),
        Mockito.anyString(), Mockito.anyMap(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE),
        Mockito.eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(failureResult);
    
    Mockito.when(mockHttpServletRequest.getRequestURI()).thenReturn("fakeUri");
    Mockito.when(mockHttpServletRequest.getLocalPort()).thenReturn(8001);

    Mockito.when(mockExchange.getIn()).thenReturn(mockRequestMessage);
    Mockito.when(mockExchange.getOut()).thenReturn(mockResponseMessage);
  }

}