Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / rserv / test / JU_Content1.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
10  * * 
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * * 
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====================================================
19  * *
20  * *
21  ******************************************************************************/
22 package org.onap.aaf.auth.rserv.test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27
28 import java.io.IOException;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.junit.Test;
34 import org.onap.aaf.auth.rserv.HttpCode;
35 import org.onap.aaf.auth.rserv.TypedCode;
36 import org.onap.aaf.misc.env.TransJAXB;
37 import org.onap.aaf.misc.env.impl.EnvFactory;
38
39
40 /**
41  * Test the functioning of the "Content" class, which holds, and routes to the right code based on Accept values
42  */
43 public class JU_Content1 {
44     
45
46     @Test
47     public void test() throws Exception {
48         final String BOOL = "Boolean";
49         final String XML = "XML";
50         TransJAXB trans = EnvFactory.newTrans();
51         try {
52         HttpCode<TransJAXB, String> cBool = new HttpCode<TransJAXB,String>(BOOL,"Standard String") {
53             @Override
54             public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {
55                 try {
56                     resp.getOutputStream().write(context.getBytes());
57                 } catch (IOException e) {
58                 }
59             }
60         };
61
62         HttpCode<TransJAXB,String> cXML = new HttpCode<TransJAXB,String>(XML, "Standard String") {
63             @Override
64             public void handle(TransJAXB trans, HttpServletRequest req, HttpServletResponse resp) {
65                 try {
66                     resp.getOutputStream().write(context.getBytes());
67                 } catch (IOException e) {
68                 }
69             }
70         };
71
72         TypedCode<TransJAXB> ct = new TypedCode<TransJAXB>()
73                 .add(cBool,"application/" + Boolean.class.getName()+"+xml;charset=utf8;version=1.1")
74                 .add(cXML,"application/xml;q=.9");
75         String expected = "application/java.lang.Boolean+xml;charset=utf8;version=1.1,application/xml;q=0.9";
76         assertEquals(expected,ct.toString());
77
78         //BogusReq req = new BogusReq();
79         //expected = (expected);
80         //HttpServletResponse resp = new BogusResp();
81         
82         assertNotNull("Same Content String and Accept String",ct.prep(trans,expected));
83
84         //expects Null (not run)
85         // A Boolean xml that must have charset utf8 and match version 1.2 or greater
86         expected = ("application/java.lang.Boolean+xml;charset=utf8;version=1.2");
87         assertNull("Accept Minor Version greater than Content Minor Version",ct.prep(trans,expected));
88
89         // Same with (too many) spaces
90         expected = (" application/java.lang.Boolean+xml ; charset = utf8 ; version = 1.2   ");
91         assertNull("Accept Minor Version greater than Content Minor Version",ct.prep(trans,expected));
92
93         //expects Null (not run)
94         expected = ("application/java.lang.Boolean+xml;charset=utf8;version=2.1");
95         assertNull("Major Versions not the same",ct.prep(trans,expected));
96
97         expected = ("application/java.lang.Boolean+xml;charset=utf8;version=1.0");
98         assertNotNull("Content Minor Version is greater than Accept Minor Version",ct.prep(trans,expected));
99
100         expected = "application/java.lang.Squid+xml;charset=utf8;version=1.0,application/xml;q=.9";
101         assertNotNull("2nd one will have to do...",ct.prep(trans,expected));
102
103         expected = "application/java.lang.Boolean+xml;charset=UTF8;version=1.0";
104         assertNotNull("Minor Charset in Caps acceptable",ct.prep(trans,expected));
105
106         // expects no run 
107         expected="application/java.lang.Boolean+xml;charset=MyType;version=1.0";
108         assertNull("Unknown Minor Charset",ct.prep(trans,expected));
109
110         expected="";
111         assertNotNull("Blank Acceptance",ct.prep(trans,expected));
112         
113         expected=null;
114         assertNotNull("Null Acceptance",ct.prep(trans,expected));    
115
116         expected = ("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
117         assertNotNull("Matches application/xml, and other content not known",ct.prep(trans,expected));
118         
119         // No SemiColon
120         expected = ("i/am/bogus,application/xml");
121         assertNotNull("Match second entry, with no Semis",ct.prep(trans,expected));
122
123          } finally {    
124             StringBuilder sb = new StringBuilder();
125             trans.auditTrail(0, sb);
126             //System.out.println(sb);
127         }
128     }
129
130 }