2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.apps.ms.neng.core.rs.interceptors;
23 import static org.junit.Assert.assertNotNull;
24 import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
25 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
26 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
27 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Spy;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequest;
37 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigResponse;
38 import org.onap.ccsdk.apps.ms.neng.extinf.props.AaiProps;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.RequestEntity;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.test.web.client.ExpectedCount;
44 import org.springframework.test.web.client.MockRestServiceServer;
45 import org.springframework.web.client.RestTemplate;
47 @RunWith(MockitoJUnitRunner.class)
48 public class AaiAuthorizationInterceptorTest {
49 MockRestServiceServer mockServer;
50 RestTemplate restTemplate;
51 String aaiHostName = "http://0.0.0.1:8080/";
52 String aaiPath = "services/service/networkInfrastructureResourcesSample/v1";
55 AaiAuthorizationInterceptor aaiAuthorizationInterceptor;
57 AaiProps props = new AaiProps();
64 if (restTemplate == null) {
65 restTemplate = new RestTemplate();
67 props.setAccept("application/json");
68 props.setCert("c:/certs");
69 props.setCertPassword("password");
70 props.setFromAppId("namegen-ms");
71 props.setUriBase("https://localhost:8080/aai/v13/");
72 props.setTransactionId("X12345YV");
73 restTemplate.getInterceptors().add(aaiAuthorizationInterceptor);
74 mockServer = MockRestServiceServer.bindTo(restTemplate).build();
78 public void testAuth() throws Exception {
80 mockServer.expect(ExpectedCount.once(), requestTo(aaiHostName + aaiPath)).andExpect(method(HttpMethod.POST))
81 .andExpect(header("x-FromAppId", new String[] {"namegen-ms"}))
82 .andRespond(withSuccess("", MediaType.APPLICATION_JSON));
83 GetConfigRequest req = new GetConfigRequest();
84 RequestEntity<GetConfigRequest> re = RequestEntity.post(new URI(aaiHostName + aaiPath))
85 .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).body(req);
86 ResponseEntity<GetConfigResponse> resp = restTemplate.exchange(re, GetConfigResponse.class);