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

OKTA-289659: Add Event Hooks Integration Tests #399

Merged
Show file tree
Hide file tree
Changes from 13 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
@@ -0,0 +1,35 @@
/*
* Copyright 2020-Present Okta, Inc.
*
* 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 com.okta.sdk.resource.event.hook;

import com.okta.commons.lang.Classes;
import com.okta.sdk.client.Client;

public interface EventHookBuilder {
EventHookBuilder setName(String name);

EventHookBuilder setUrl(String url);

EventHookBuilder setAuthorizationHeaderValue(String authorizationHeaderValue);

EventHookBuilder addHeader(String name, String value);

EventHook buildAndCreate(Client client);

static EventHookBuilder instance() {
return Classes.newInstance("com.okta.sdk.impl.resource.event.hook.DefaultEventHookBuilder");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2020-Present Okta, Inc.
*
* 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 com.okta.sdk.impl.resource.event.hook;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.net.HttpHeaders;

import com.okta.sdk.client.Client;
import com.okta.sdk.resource.event.hook.EventHook;
import com.okta.sdk.resource.event.hook.EventHookBuilder;
import com.okta.sdk.resource.event.hook.EventHookChannel;
import com.okta.sdk.resource.event.hook.EventHookChannelConfig;
import com.okta.sdk.resource.event.hook.EventHookChannelConfigHeader;
import com.okta.sdk.resource.event.hook.EventHookChannelConfigAuthScheme;
import com.okta.sdk.resource.event.hook.EventHookChannelConfigAuthSchemeType;
import com.okta.sdk.resource.event.hook.EventSubscriptions;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* Builder for {@link EventHook}.
* @since 2.0.0
*/
public class DefaultEventHookBuilder implements EventHookBuilder {

private static final String LIFECYCLE_EVENT_CREATE = "user.lifecycle.create";
private static final String LIFECYCLE_EVENT_ACTIVATE = "user.lifecycle.activate";

private static final String VERSION = "1.0.0";

private String name;
private String url;
private String authorizationHeaderValue;
private Map<String, String> headerMap = Maps.newHashMap();

@Override
public EventHookBuilder setName(String name) {
this.name = name;
return this;
}

@Override
public EventHookBuilder setUrl(String url) {
this.url = url;
return this;
}

@Override
public EventHookBuilder setAuthorizationHeaderValue(String authorizationHeaderValue) {
this.authorizationHeaderValue = authorizationHeaderValue;
return this;
}

@Override
public EventHookBuilder addHeader(String name, String value) {
headerMap.put(name, value);
return this;
}

@Override
public EventHook buildAndCreate(Client client) {

EventSubscriptions eventSubscriptions = client.instantiate(EventSubscriptions.class)
.setType(EventSubscriptions.TypeEnum.EVENT_TYPE)
.setItems(Arrays.asList(LIFECYCLE_EVENT_CREATE, LIFECYCLE_EVENT_ACTIVATE));

EventHookChannelConfigAuthScheme eventHookChannelConfigAuthScheme =
client.instantiate(EventHookChannelConfigAuthScheme.class)
.setType(EventHookChannelConfigAuthSchemeType.HEADER)
.setKey(HttpHeaders.AUTHORIZATION)
.setValue(authorizationHeaderValue);

List<EventHookChannelConfigHeader> headers = Lists.newArrayList();

for (Map.Entry<String, String> entry : headerMap.entrySet()) {
headers.add(client.instantiate(EventHookChannelConfigHeader.class)
.setKey(entry.getKey())
.setValue(entry.getValue()));
}

EventHookChannelConfig eventHookChannelConfig = client.instantiate(EventHookChannelConfig.class)
.setUri(url)
.setHeaders(headers)
.setAuthScheme(eventHookChannelConfigAuthScheme);

EventHookChannel eventHookChannel = client.instantiate(EventHookChannel.class)
.setType(EventHookChannel.TypeEnum.HTTP)
.setVersion(VERSION)
.setConfig(eventHookChannelConfig);

EventHook createdEventHook = client.createEventHook(client.instantiate(EventHook.class)
.setName(name)
.setEvents(eventSubscriptions)
.setChannel(eventHookChannel));

return createdEventHook;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* Copyright 2020-Present Okta, Inc.
*
* 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 com.okta.sdk.tests.it

import com.okta.sdk.resource.ResourceException
import com.okta.sdk.resource.event.hook.EventHook
import com.okta.sdk.resource.event.hook.EventHookChannelConfigAuthScheme
import com.okta.sdk.resource.event.hook.EventHookChannelConfigAuthSchemeType
import com.okta.sdk.impl.resource.event.hook.DefaultEventHookBuilder

import com.okta.sdk.tests.it.util.ITSupport

import org.testng.annotations.Test

import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.Matchers.equalTo
import static org.hamcrest.Matchers.iterableWithSize
import static org.hamcrest.Matchers.notNullValue

import static com.okta.sdk.tests.it.util.Util.assertPresent

/**
* Tests for {@code /api/v1/eventHooks}.
* @since 2.0.0
*/
class EventHooksIT extends ITSupport {

@Test
void createEventHookTest() {
String name = "event-hook-java-sdk-${UUID.randomUUID().toString()}"

EventHook createdEventHook = DefaultEventHookBuilder.instance()
arvindkrishnakumar-okta marked this conversation as resolved.
Show resolved Hide resolved
.setName(name)
.setUrl("https://www.example.com/eventHooks")
.setAuthorizationHeaderValue("Test-Api-Key")
.addHeader("X-Test-Header", "Test header value")
.buildAndCreate(client);
registerForCleanup(createdEventHook)

assertThat(createdEventHook.getId(), notNullValue())
assertThat(createdEventHook.getName(), equalTo(name))
assertThat(createdEventHook.getStatus(), equalTo(EventHook.StatusEnum.ACTIVE))
assertThat(createdEventHook.getEvents().getItems(), iterableWithSize(2))
assertThat(createdEventHook.getChannel().getConfig().getUri(), equalTo("https://www.example.com/eventHooks"))

createdEventHook.deactivate()
}

@Test
void getEventHookTest() {
String name = "event-hook-java-sdk-${UUID.randomUUID().toString()}"

EventHook createdEventHook = DefaultEventHookBuilder.instance()
.setName(name)
.setUrl("https://www.example.com/eventHooks")
.setAuthorizationHeaderValue("Test-Api-Key")
.addHeader("X-Test-Header", "Test header value")
.buildAndCreate(client);
registerForCleanup(createdEventHook)

assertThat(createdEventHook.getId(), notNullValue())

EventHook retrievedEventHook = client.getEventHook(createdEventHook.getId())

assertThat(retrievedEventHook.getId(), notNullValue())
assertThat(retrievedEventHook.getName(), equalTo(name))
assertThat(createdEventHook.getStatus(), equalTo(EventHook.StatusEnum.ACTIVE))
assertThat(retrievedEventHook.getEvents().getItems(), iterableWithSize(2))
assertThat(retrievedEventHook.getChannel().getConfig().getUri(), equalTo("https://www.example.com/eventHooks"))

createdEventHook.deactivate()
}

@Test
void updateEventHookTest() {
String name = "event-hook-java-sdk-${UUID.randomUUID().toString()}"

EventHook createdEventHook = DefaultEventHookBuilder.instance()
.setName(name)
.setUrl("https://www.example.com/eventHooks")
.setAuthorizationHeaderValue("Test-Api-Key")
.addHeader("X-Test-Header", "Test header value")
.buildAndCreate(client);
registerForCleanup(createdEventHook)

assertThat(createdEventHook.getId(), notNullValue())

EventHook toBeUpdatedEventHook = createdEventHook.setName("updated-" + name)
createdEventHook.getEvents().getItems().add("user.lifecycle.deactivate")
createdEventHook.getChannel().getConfig().setAuthScheme(client.instantiate(EventHookChannelConfigAuthScheme)
.setType(EventHookChannelConfigAuthSchemeType.HEADER)
.setKey("Authorization")
.setValue("Test-Api-Key-Updated"))
.setUri("https://www.example.com/eventHooksUpdated")

EventHook updatedEventHook = toBeUpdatedEventHook.update()

assertThat(updatedEventHook.getId(), notNullValue())
assertThat(updatedEventHook.getId(), equalTo(createdEventHook.getId()))
assertThat(createdEventHook.getStatus(), equalTo(EventHook.StatusEnum.ACTIVE))
assertThat(updatedEventHook.getName(), equalTo("updated-" + name))
assertThat(updatedEventHook.getEvents().getItems(), iterableWithSize(3))
assertThat(updatedEventHook.getChannel().getConfig().getUri(), equalTo("https://www.example.com/eventHooksUpdated"))

createdEventHook.deactivate()
}

@Test
void deleteEventHookTest() {
String name = "event-hook-java-sdk-${UUID.randomUUID().toString()}"

EventHook createdEventHook = DefaultEventHookBuilder.instance()
.setName(name)
.setUrl("https://www.example.com/eventHooks")
.setAuthorizationHeaderValue("Test-Api-Key")
.addHeader("X-Test-Header", "Test header value")
.buildAndCreate(client);
registerForCleanup(createdEventHook)

assertThat(createdEventHook.getId(), notNullValue())

EventHook retrievedEventHook = client.getEventHook(createdEventHook.getId())
assertThat(retrievedEventHook.getId(), equalTo(createdEventHook.getId()))
assertThat(retrievedEventHook.getStatus(), equalTo(EventHook.StatusEnum.ACTIVE))

createdEventHook.deactivate()
createdEventHook.delete()

try {
client.getEventHook(createdEventHook.getId())
}
catch (ResourceException e) {
assertThat(e.status, equalTo(404))
}
}

@Test
void listAllEventHooksTest() {
String name = "event-hook-java-sdk-${UUID.randomUUID().toString()}"

EventHook createdEventHook = DefaultEventHookBuilder.instance()
.setName(name)
.setUrl("https://www.example.com/eventHooks")
.setAuthorizationHeaderValue("Test-Api-Key")
.addHeader("X-Test-Header", "Test header value")
.buildAndCreate(client);
registerForCleanup(createdEventHook)

assertThat(createdEventHook.getId(), notNullValue())

assertPresent(client.listEventHooks(), createdEventHook)

createdEventHook.deactivate()
}

@Test
void activateDeactivateEventHookTest() {
String name = "event-hook-java-sdk-${UUID.randomUUID().toString()}"

EventHook createdEventHook = DefaultEventHookBuilder.instance()
.setName(name)
.setUrl("https://www.example.com/eventHooks")
.setAuthorizationHeaderValue("Test-Api-Key")
.addHeader("X-Test-Header", "Test header value")
.buildAndCreate(client);
registerForCleanup(createdEventHook)

assertThat(createdEventHook.getId(), notNullValue())
assertThat(createdEventHook.getStatus(), equalTo(EventHook.StatusEnum.ACTIVE))

createdEventHook.deactivate()

EventHook retrievedEventHook = client.getEventHook(createdEventHook.getId())

assertThat(retrievedEventHook.getStatus(), equalTo(EventHook.StatusEnum.INACTIVE))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.okta.sdk.tests.it

import com.okta.sdk.resource.UserType
import com.okta.sdk.resource.user.type.UserType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user.type seems a bit redundant, maybe just tag it with user?
(maybe there are a bunch of other user type objects 🤷)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed :)

UserType and UserTypeList are the only classes in package com.okta.sdk.resource.user.type.

image

Will consider moving it to a different package (say com.okta.sdk.resource.user) as the user.type seems redundant. Will do this as part of separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://oktainc.atlassian.net/browse/OKTA-305898 created to address this improvement.

import com.okta.sdk.tests.it.util.ITSupport
import org.testng.annotations.Test
import wiremock.org.apache.commons.lang3.RandomStringUtils
Expand Down