40e507ea84bb2a35d6cd1ea4c12f782e5b85798e
[ccsdk/apps.git] / ms / neng / src / test / java / org / onap / ccsdk / apps / ms / neng / core / rs / interceptors / AaiAuthorizationInterceptorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.ccsdk.apps.ms.neng.core.rs.interceptors;
22
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;
28
29 import java.net.URI;
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;
46
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";
53
54     @InjectMocks
55     AaiAuthorizationInterceptor aaiAuthorizationInterceptor;
56     @Spy
57     AaiProps props = new AaiProps();
58
59     /**
60      * Does the setup.
61      */
62     @Before
63     public void setUp() {
64         if (restTemplate == null) {
65             restTemplate = new RestTemplate();
66         }
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();
75     }
76
77     @Test
78     public void testAuth() throws Exception {
79
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);
87         mockServer.verify();
88         assertNotNull(resp);
89     }
90 }