Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tr064] fix phonebook lookup #79

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ public String toString() {
return "Phonebook{" + "phonebookName='" + phonebookName + "', phonebook=" + phonebook + '}';
}

private String normalizeNumber(String number) {
// Naive normalization: remove all non-digit characters
return number.replaceAll("[^0-9]\\+\\*", "");
/**
* normalize a phone number (remove everything except digits and *) for comparison
*
* @param number the input phone number string
* @return normalized phone number string
*/
public final String normalizeNumber(String number) {
return number.replaceAll("[^0-9\\*\\+]", "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2021 Contributors to the SmartHome/J project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.smarthomej.binding.tr064.internal.phonebook;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

/**
* The {@link Tr064PhonebookImplTest} class implements test cases for the {@link Tr064PhonebookImpl} class
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
@MockitoSettings(strictness = Strictness.WARN)
@ExtendWith(MockitoExtension.class)
public class Tr064PhonebookImplTest {
@Mock
private @NonNullByDefault({}) HttpClient httpClient;

// key -> input, value -> output
public static Collection<Map.Entry<String, String>> phoneNumbers() {
return List.of( //
Map.entry("**820", "**820"), //
Map.entry("49200123456", "49200123456"), //
Map.entry("+49-200-123456", "+49200123456"), //
Map.entry("49 (200) 123456", "49200123456"), //
Map.entry("+49 200/123456", "+49200123456"));
}

@ParameterizedTest
@MethodSource("phoneNumbers")
public void testNormalization(Map.Entry<String, String> input) {
when(httpClient.newRequest((String) any())).thenThrow(new IllegalArgumentException("testing"));
Tr064PhonebookImpl testPhonebook = new Tr064PhonebookImpl(httpClient, "", 0);
Assertions.assertEquals(input.getValue(), testPhonebook.normalizeNumber(input.getKey()));
}
}