1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cssa.rserv.test;
\r
26 import static org.junit.Assert.assertEquals;
\r
27 import static org.junit.Assert.assertNotNull;
\r
28 import static org.junit.Assert.assertNull;
\r
30 import java.io.IOException;
\r
32 import javax.servlet.http.HttpServletRequest;
\r
33 import javax.servlet.http.HttpServletResponse;
\r
35 import org.junit.Test;
\r
37 import com.att.cssa.rserv.HttpCode;
\r
38 import com.att.cssa.rserv.TypedCode;
\r
39 import com.att.inno.env.TransJAXB;
\r
40 import com.att.inno.env.impl.EnvFactory;
\r
44 * Test the functioning of the "Content" class, which holds, and routes to the right code based on Accept values
\r
46 public class JU_Content {
\r
50 public void test() throws Exception {
\r
51 final String BOOL = "Boolean";
\r
52 final String XML = "XML";
\r
53 TransJAXB trans = EnvFactory.newTrans();
\r
55 HttpCode<TransJAXB, String> cBool = new HttpCode<TransJAXB,String>(BOOL,"Standard String") {
\r
57 public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {
\r
59 resp.getOutputStream().write(context.getBytes());
\r
60 } catch (IOException e) {
\r
65 HttpCode<TransJAXB,String> cXML = new HttpCode<TransJAXB,String>(XML, "Standard String") {
\r
67 public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {
\r
69 resp.getOutputStream().write(context.getBytes());
\r
70 } catch (IOException e) {
\r
75 TypedCode<TransJAXB> ct = new TypedCode<TransJAXB>()
\r
76 .add(cBool,"application/" + Boolean.class.getName()+"+xml;charset=utf8;version=1.1")
\r
77 .add(cXML,"application/xml;q=.9");
\r
78 String expected = "application/java.lang.Boolean+xml;charset=utf8;version=1.1,application/xml;q=0.9";
\r
79 assertEquals(expected,ct.toString());
\r
81 //BogusReq req = new BogusReq();
\r
82 //expected = (expected);
\r
83 //HttpServletResponse resp = new BogusResp();
\r
85 assertNotNull("Same Content String and Accept String",ct.prep(trans,expected));
\r
87 //expects Null (not run)
\r
88 // A Boolean xml that must have charset utf8 and match version 1.2 or greater
\r
89 expected = ("application/java.lang.Boolean+xml;charset=utf8;version=1.2");
\r
90 assertNull("Accept Minor Version greater than Content Minor Version",ct.prep(trans,expected));
\r
92 // Same with (too many) spaces
\r
93 expected = (" application/java.lang.Boolean+xml ; charset = utf8 ; version = 1.2 ");
\r
94 assertNull("Accept Minor Version greater than Content Minor Version",ct.prep(trans,expected));
\r
96 //expects Null (not run)
\r
97 expected = ("application/java.lang.Boolean+xml;charset=utf8;version=2.1");
\r
98 assertNull("Major Versions not the same",ct.prep(trans,expected));
\r
100 expected = ("application/java.lang.Boolean+xml;charset=utf8;version=1.0");
\r
101 assertNotNull("Content Minor Version is greater than Accept Minor Version",ct.prep(trans,expected));
\r
103 expected = "application/java.lang.Squid+xml;charset=utf8;version=1.0,application/xml;q=.9";
\r
104 assertNotNull("2nd one will have to do...",ct.prep(trans,expected));
\r
106 expected = "application/java.lang.Boolean+xml;charset=UTF8;version=1.0";
\r
107 assertNotNull("Minor Charset in Caps acceptable",ct.prep(trans,expected));
\r
110 expected="application/java.lang.Boolean+xml;charset=MyType;version=1.0";
\r
111 assertNull("Unknown Minor Charset",ct.prep(trans,expected));
\r
114 assertNotNull("Blank Acceptance",ct.prep(trans,expected));
\r
117 assertNotNull("Null Acceptance",ct.prep(trans,expected));
\r
119 expected = ("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
\r
120 assertNotNull("Matches application/xml, and other content not known",ct.prep(trans,expected));
\r
123 expected = ("i/am/bogus,application/xml");
\r
124 assertNotNull("Match second entry, with no Semis",ct.prep(trans,expected));
\r
127 StringBuilder sb = new StringBuilder();
\r
128 trans.auditTrail(0, sb);
\r
129 System.out.println(sb);
\r