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.SAML2IdPEntityTO;
30  import org.apache.syncope.common.rest.api.service.SAML2IdPEntityService;
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 SAML2IdPEntityITCase extends AbstractITCase {
36  
37      private static SAML2IdPEntityService WA_SAML2IDP_ENTITY_SERVICE;
38  
39      @BeforeAll
40      public static void setup() {
41          assumeTrue(CLIENT_FACTORY.getContentType() == SyncopeClientFactoryBean.ContentType.JSON);
42  
43          WA_SAML2IDP_ENTITY_SERVICE = ANONYMOUS_CLIENT.getService(SAML2IdPEntityService.class);
44      }
45  
46      private static SAML2IdPEntityTO set() {
47          SAML2IdPEntityTO entityTO = new SAML2IdPEntityTO.Builder().
48                  key(SAML2IdPEntityService.DEFAULT_OWNER).
49                  metadata(Base64.getEncoder().encodeToString(
50                          "testMetadata".getBytes(StandardCharsets.UTF_8))).
51                  encryptionCertificate(Base64.getEncoder().encodeToString(
52                          "testEncryptionCert".getBytes(StandardCharsets.UTF_8))).
53                  encryptionKey(Base64.getEncoder().encodeToString(
54                          "testEncryptionKey".getBytes(StandardCharsets.UTF_8))).
55                  signingCertificate(Base64.getEncoder().encodeToString(
56                          "testSigningCert".getBytes(StandardCharsets.UTF_8))).
57                  signingKey(Base64.getEncoder().encodeToString(
58                          "testSigningKey".getBytes(StandardCharsets.UTF_8))).
59                  build();
60          WA_SAML2IDP_ENTITY_SERVICE.set(entityTO);
61  
62          return entityTO;
63      }
64  
65      @Test
66      public void get() {
67          SAML2IdPEntityTO entityTO;
68          try {
69              entityTO = WA_SAML2IDP_ENTITY_SERVICE.get(SAML2IdPEntityService.DEFAULT_OWNER);
70          } catch (SyncopeClientException e) {
71              entityTO = set();
72          }
73          assertNotNull(entityTO);
74  
75          assertEquals(SAML2IdPEntityService.DEFAULT_OWNER, entityTO.getKey());
76      }
77  
78      @Test
79      public void getAndSet() {
80          SAML2IdPEntityTO entityTO;
81          try {
82              entityTO = WA_SAML2IDP_ENTITY_SERVICE.get(SAML2IdPEntityService.DEFAULT_OWNER);
83          } catch (SyncopeClientException e) {
84              entityTO = set();
85          }
86          assertNotNull(entityTO);
87  
88          entityTO.setMetadata(Base64.getEncoder().encodeToString("new metadata".getBytes(StandardCharsets.UTF_8)));
89          WA_SAML2IDP_ENTITY_SERVICE.set(entityTO);
90  
91          entityTO = WA_SAML2IDP_ENTITY_SERVICE.get(entityTO.getKey());
92          assertEquals(
93                  "new metadata",
94                  new String(Base64.getDecoder().decode(entityTO.getMetadata()), StandardCharsets.UTF_8));
95      }
96  }