[AAF-21] Initial code import
[aaf/authz.git] / authz-core / src / test / java / com / att / cssa / rserv / test / JU_Content.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\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
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \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
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cssa.rserv.test;\r
25 \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
29 \r
30 import java.io.IOException;\r
31 \r
32 import javax.servlet.http.HttpServletRequest;\r
33 import javax.servlet.http.HttpServletResponse;\r
34 \r
35 import org.junit.Test;\r
36 \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
41 \r
42 \r
43 /**\r
44  * Test the functioning of the "Content" class, which holds, and routes to the right code based on Accept values\r
45  */\r
46 public class JU_Content {\r
47         \r
48 \r
49         @Test\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
54                 try {\r
55                 HttpCode<TransJAXB, String> cBool = new HttpCode<TransJAXB,String>(BOOL,"Standard String") {\r
56                         @Override\r
57                         public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {\r
58                                 try {\r
59                                         resp.getOutputStream().write(context.getBytes());\r
60                                 } catch (IOException e) {\r
61                                 }\r
62                         }\r
63                 };\r
64 \r
65                 HttpCode<TransJAXB,String> cXML = new HttpCode<TransJAXB,String>(XML, "Standard String") {\r
66                         @Override\r
67                         public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {\r
68                                 try {\r
69                                         resp.getOutputStream().write(context.getBytes());\r
70                                 } catch (IOException e) {\r
71                                 }\r
72                         }\r
73                 };\r
74 \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
80 \r
81                 //BogusReq req = new BogusReq();\r
82                 //expected = (expected);\r
83                 //HttpServletResponse resp = new BogusResp();\r
84                 \r
85                 assertNotNull("Same Content String and Accept String",ct.prep(trans,expected));\r
86 \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
91 \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
95 \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
99 \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
102 \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
105 \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
108 \r
109                 // expects no run \r
110                 expected="application/java.lang.Boolean+xml;charset=MyType;version=1.0";\r
111                 assertNull("Unknown Minor Charset",ct.prep(trans,expected));\r
112 \r
113                 expected="";\r
114                 assertNotNull("Blank Acceptance",ct.prep(trans,expected));\r
115                 \r
116                 expected=null;\r
117                 assertNotNull("Null Acceptance",ct.prep(trans,expected));       \r
118 \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
121                 \r
122                 // No SemiColon\r
123                 expected = ("i/am/bogus,application/xml");\r
124                 assertNotNull("Match second entry, with no Semis",ct.prep(trans,expected));\r
125 \r
126                 } finally {     \r
127                         StringBuilder sb = new StringBuilder();\r
128                         trans.auditTrail(0, sb);\r
129                         System.out.println(sb);\r
130                 }\r
131         }\r
132 \r
133 }\r