]> git.basschouten.com Git - openhab-addons.git/blob
b396bcc1295f02bcf2e6fffc5ecbbb1edc44bf12
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.networkupstools.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.Optional;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.junit.jupiter.api.Test;
22
23 /**
24  * Test class to check the validity of the {@link NutName} enum.
25  *
26  * @author Hilbrand Bouwkamp - Initial contribution
27  */
28 public class NutNameTest {
29
30     private static final Pattern CHANNEL_PATTERN = Pattern.compile("(\\w+)\\.(\\w+)\\.?(\\w+)?\\.?(\\w+)?");
31
32     /**
33      * Tests if the name in {@link NutName} enum matches with the channelID in the enum.
34      */
35     @Test
36     public void testChannelIdName() {
37         for (final NutName nn : NutName.values()) {
38             final Matcher matcher = CHANNEL_PATTERN.matcher(nn.getName());
39
40             assertTrue(matcher.find(), "NutName name '" + nn + "' could not be matched with expected pattern.");
41             final String expectedChannelId = matcher.group(1)
42                     + capitalize(Optional.ofNullable(matcher.group(2)).orElse(""))
43                     + capitalize(Optional.ofNullable(matcher.group(3)).orElse(""))
44                     + capitalize(Optional.ofNullable(matcher.group(4)).orElse(""));
45             assertEquals(expectedChannelId, nn.getChannelId(), "Channel name not correct");
46         }
47     }
48
49     private String capitalize(String s) {
50         return s.isEmpty() ? "" : Character.toUpperCase(s.charAt(0)) + s.substring(1);
51     }
52 }