2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.networkupstools.internal;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.util.Optional;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
21 import org.junit.jupiter.api.Test;
24 * Test class to check the validity of the {@link NutName} enum.
26 * @author Hilbrand Bouwkamp - Initial contribution
28 public class NutNameTest {
30 private static final Pattern CHANNEL_PATTERN = Pattern.compile("(\\w+)\\.(\\w+)\\.?(\\w+)?\\.?(\\w+)?");
33 * Tests if the name in {@link NutName} enum matches with the channelID in the enum.
36 public void testChannelIdName() {
37 for (final NutName nn : NutName.values()) {
38 final Matcher matcher = CHANNEL_PATTERN.matcher(nn.getName());
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");
49 private String capitalize(String s) {
50 return s.isEmpty() ? "" : Character.toUpperCase(s.charAt(0)) + s.substring(1);