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 ******************************************************************************/
23 package org.onap.aaf.auth.rserv.test;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
29 import java.io.IOException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.junit.Test;
35 import org.onap.aaf.auth.rserv.HttpCode;
36 import org.onap.aaf.auth.rserv.TypedCode;
37 import org.onap.aaf.misc.env.TransJAXB;
38 import org.onap.aaf.misc.env.impl.EnvFactory;
42 * Test the functioning of the "Content" class, which holds, and routes to the right code based on Accept values
44 public class JU_Content1 {
48 public void test() throws Exception {
49 final String BOOL = "Boolean";
50 final String XML = "XML";
51 TransJAXB trans = EnvFactory.newTrans();
53 HttpCode<TransJAXB, String> cBool = new HttpCode<TransJAXB,String>(BOOL,"Standard String") {
55 public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {
57 resp.getOutputStream().write(context.getBytes());
58 } catch (IOException e) {
63 HttpCode<TransJAXB,String> cXML = new HttpCode<TransJAXB,String>(XML, "Standard String") {
65 public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {
67 resp.getOutputStream().write(context.getBytes());
68 } catch (IOException e) {
73 TypedCode<TransJAXB> ct = new TypedCode<TransJAXB>()
74 .add(cBool,"application/" + Boolean.class.getName()+"+xml;charset=utf8;version=1.1")
75 .add(cXML,"application/xml;q=.9");
76 String expected = "application/java.lang.Boolean+xml;charset=utf8;version=1.1,application/xml;q=0.9";
77 assertEquals(expected,ct.toString());
79 //BogusReq req = new BogusReq();
80 //expected = (expected);
81 //HttpServletResponse resp = new BogusResp();
83 assertNotNull("Same Content String and Accept String",ct.prep(trans,expected));
85 //expects Null (not run)
86 // A Boolean xml that must have charset utf8 and match version 1.2 or greater
87 expected = ("application/java.lang.Boolean+xml;charset=utf8;version=1.2");
88 assertNull("Accept Minor Version greater than Content Minor Version",ct.prep(trans,expected));
90 // Same with (too many) spaces
91 expected = (" application/java.lang.Boolean+xml ; charset = utf8 ; version = 1.2 ");
92 assertNull("Accept Minor Version greater than Content Minor Version",ct.prep(trans,expected));
94 //expects Null (not run)
95 expected = ("application/java.lang.Boolean+xml;charset=utf8;version=2.1");
96 assertNull("Major Versions not the same",ct.prep(trans,expected));
98 expected = ("application/java.lang.Boolean+xml;charset=utf8;version=1.0");
99 assertNotNull("Content Minor Version is greater than Accept Minor Version",ct.prep(trans,expected));
101 expected = "application/java.lang.Squid+xml;charset=utf8;version=1.0,application/xml;q=.9";
102 assertNotNull("2nd one will have to do...",ct.prep(trans,expected));
104 expected = "application/java.lang.Boolean+xml;charset=UTF8;version=1.0";
105 assertNotNull("Minor Charset in Caps acceptable",ct.prep(trans,expected));
108 expected="application/java.lang.Boolean+xml;charset=MyType;version=1.0";
109 assertNull("Unknown Minor Charset",ct.prep(trans,expected));
112 assertNotNull("Blank Acceptance",ct.prep(trans,expected));
115 assertNotNull("Null Acceptance",ct.prep(trans,expected));
117 expected = ("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
118 assertNotNull("Matches application/xml, and other content not known",ct.prep(trans,expected));
121 expected = ("i/am/bogus,application/xml");
122 assertNotNull("Match second entry, with no Semis",ct.prep(trans,expected));
125 StringBuilder sb = new StringBuilder();
126 trans.auditTrail(0, sb);
127 //System.out.println(sb);