f3e05d9bcc0f6e85950363ce57819cfb74cf04bf
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / config / test / JU_RegistrationPropHolder.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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 package org.onap.aaf.cadi.config.test;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.net.UnknownHostException;
26
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.PropAccess;
31 import org.onap.aaf.cadi.config.Config;
32 import org.onap.aaf.cadi.config.RegistrationPropHolder;
33
34 public class JU_RegistrationPropHolder {
35
36         @Test
37         public void testBlank() {
38                 PropAccess pa = new PropAccess();
39                 RegistrationPropHolder rph;
40                 int ju_port = 20;
41                 try {
42                         ////////////////
43                         // Check Required Properties
44                         ////////////////
45                         try {
46                                 rph = new RegistrationPropHolder(pa,20);
47                         } catch (CadiException e) {
48                                 Assert.assertEquals(
49                                                 "\ncadi_latitude must be defined." + 
50                                                 "\ncadi_longitude must be defined.",e.getMessage());
51                         }
52                         
53                         try {
54                                 pa.setProperty(Config.CADI_LATITUDE, "32.7");
55                                 rph = new RegistrationPropHolder(pa,20);
56                         } catch (CadiException e) {
57                                 Assert.assertEquals(
58                                                 "\ncadi_longitude must be defined.",e.getMessage());
59                         }
60                         
61                         pa.setProperty(Config.CADI_LONGITUDE, "-72.0");
62                         rph = new RegistrationPropHolder(pa,ju_port);
63                         
64                         ////////////////
65                         // Validate Default Properties
66                         ////////////////
67                         for(String dot_le : new String[] {"",".helm"}) {
68                                 assertEquals(rph.hostname,rph.default_fqdn);
69                                 assertEquals("",rph.lcontainer);
70                                 assertEquals(rph.hostname,rph.public_hostname);
71                                 assertEquals(ju_port,rph.getEntryPort(dot_le));
72                                 assertEquals(rph.hostname,rph.getEntryFQDN("",dot_le));
73                         }
74
75                         String ns = "myns";
76                         pa.setProperty(Config.AAF_LOCATOR_NS, ns);
77                         for(String dot_le : new String[] {"",".helm"}) {
78                                 assertEquals(rph.hostname,rph.default_fqdn);
79                                 assertEquals("",rph.lcontainer);
80                                 assertEquals(rph.hostname,rph.public_hostname);
81                                 assertEquals(ju_port,rph.getEntryPort(dot_le));
82                                 assertEquals(rph.hostname,rph.getEntryFQDN("",dot_le));
83                         }
84
85                         String ns2 = "onap";
86                         pa.setProperty(Config.AAF_LOCATOR_NS+".helm", ns2);
87                         for(String dot_le : new String[] {"",".helm"}) {
88                                 String pns;
89                                 if(dot_le.isEmpty()) {
90                                         pns = ns;
91                                 } else {
92                                         pns = ns2;
93                                 }
94                                 assertEquals(rph.hostname,rph.default_fqdn);
95                                 assertEquals("",rph.lcontainer);
96                                 assertEquals(rph.hostname,rph.public_hostname);
97                                 assertEquals(ju_port,rph.getEntryPort(dot_le));
98                                 assertEquals(rph.hostname,rph.getEntryFQDN("",dot_le));
99                         }
100
101                         ////////////////
102                         // Validate Public Host and Port settings
103                         ////////////////
104                         String public_hostname = "com.public.hostname";
105                         int public_port = 999;
106                         pa.setProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, public_hostname);
107                         pa.setProperty(Config.AAF_LOCATOR_PUBLIC_PORT,Integer.toString(public_port));
108                         RegistrationPropHolder pubRPH = new RegistrationPropHolder(pa,ju_port);
109                         assertEquals(public_hostname,pubRPH.public_hostname);
110                         assertEquals(public_port,pubRPH.getEntryPort(""));
111
112
113                         final String url = "https://aaf.osaaf.org:8095/org.osaaf.aaf.service:2.1";
114                         String name="theName";
115                         assertEquals(url,rph.replacements(url, name, ""));
116                         
117                         String alu = "aaf.osaaf.org:8095";
118                         String curl = url.replace(alu, Config.AAF_LOCATE_URL_TAG);
119                         pa.setProperty(Config.AAF_LOCATE_URL,"https://"+alu);
120                         assertEquals(url.replace("8095","8095/locate"),rph.replacements(curl, name, ""));
121                         
122                         String root_ns = "org.osaaf.aaf";
123                         curl = url.replace(root_ns, "AAF_NS");
124                         pa.setProperty(Config.AAF_ROOT_NS,root_ns);
125                         assertEquals(url,rph.replacements(curl, name, ""));
126                         
127                         curl = url.replace(root_ns, "%AAF_NS");
128                         pa.setProperty(Config.AAF_ROOT_NS,root_ns);
129                         assertEquals(url,rph.replacements(curl, name, ""));
130                         
131                         final String fqdn = "%C.%CNS.%NS.%N";
132                         String target = "myns.theName";
133                         assertEquals(target,rph.replacements(fqdn, name, ""));
134
135                         pa.setProperty(Config.AAF_LOCATOR_CONTAINER_NS+".hello", "mycontns");
136                         target = "mycontns.org.osaaf.aaf.theName";
137                         assertEquals(target,rph.replacements(fqdn, name, ".hello"));
138                         
139                         pa.setProperty(Config.AAF_LOCATOR_CONTAINER+".hello","hello");
140                         target = "hello.mycontns.org.osaaf.aaf.theName";
141                         assertEquals(target,rph.replacements(fqdn, name, ".hello"));
142                         
143                         pa.setProperty(Config.AAF_LOCATOR_CONTAINER_NS,"c_ns");
144                         target = "c_ns.myns.theName";
145                         assertEquals(target,rph.replacements(fqdn, name, ""));
146
147
148                 } catch (UnknownHostException | CadiException e) {
149                         e.printStackTrace();
150                         Assert.fail();
151                 }
152         }
153         
154
155 }