1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 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 ******************************************************************************/
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_Content1 {
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);