Merge "PolicyAudit creation when deploy/undeploy triggered."
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / PapRestControllerV1Test.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.security.Principal;
31 import java.util.UUID;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.Response.ResponseBuilder;
36 import javax.ws.rs.core.SecurityContext;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.mockito.internal.stubbing.answers.Returns;
44
45 public class PapRestControllerV1Test {
46
47     @Mock
48     SecurityContext mockSecurityContext;
49
50     @InjectMocks
51     PapRestControllerV1 mockController;
52
53     private AutoCloseable closeable;
54     private ResponseBuilder bldr;
55
56     @Before
57     public void setUp() {
58         bldr = Response.status(Response.Status.OK);
59         closeable = MockitoAnnotations.openMocks(this);
60     }
61
62     @After
63     public void after() throws Exception {
64         closeable.close();
65     }
66
67     @Test
68     public void testProduces() {
69         Produces annotation = PapRestControllerV1.class.getAnnotation(Produces.class);
70         assertNotNull(annotation);
71         assertThat(annotation.value()).contains(MediaType.APPLICATION_JSON)
72                         .contains(PapRestControllerV1.APPLICATION_YAML);
73     }
74
75     @Test
76     public void testAddVersionControlHeaders() {
77         Response resp = mockController.addVersionControlHeaders(bldr).build();
78         assertEquals("0", resp.getHeaderString(PapRestControllerV1.VERSION_MINOR_NAME));
79         assertEquals("0", resp.getHeaderString(PapRestControllerV1.VERSION_PATCH_NAME));
80         assertEquals("1.0.0", resp.getHeaderString(PapRestControllerV1.VERSION_LATEST_NAME));
81     }
82
83     @Test
84     public void testAddLoggingHeaders_Null() {
85         Response resp = mockController.addLoggingHeaders(bldr, null).build();
86         assertNotNull(resp.getHeaderString(PapRestControllerV1.REQUEST_ID_NAME));
87     }
88
89     @Test
90     public void testAddLoggingHeaders_NonNull() {
91         UUID uuid = UUID.randomUUID();
92         Response resp = mockController.addLoggingHeaders(bldr, uuid).build();
93         assertEquals(uuid.toString(), resp.getHeaderString(PapRestControllerV1.REQUEST_ID_NAME));
94     }
95
96     @Test
97     public void testGetPrincipal() {
98         assertThat(new PapRestControllerV1().getPrincipal()).isEmpty();
99
100         Principal mockUser = mock(Principal.class, new Returns("myFakeUser"));
101         when(mockSecurityContext.getUserPrincipal()).thenReturn(mockUser);
102
103         assertEquals("myFakeUser", mockController.getPrincipal());
104     }
105 }