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.core.persistence.jpa.inner;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.List;
28  import org.apache.syncope.core.persistence.api.dao.WAConfigDAO;
29  import org.apache.syncope.core.persistence.api.entity.am.WAConfigEntry;
30  import org.apache.syncope.core.persistence.jpa.AbstractTest;
31  import org.apache.syncope.core.persistence.jpa.entity.am.JPAWAConfigEntry;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.springframework.beans.factory.annotation.Autowired;
35  import org.springframework.transaction.annotation.Transactional;
36  
37  @Transactional("Master")
38  public class WAConfigTest extends AbstractTest {
39  
40      @Autowired
41      private WAConfigDAO configDAO;
42  
43      @BeforeEach
44      public void beforeEach() {
45          entityManager().createQuery("DELETE FROM " + JPAWAConfigEntry.class.getSimpleName()).executeUpdate();
46      }
47  
48      @Test
49      public void saveCommaSeparatedValueStrings() {
50          create("system.example.key[0]", Arrays.asList("value1", "value2", "value3"));
51          assertFalse(configDAO.findAll().isEmpty());
52      }
53  
54      @Test
55      public void saveNumbers() {
56          create("system.example.key[0]", List.of("1984"));
57          assertFalse(configDAO.findAll().isEmpty());
58      }
59  
60      @Test
61      public void saveCollection() {
62          WAConfigEntry entry = create("system.example.key[0]", new ArrayList<>(Arrays.asList("1", "2")));
63          assertNotNull(entry.getValues());
64          assertFalse(configDAO.findAll().isEmpty());
65      }
66  
67      @Test
68      public void saveList() {
69          create("system.example.key[0].key1", List.of("value1"));
70          assertFalse(configDAO.findAll().isEmpty());
71      }
72  
73      @Test
74      public void update() {
75          WAConfigEntry entry = create("system.syncope.key[0]", Arrays.asList("1", "2", "3", "4"));
76          assertNotNull(entry);
77          entry.setValues(List.of("v1"));
78  
79          entry = configDAO.save(entry);
80          assertNotNull(entry);
81          assertNotNull(entry.getKey());
82          WAConfigEntry found = configDAO.find(entry.getKey());
83          assertNotNull(found);
84          assertEquals(List.of("v1"), found.getValues());
85      }
86  
87      private WAConfigEntry create(final String name, final List<String> value) {
88          WAConfigEntry entry = entityFactory.newEntity(WAConfigEntry.class);
89          entry.setKey(name);
90          entry.setValues(value);
91          configDAO.save(entry);
92          assertNotNull(entry);
93          assertNotNull(entry.getKey());
94          assertNotNull(configDAO.find(entry.getKey()));
95          return entry;
96      }
97  }