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