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