Skip to content

Commit

Permalink
OKTA-289659: Add Event Hooks Integration Tests (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindkrishnakumar-okta authored Jun 17, 2020
1 parent b6f0ec9 commit 99e9de1
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 1 deletion.
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.EventHookBuilder
import com.okta.sdk.resource.event.hook.EventHookChannelConfigAuthScheme
import com.okta.sdk.resource.event.hook.EventHookChannelConfigAuthSchemeType

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 = EventHookBuilder.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.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 = EventHookBuilder.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 = EventHookBuilder.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 = EventHookBuilder.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 = EventHookBuilder.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 = EventHookBuilder.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
import com.okta.sdk.tests.it.util.ITSupport
import org.testng.annotations.Test
import wiremock.org.apache.commons.lang3.RandomStringUtils
Expand Down

0 comments on commit 99e9de1

Please sign in to comment.