From 8c8aeeb892ec9f9da234bfe1ba736fb061b2b187 Mon Sep 17 00:00:00 2001 From: William Kim Date: Wed, 24 Jul 2013 17:13:37 -0400 Subject: [PATCH] hybrid server and client configuration services unit tests done. --- .../TestHybridClientConfigurationService.java | 114 ++++++++++++++++++ .../TestHybridServerConfigurationService.java | 105 ++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 openid-connect-client/src/test/java/org/mitre/openid/connect/client/service/impl/TestHybridClientConfigurationService.java create mode 100644 openid-connect-client/src/test/java/org/mitre/openid/connect/client/service/impl/TestHybridServerConfigurationService.java diff --git a/openid-connect-client/src/test/java/org/mitre/openid/connect/client/service/impl/TestHybridClientConfigurationService.java b/openid-connect-client/src/test/java/org/mitre/openid/connect/client/service/impl/TestHybridClientConfigurationService.java new file mode 100644 index 000000000..3beaac872 --- /dev/null +++ b/openid-connect-client/src/test/java/org/mitre/openid/connect/client/service/impl/TestHybridClientConfigurationService.java @@ -0,0 +1,114 @@ +/******************************************************************************* + * Copyright 2013 The MITRE Corporation + * and the MIT Kerberos and Internet Trust Consortium + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.mitre.openid.connect.client.service.impl; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mitre.oauth2.model.RegisteredClient; +import org.mitre.openid.connect.config.ServerConfiguration; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * @author wkim + * + */ +@RunWith(MockitoJUnitRunner.class) +public class TestHybridClientConfigurationService { + + @Mock + private StaticClientConfigurationService mockStaticService; + + @Mock + private DynamicRegistrationClientConfigurationService mockDynamicService; + + @InjectMocks + private HybridClientConfigurationService hybridService; + + // test fixture + + @Mock + private RegisteredClient mockClient; + + @Mock + private ServerConfiguration mockServerConfig; + + private String issuer = "https://www.example.com/"; + + @Before + public void prepare() { + + Mockito.reset(mockDynamicService, mockStaticService); + + Mockito.when(mockServerConfig.getIssuer()).thenReturn(issuer); + + } + + @Test + public void getClientConfiguration_useStatic() { + + Mockito.when(mockStaticService.getClientConfiguration(mockServerConfig)).thenReturn(mockClient); + + RegisteredClient result = hybridService.getClientConfiguration(mockServerConfig); + + Mockito.verify(mockStaticService).getClientConfiguration(mockServerConfig); + Mockito.verify(mockDynamicService, Mockito.never()).getClientConfiguration(Mockito.any(ServerConfiguration.class)); + assertEquals(mockClient, result); + } + + @Test + public void getClientConfiguration_useDynamic() { + + Mockito.when(mockStaticService.getClientConfiguration(mockServerConfig)).thenReturn(null); + Mockito.when(mockDynamicService.getClientConfiguration(mockServerConfig)).thenReturn(mockClient); + + RegisteredClient result = hybridService.getClientConfiguration(mockServerConfig); + + Mockito.verify(mockStaticService).getClientConfiguration(mockServerConfig); + Mockito.verify(mockDynamicService).getClientConfiguration(mockServerConfig); + assertEquals(mockClient, result); + } + + /** + * Checks the behavior when the issuer is not known. + */ + @Test + public void getClientConfiguration_noIssuer() { + + // The mockServerConfig is known to both services + Mockito.when(mockStaticService.getClientConfiguration(mockServerConfig)).thenReturn(mockClient); + Mockito.when(mockDynamicService.getClientConfiguration(mockServerConfig)).thenReturn(mockClient); + + // But oh noes! We're going to ask it to find us some other issuer + ServerConfiguration badIssuer = Mockito.mock(ServerConfiguration.class); + Mockito.when(badIssuer.getIssuer()).thenReturn("www.badexample.com"); + + RegisteredClient result = hybridService.getClientConfiguration(badIssuer); + + Mockito.verify(mockStaticService).getClientConfiguration(badIssuer); + Mockito.verify(mockDynamicService).getClientConfiguration(badIssuer); + assertThat(result, is(nullValue())); + } +} diff --git a/openid-connect-client/src/test/java/org/mitre/openid/connect/client/service/impl/TestHybridServerConfigurationService.java b/openid-connect-client/src/test/java/org/mitre/openid/connect/client/service/impl/TestHybridServerConfigurationService.java new file mode 100644 index 000000000..ceda6e1c2 --- /dev/null +++ b/openid-connect-client/src/test/java/org/mitre/openid/connect/client/service/impl/TestHybridServerConfigurationService.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * Copyright 2013 The MITRE Corporation + * and the MIT Kerberos and Internet Trust Consortium + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.mitre.openid.connect.client.service.impl; + + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mitre.openid.connect.config.ServerConfiguration; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * @author wkim + * + */ +@RunWith(MockitoJUnitRunner.class) +public class TestHybridServerConfigurationService { + + @Mock + private StaticServerConfigurationService mockStaticService; + + @Mock + private DynamicServerConfigurationService mockDynamicService; + + @InjectMocks + private HybridServerConfigurationService hybridService; + + @Mock + private ServerConfiguration mockServerConfig; + + private String issuer = "https://www.example.com/"; + + @Before + public void prepare() { + + Mockito.reset(mockDynamicService, mockStaticService); + + } + + + @Test + public void getServerConfiguration_useStatic() { + + Mockito.when(mockStaticService.getServerConfiguration(issuer)).thenReturn(mockServerConfig); + + ServerConfiguration result = hybridService.getServerConfiguration(issuer); + + Mockito.verify(mockStaticService).getServerConfiguration(issuer); + Mockito.verify(mockDynamicService, Mockito.never()).getServerConfiguration(Mockito.anyString()); + assertEquals(mockServerConfig, result); + } + + @Test + public void getServerConfiguration_useDynamic() { + + Mockito.when(mockStaticService.getServerConfiguration(issuer)).thenReturn(null); + Mockito.when(mockDynamicService.getServerConfiguration(issuer)).thenReturn(mockServerConfig); + + ServerConfiguration result = hybridService.getServerConfiguration(issuer); + + Mockito.verify(mockStaticService).getServerConfiguration(issuer); + Mockito.verify(mockDynamicService).getServerConfiguration(issuer); + assertEquals(mockServerConfig, result); + } + + /** + * Checks the behavior when the issuer is not known. + */ + @Test + public void getServerConfiguration_noIssuer() { + + Mockito.when(mockStaticService.getServerConfiguration(issuer)).thenReturn(mockServerConfig); + Mockito.when(mockDynamicService.getServerConfiguration(issuer)).thenReturn(mockServerConfig); + + String badIssuer = "www.badexample.com"; + + ServerConfiguration result = hybridService.getServerConfiguration(badIssuer); + + Mockito.verify(mockStaticService).getServerConfiguration(badIssuer); + Mockito.verify(mockDynamicService).getServerConfiguration(badIssuer); + assertThat(result, is(nullValue())); + } +}