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