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.junit.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.PolicyManagerProps;
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 PolicyManagerAuthorizationInterceptorTest {
49 MockRestServiceServer mockServer;
50 RestTemplate restTemplate;
51 String policyManagerHostname = "http://0.0.0.1:8080/";
52 String policyManagerResPath = "services/service/networkInfrastructureResourcesSample/v1";
54 PolicyManagerAuthorizationInterceptor policyManagerInterceptor;
56 PolicyManagerProps props = new PolicyManagerProps();
59 * Does the setup for tests.
63 if (restTemplate == null) {
64 restTemplate = new RestTemplate();
66 props.setBasicAuth("Basic bnVsbDpudWxs");
67 props.setClientAuth("Basic bnVsbDpudWxs");
68 props.setEcompRequestId("xxuv");
69 props.setEnvironment("TEST");
70 restTemplate.getInterceptors().add(policyManagerInterceptor);
71 mockServer = MockRestServiceServer.bindTo(restTemplate).build();
75 public void testAuth() throws Exception {
76 mockServer.expect(ExpectedCount.once(), requestTo(policyManagerHostname + policyManagerResPath))
77 .andExpect(method(HttpMethod.POST))
78 .andExpect(header("Authorization", new String[] {"Basic bnVsbDpudWxs"}))
79 .andRespond(withSuccess("", MediaType.APPLICATION_JSON));
80 GetConfigRequest req = new GetConfigRequest();
81 RequestEntity<GetConfigRequest> re = RequestEntity.post(new URI(policyManagerHostname + policyManagerResPath))
82 .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).body(req);
83 ResponseEntity<GetConfigResponse> resp = restTemplate.exchange(re, GetConfigResponse.class);