2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.rest;
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;
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;
45 public class PapRestControllerV1Test {
48 SecurityContext mockSecurityContext;
51 PapRestControllerV1 mockController;
53 private AutoCloseable closeable;
54 private ResponseBuilder bldr;
58 bldr = Response.status(Response.Status.OK);
59 closeable = MockitoAnnotations.openMocks(this);
63 public void after() throws Exception {
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);
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));
84 public void testAddLoggingHeaders_Null() {
85 Response resp = mockController.addLoggingHeaders(bldr, null).build();
86 assertNotNull(resp.getHeaderString(PapRestControllerV1.REQUEST_ID_NAME));
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));
97 public void testGetPrincipal() {
98 assertThat(new PapRestControllerV1().getPrincipal()).isEmpty();
100 Principal mockUser = mock(Principal.class, new Returns("myFakeUser"));
101 when(mockSecurityContext.getUserPrincipal()).thenReturn(mockUser);
103 assertEquals("myFakeUser", mockController.getPrincipal());