From e98524f7770748ef79db82d48da0f97ab541d287 Mon Sep 17 00:00:00 2001 From: Skip Wonnell Date: Mon, 15 Jan 2018 09:38:13 -0600 Subject: Fix random test failure in TestChefApiClient The check of the timestamp header fails when rolling over to a new minute. Change the code to compare the length of the interval instead. Issue-ID: APPC-411 Change-Id: I3a98aab2de878335a2bc320d261fee3101fd0652 Signed-off-by: Skip Wonnell --- .../appc-chef-adapter-bundle/.gitignore | 3 +- .../adapter/chef/chefclient/TestChefApiClient.java | 56 +++++++++------------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/.gitignore b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/.gitignore index 755cdc373..541e6082d 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/.gitignore +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/.gitignore @@ -1,3 +1,4 @@ /bin/ /target/ -/.settings/ +/target-ide/ +/.settings/ \ No newline at end of file diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/chefclient/TestChefApiClient.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/chefclient/TestChefApiClient.java index bf718ab2f..b8be22560 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/chefclient/TestChefApiClient.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/chefclient/TestChefApiClient.java @@ -9,15 +9,15 @@ * 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. - * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. * ============LICENSE_END========================================================= */ @@ -27,15 +27,12 @@ package org.onap.appc.adapter.chef.chefclient; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; - import java.io.InputStream; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; import java.util.Properties; -import java.util.TimeZone; -import java.util.regex.Pattern; - - import org.junit.Before; import org.junit.Test; import org.onap.appc.adapter.chef.chefapi.ApiMethod; @@ -48,6 +45,7 @@ public class TestChefApiClient { private ChefApiClient client; private Properties props; + private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); @Before public void setup() throws IllegalArgumentException, IllegalAccessException { @@ -64,25 +62,22 @@ public class TestChefApiClient { e.printStackTrace(); fail("Could not initialize properties"); } - client = new ChefApiClient( - props.getProperty("org.onap.appc.adapter.chef.chefclient.userId"), - System.getProperty("user.dir") + - props.getProperty("org.onap.appc.adapter.chef.chefclient.pemPath"), + client = new ChefApiClient(props.getProperty("org.onap.appc.adapter.chef.chefclient.userId"), + System.getProperty("user.dir") + props.getProperty("org.onap.appc.adapter.chef.chefclient.pemPath"), props.getProperty("org.onap.appc.adapter.chef.chefclient.endPoint"), props.getProperty("org.onap.appc.adapter.chef.chefclient.organizations")); } @Test - public void testGet(){ + public void testGet() { Get get = client.get(props.getProperty("org.onap.appc.adapter.chef.chefclient.path")); ApiMethod method = get.execute(); String[] response = method.test.split("\n"); - thenStringShouldMatch("GET", response); } @Test - public void testPut(){ + public void testPut() { Put put = client.put(props.getProperty("org.onap.appc.adapter.chef.chefclient.path")); ApiMethod method = put.execute(); String[] response = method.test.split("\n"); @@ -100,7 +95,7 @@ public class TestChefApiClient { } @Test - public void testDelete(){ + public void testDelete() { Delete delete = client.delete(props.getProperty("org.onap.appc.adapter.chef.chefclient.path")); ApiMethod method = delete.execute(); String[] response = method.test.split("\n"); @@ -108,24 +103,17 @@ public class TestChefApiClient { thenStringShouldMatch("DELETE", response); } - private String timestamp(){ - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - sdf.setTimeZone(TimeZone.getTimeZone("UTC")); - String timeStamp = sdf.format(new Date()); - timeStamp = timeStamp.replace(" ", "T"); - timeStamp = timeStamp + "Z"; - return timeStamp; - } - - private void thenStringShouldMatch(String method, String[] response){ + private void thenStringShouldMatch(String method, String[] response) { assertEquals("sb Method:" + method, response[0]); assertEquals("Hashed Path:+JEk1y2gXwqZRweNjXYtx4ojxW8=", response[1]); assertEquals("X-Ops-Content-Hash:2jmj7l5rSw0yVb/vlWAYkK/YBwk=", response[2]); - String timestamp = timestamp().substring(0, timestamp().length() - 3); - String regEx = "X-Ops-Timestamp:" + - timestamp + - "..."; - assertTrue(Pattern.matches(regEx, response[3])); + checkTimestamp(response[3], 5000); assertEquals("X-Ops-UserId:test", response[4]); } -} + + private void checkTimestamp(String timeStampHeader, long maxDeltaMs) { + assertTrue(timeStampHeader.startsWith("X-Ops-Timestamp:")); + LocalDateTime ld1 = LocalDateTime.parse(timeStampHeader.replace("X-Ops-Timestamp:", ""), dtf); + assertTrue(ChronoUnit.MILLIS.between(ld1, LocalDateTime.now(ZoneId.of("UTC"))) <= maxDeltaMs); + } +} \ No newline at end of file -- cgit 1.2.3-korg