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.wa.starter.pac4j.saml;
20  
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.mockito.ArgumentMatchers.any;
23  import static org.mockito.ArgumentMatchers.anyString;
24  import static org.mockito.Mockito.doNothing;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  import java.io.File;
29  import java.util.Base64;
30  import org.apache.commons.io.IOUtils;
31  import org.apache.syncope.common.lib.to.SAML2SPEntityTO;
32  import org.apache.syncope.common.rest.api.service.SAML2SPEntityService;
33  import org.apache.syncope.wa.bootstrap.WARestClient;
34  import org.junit.jupiter.api.Test;
35  import org.pac4j.saml.client.SAML2Client;
36  import org.springframework.core.io.ClassPathResource;
37  
38  public class WASAML2MetadataResolverTest extends BaseWASAML2ClientTest {
39  
40      @Test
41      public void fetchMetadata() throws Exception {
42          SAML2Client client = getSAML2Client();
43          String keystoreFile = File.createTempFile("keystore", "jks").getCanonicalPath();
44          client.getConfiguration().setKeystoreResourceFilepath(keystoreFile);
45  
46          SAML2SPEntityTO metadataTO = new SAML2SPEntityTO.Builder()
47                  .key("Syncope")
48                  .metadata(Base64.getEncoder().encodeToString(
49                          IOUtils.toByteArray(new ClassPathResource("sp-metadata.xml").getInputStream())))
50                  .build();
51  
52          SAML2SPEntityService saml2SPMetadataService = mock(SAML2SPEntityService.class);
53          when(saml2SPMetadataService.get(anyString())).thenReturn(metadataTO);
54          doNothing().when(saml2SPMetadataService).set(any(SAML2SPEntityTO.class));
55  
56          WARestClient waRestClient = mock(WARestClient.class);
57          when(waRestClient.getService(SAML2SPEntityService.class)).thenReturn(saml2SPMetadataService);
58  
59          WASAML2MetadataResolver resolver = new WASAML2MetadataResolver(waRestClient, client);
60          assertNotNull(resolver.fetchMetadata());
61      }
62  }