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====================================================
22 package org.onap.aaf.auth.rserv.test;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
28 import java.io.IOException;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
33 import org.junit.Test;
34 import org.onap.aaf.auth.rserv.HttpCode;
35 import org.onap.aaf.auth.rserv.TypedCode;
36 import org.onap.aaf.misc.env.TransJAXB;
37 import org.onap.aaf.misc.env.impl.EnvFactory;
41 * Test the functioning of the "Content" class, which holds, and routes to the right code based on Accept values
43 public class JU_Content {
47 public void test() throws Exception {
48 final String BOOL = "Boolean";
49 final String XML = "XML";
50 TransJAXB trans = EnvFactory.newTrans();
52 HttpCode<TransJAXB, String> cBool = new HttpCode<TransJAXB,String>(BOOL,"Standard String") {
54 public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {
56 resp.getOutputStream().write(context.getBytes());
57 } catch (IOException e) {
62 HttpCode<TransJAXB,String> cXML = new HttpCode<TransJAXB,String>(XML, "Standard String") {
64 public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {
66 resp.getOutputStream().write(context.getBytes());
67 } catch (IOException e) {
72 TypedCode<TransJAXB> ct = new TypedCode<TransJAXB>()
73 .add(cBool,"application/" + Boolean.class.getName()+"+xml;charset=utf8;version=1.1")
74 .add(cXML,"application/xml;q=.9");
75 String expected = "application/java.lang.Boolean+xml;charset=utf8;version=1.1,application/xml;q=0.9";
76 assertEquals(expected,ct.toString());
78 //BogusReq req = new BogusReq();
79 //expected = (expected);
80 //HttpServletResponse resp = new BogusResp();
82 assertNotNull("Same Content String and Accept String",ct.prep(trans,expected));
84 //expects Null (not run)
85 // A Boolean xml that must have charset utf8 and match version 1.2 or greater
86 expected = ("application/java.lang.Boolean+xml;charset=utf8;version=1.2");
87 assertNull("Accept Minor Version greater than Content Minor Version",ct.prep(trans,expected));
89 // Same with (too many) spaces
90 expected = (" application/java.lang.Boolean+xml ; charset = utf8 ; version = 1.2 ");
91 assertNull("Accept Minor Version greater than Content Minor Version",ct.prep(trans,expected));
93 //expects Null (not run)
94 expected = ("application/java.lang.Boolean+xml;charset=utf8;version=2.1");
95 assertNull("Major Versions not the same",ct.prep(trans,expected));
97 expected = ("application/java.lang.Boolean+xml;charset=utf8;version=1.0");
98 assertNotNull("Content Minor Version is greater than Accept Minor Version",ct.prep(trans,expected));
100 expected = "application/java.lang.Squid+xml;charset=utf8;version=1.0,application/xml;q=.9";
101 assertNotNull("2nd one will have to do...",ct.prep(trans,expected));
103 expected = "application/java.lang.Boolean+xml;charset=UTF8;version=1.0";
104 assertNotNull("Minor Charset in Caps acceptable",ct.prep(trans,expected));
107 expected="application/java.lang.Boolean+xml;charset=MyType;version=1.0";
108 assertNull("Unknown Minor Charset",ct.prep(trans,expected));
111 assertNotNull("Blank Acceptance",ct.prep(trans,expected));
114 assertNotNull("Null Acceptance",ct.prep(trans,expected));
116 expected = ("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
117 assertNotNull("Matches application/xml, and other content not known",ct.prep(trans,expected));
120 expected = ("i/am/bogus,application/xml");
121 assertNotNull("Match second entry, with no Semis",ct.prep(trans,expected));
124 StringBuilder sb = new StringBuilder();
125 trans.auditTrail(0, sb);
126 //System.out.println(sb);
130 // Original API used HTTPServletRequest and HTTPServletResponse. Due to the fact that sometimes we use Accept, and others Content-TYpe
131 // I changed it to simply accept a string
135 // @SuppressWarnings("rawtypes")
136 // class BogusReq implements HttpServletRequest {
137 // private String accept;
139 // public void accept(String accept) {
140 // this.accept = accept;
144 // public Object getAttribute(String name) {
150 // public Enumeration getAttributeNames() {
155 // public String getCharacterEncoding() {
160 // public void setCharacterEncoding(String env)
161 // throws UnsupportedEncodingException {
167 // public int getContentLength() {
173 // public String getContentType() {
179 // public ServletInputStream getInputStream() throws IOException {
185 // public String getParameter(String name) {
191 // public Enumeration getParameterNames() {
197 // public String[] getParameterValues(String name) {
203 // public Map getParameterMap() {
209 // public String getProtocol() {
215 // public String getScheme() {
221 // public String getServerName() {
227 // public int getServerPort() {
233 // public BufferedReader getReader() throws IOException {
239 // public String getRemoteAddr() {
245 // public String getRemoteHost() {
251 // public void setAttribute(String name, Object o) {
257 // public void removeAttribute(String name) {
263 // public Locale getLocale() {
269 // public Enumeration getLocales() {
275 // public boolean isSecure() {
281 // public RequestDispatcher getRequestDispatcher(String path) {
287 // public String getRealPath(String path) {
293 // public int getRemotePort() {
299 // public String getLocalName() {
305 // public String getLocalAddr() {
311 // public int getLocalPort() {
317 // public String getAuthType() {
323 // public Cookie[] getCookies() {
329 // public long getDateHeader(String name) {
335 // public String getHeader(String name) {
340 // public Enumeration getHeaders(String name) {
346 // public Enumeration getHeaderNames() {
352 // public int getIntHeader(String name) {
358 // public String getMethod() {
364 // public String getPathInfo() {
370 // public String getPathTranslated() {
376 // public String getContextPath() {
382 // public String getQueryString() {
388 // public String getRemoteUser() {
394 // public boolean isUserInRole(String role) {
400 // public Principal getUserPrincipal() {
406 // public String getRequestedSessionId() {
412 // public String getRequestURI() {
418 // public StringBuffer getRequestURL() {
424 // public String getServletPath() {
430 // public HttpSession getSession(boolean create) {
436 // public HttpSession getSession() {
442 // public boolean isRequestedSessionIdValid() {
448 // public boolean isRequestedSessionIdFromCookie() {
454 // public boolean isRequestedSessionIdFromURL() {
460 // public boolean isRequestedSessionIdFromUrl() {
466 // public class BogusResp implements HttpServletResponse {
467 // public String contentType;
470 // public String getCharacterEncoding() {
476 // public String getContentType() {
477 // return contentType;
481 // public ServletOutputStream getOutputStream() throws IOException {
487 // public PrintWriter getWriter() throws IOException {
493 // public void setCharacterEncoding(String charset) {
499 // public void setContentLength(int len) {
505 // public void setContentType(String type) {
506 // contentType = type;
510 // public void setBufferSize(int size) {
516 // public int getBufferSize() {
522 // public void flushBuffer() throws IOException {
528 // public void resetBuffer() {
534 // public boolean isCommitted() {
540 // public void reset() {
546 // public void setLocale(Locale loc) {
552 // public Locale getLocale() {
558 // public void addCookie(Cookie cookie) {
564 // public boolean containsHeader(String name) {
570 // public String encodeURL(String url) {
576 // public String encodeRedirectURL(String url) {
582 // public String encodeUrl(String url) {
588 // public String encodeRedirectUrl(String url) {
594 // public void sendError(int sc, String msg) throws IOException {
600 // public void sendError(int sc) throws IOException {
606 // public void sendRedirect(String location) throws IOException {
612 // public void setDateHeader(String name, long date) {
618 // public void addDateHeader(String name, long date) {
624 // public void setHeader(String name, String value) {
630 // public void addHeader(String name, String value) {
636 // public void setIntHeader(String name, int value) {
642 // public void addIntHeader(String name, int value) {
648 // public void setStatus(int sc) {
654 // public void setStatus(int sc, String sm) {