View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.core;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assumptions.assumeTrue;
24  
25  import java.nio.charset.StandardCharsets;
26  import java.util.Base64;
27  import org.apache.syncope.client.lib.SyncopeClientFactoryBean;
28  import org.apache.syncope.common.lib.SyncopeClientException;
29  import org.apache.syncope.common.lib.to.SAML2SPEntityTO;
30  import org.apache.syncope.common.rest.api.service.SAML2SPEntityService;
31  import org.apache.syncope.fit.AbstractITCase;
32  import org.junit.jupiter.api.BeforeAll;
33  import org.junit.jupiter.api.Test;
34  
35  public class SAML2SPEntityITCase extends AbstractITCase {
36  
37      private static final String OWNER = "owner";
38  
39      private static SAML2SPEntityService WA_SAML2SP_ENTITY_SERVICE;
40  
41      @BeforeAll
42      public static void setup() {
43          assumeTrue(CLIENT_FACTORY.getContentType() == SyncopeClientFactoryBean.ContentType.JSON);
44  
45          WA_SAML2SP_ENTITY_SERVICE = ANONYMOUS_CLIENT.getService(SAML2SPEntityService.class);
46      }
47  
48      private static SAML2SPEntityTO set() {
49          SAML2SPEntityTO entityTO = new SAML2SPEntityTO.Builder().
50                  key(OWNER).
51                  metadata(Base64.getEncoder().encodeToString("testMetadata".getBytes(StandardCharsets.UTF_8))).
52                  build();
53          WA_SAML2SP_ENTITY_SERVICE.set(entityTO);
54  
55          return entityTO;
56      }
57  
58      @Test
59      public void get() {
60          SAML2SPEntityTO entityTO;
61          try {
62              entityTO = WA_SAML2SP_ENTITY_SERVICE.get(OWNER);
63          } catch (SyncopeClientException e) {
64              entityTO = set();
65          }
66          assertNotNull(entityTO);
67  
68          assertEquals(OWNER, entityTO.getKey());
69      }
70  
71      @Test
72      public void getAndSet() {
73          SAML2SPEntityTO entityTO;
74          try {
75              entityTO = WA_SAML2SP_ENTITY_SERVICE.get(OWNER);
76          } catch (SyncopeClientException e) {
77              entityTO = set();
78          }
79          assertNotNull(entityTO);
80  
81          entityTO.setMetadata(Base64.getEncoder().encodeToString("new metadata".getBytes(StandardCharsets.UTF_8)));
82          WA_SAML2SP_ENTITY_SERVICE.set(entityTO);
83  
84          entityTO = WA_SAML2SP_ENTITY_SERVICE.get(entityTO.getKey());
85          assertEquals(
86                  "new metadata",
87                  new String(Base64.getDecoder().decode(entityTO.getMetadata()), StandardCharsets.UTF_8));
88      }
89  }