Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
SmartNotifications + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Apr 3, 2017
1 parent cdc1328 commit cf8ec9a
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .0pdd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
errors:
- amihaiemil@gmail.com
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -82,12 +82,14 @@ public GithubNotificationsCheck(String notificationsEdp, String receiverEdp) {
throw new IllegalStateException ("Missing post.rest.roken system property!");
}
this.post = new NtPost(
new RtNotifications(
new Mention(),
new Authorization.WithTokenPrefix(
new Authorization.MandatoryFromSystem()
),
notificationsEdp
new SmartNotifications(
new RtNotifications(
new Mention(),
new Authorization.WithTokenPrefix(
new Authorization.MandatoryFromSystem()
),
notificationsEdp
)
),
new Authorization.MandatoryFromSystem(),
receiverEdp
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/charles/github/Mention.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/amihaiemil/charles/github/NtPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
package com.amihaiemil.charles.github;

import com.jcabi.http.Request;
import com.jcabi.http.response.RestResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;
Expand All @@ -36,6 +34,8 @@
import org.hamcrest.Matchers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jcabi.http.Request;
import com.jcabi.http.response.RestResponse;

/**
* Notifications post.
Expand Down Expand Up @@ -94,7 +94,6 @@ public void send() throws IOException {
} else {
log.info("No notifications to send");
}

}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/charles/github/Post.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/charles/github/Reason.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/com/amihaiemil/charles/github/SmartNotifications.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of mention-notifications-ejb nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.charles.github;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.json.JsonObject;

/**
* Smart notifications. If there are no new notifications for a few
* checks in a row, it will skip a few next checks, thus saving bandwidth
* and making less HTTP calls. This is all based on the assumption that
* notifications aren't so frequent, so it makes no sense to always, blindly
* repeat the cycle.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.2.0
* @todo #13:30m/DEV Let's make this configurable. Currently it is
* hard-coded to 3 empty Checks. It should configurable via the system
* property skip.empty.notifications.
*/
public final class SmartNotifications implements Notifications {

/**
* Original notifications to use.
*/
private Notifications original;

/**
* Count of empty notifications that can pass.
*/
private int allowedEmpty = 3;

/**
* Nr of consecutive empty checks.
*/
private int empty = 0;

/**
* Skip this fetch, or not?
*/
private boolean skip = false;

/**
* Ctor.
* @param orig Given Notifications
*/
public SmartNotifications(final Notifications orig) {
this.original = orig;
}

@Override
public List<JsonObject> fetch() throws IOException {
List<JsonObject> notifications = new ArrayList<>();
if(!this.skip) {
if(this.allowedEmpty == 0) {
this.allowedEmpty = 3;
this.empty = 0;
}
notifications = this.original.fetch();
if(notifications.isEmpty()) {
this.empty++;
} else {
this.empty = 0;
}
} else {
this.allowedEmpty--;
}
this.skip = this.empty >= this.allowedEmpty && this.allowedEmpty > 0;
return notifications;
}

@Override
public void markAsRead() throws IOException {
this.original.markAsRead();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of mention-notifications-ejb nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.amihaiemil.charles.github;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.json.Json;
import javax.json.JsonObject;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Unit tests for {@link SmartNotifications}
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $id$
* @since 1.2.0
*/
public final class SmartNotificationsTestcase {

/**
* SmartNotifications can skip 3 checks if the the previous
* 3 notifications came out empty.
* @throws IOException If something goes wrong.
*/
@Test
public void skipsThreeChecksAfterThreeEmptyChecks() throws IOException {
List<JsonObject> found = new ArrayList<>();
found.add(
Json.createObjectBuilder().add("found", "testnotification").build()
);
Notifications three = Mockito.mock(Notifications.class);
Mockito.when(three.fetch())
.thenReturn(new ArrayList<JsonObject>())
.thenReturn(new ArrayList<JsonObject>())
.thenReturn(new ArrayList<JsonObject>())
.thenReturn(found);
Notifications smart = new SmartNotifications(three);
for(int i=0;i<6;i++) {
MatcherAssert.assertThat(
smart.fetch(), Matchers.is(Matchers.empty())
);
}
List<JsonObject> fetched = smart.fetch();
MatcherAssert.assertThat(fetched.size(), Matchers.is(1));
MatcherAssert.assertThat(
fetched.get(0).getString("found"),
Matchers.equalTo("testnotification")
);
Mockito.verify(three, Mockito.times(4)).fetch();
}

/**
* SmartNotifications continues fetching notifications if less than 3
* empty checks happened in a row.
* @throws IOException If something goes wrong.
*/
@Test
public void doesntSkipAfterTwoEmptyChecks() throws IOException {
List<JsonObject> found = new ArrayList<>();
found.add(
Json.createObjectBuilder().add("found", "testnotification").build()
);
Notifications three = Mockito.mock(Notifications.class);
Mockito.when(three.fetch())
.thenReturn(new ArrayList<JsonObject>())
.thenReturn(new ArrayList<JsonObject>())
.thenReturn(found);
Notifications smart = new SmartNotifications(three);
MatcherAssert.assertThat(
smart.fetch(), Matchers.is(Matchers.empty())
);
MatcherAssert.assertThat(
smart.fetch(), Matchers.is(Matchers.empty())
);
MatcherAssert.assertThat(
smart.fetch(), Matchers.not(Matchers.is(Matchers.empty()))
);
MatcherAssert.assertThat(
smart.fetch(), Matchers.not(Matchers.is(Matchers.empty()))
);
MatcherAssert.assertThat(
smart.fetch(), Matchers.not(Matchers.is(Matchers.empty()))
);
MatcherAssert.assertThat(
smart.fetch(), Matchers.not(Matchers.is(Matchers.empty()))
);
Mockito.verify(three, Mockito.times(6)).fetch();
}
}

0 comments on commit cf8ec9a

Please sign in to comment.