Honor accessedViaHttps flag on self and next links

Wasn't calling prefixForHttps on self and next links

Add tests to validate they are being set correctly

Change api-config.yml to work with debugging on devstack

Change-Id: Ie85d7376c0dda88fa6c721fb180a826cca2a4f75
Closes-Bug: #1563499
This commit is contained in:
Craig Bryant 2016-03-30 11:03:15 -06:00
parent 235572aac0
commit b917418c67
4 changed files with 123 additions and 29 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
* (C) Copyright 2015-2016 Hewlett Packard Enterprise Development Company LP
*
* 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
@ -24,4 +24,39 @@ public class Paged {
public List<?> elements;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((elements == null) ? 0 : elements.hashCode());
result = prime * result + ((links == null) ? 0 : links.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Paged other = (Paged) obj;
if (elements == null) {
if (other.elements != null)
return false;
} else if (!elements.equals(other.elements))
return false;
if (links == null) {
if (other.links != null)
return false;
} else if (!links.equals(other.links))
return false;
return true;
}
@Override
public String toString() {
return "Paged [links=" + links + ", elements=" + elements + "]";
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2016 Hewlett Packard Enterprise Development Company, L.P.
* (C) Copyright 2014-2016 Hewlett Packard Enterprise Development Company LP
*
* 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
@ -160,7 +160,7 @@ public final class Links {
* @param uriInfo
* @return
*/
public static Object paginate(int limit, List<? extends AbstractEntity> elements, UriInfo uriInfo)
public static Paged paginate(int limit, List<? extends AbstractEntity> elements, UriInfo uriInfo)
throws UnsupportedEncodingException {
// Check for paging turned off. Happens if maxQueryLimit is not set or is set to zero.
@ -362,7 +362,7 @@ public final class Links {
Link selfLink = new Link();
selfLink.rel = "self";
selfLink.href = uriInfo.getRequestUri().toString();
selfLink.href = prefixForHttps(uriInfo.getRequestUri().toString());
return selfLink;
}
@ -373,8 +373,8 @@ public final class Links {
nextLink.rel = "next";
// Create a new URL with the new offset.
nextLink.href = uriInfo.getAbsolutePath().toString()
+ "?offset=" + URLEncoder.encode(offset, "UTF-8");
nextLink.href = prefixForHttps(uriInfo.getAbsolutePath().toString()
+ "?offset=" + URLEncoder.encode(offset, "UTF-8"));
// Add the query parms back to the URL without the original offset.
for (String parmKey : uriInfo.getQueryParameters().keySet()) {

View File

@ -16,14 +16,14 @@ maxQueryLimit: 10000
kafka:
brokerUris:
- 192.168.10.4:9092
- 192.168.10.6:9092
zookeeperUris:
- 192.168.10.4:2181
- 192.168.10.6:2181
healthCheckTopic: healthcheck
mysql:
driverClass: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.10.4:3306/mon?connectTimeout=5000&autoReconnect=true&useLegacyDatetimeCode=false
url: jdbc:mysql://192.168.10.6:3306/mon?connectTimeout=5000&autoReconnect=true&useLegacyDatetimeCode=false
user: monapi
password: password
maxWaitForConnection: 1s
@ -42,8 +42,7 @@ influxDB:
maxHttpConnections: 100
name: mon
replicationFactor: 1
url: http://192.168.10.4:8086
# url: http://10.10.10.2:8086
url: http://192.168.10.6:8086
user: mon_api
password: password
@ -51,7 +50,7 @@ influxDB:
# As of 7/10 there is a bug in the monasca-api that requires this section even if databaseType is set to influxdb
vertica:
driverClass: com.vertica.jdbc.Driver
url: jdbc:vertica://localhost/mon
url: jdbc:vertica://192.168.10.6/mon
user: dbadmin
password: password
maxWaitForConnection: 1s
@ -62,9 +61,9 @@ vertica:
middleware:
enabled: true
serverVIP: 192.168.10.5
serverVIP: 192.168.10.6
serverPort: 5000
connTimeout: 500
connTimeout: 2000
connSSLClientAuth: false
connPoolMaxActive: 3
connPoolMaxIdle: 3
@ -76,13 +75,11 @@ middleware:
agentAuthorizedRoles: [monasca-agent]
adminAuthMethod: password
adminUser: admin
adminPassword: admin
adminPassword: secretadmin
adminProjectId:
adminProjectName:
adminProjectName: admin
adminUserDomainId:
adminUserDomainName:
adminUserProjectId:
adminUserProjectName:
adminToken:
timeToCacheToken: 600
maxTokenCacheSize: 1048576

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
* (C) Copyright 2015-2016 Hewlett Packard Enterprise Development Company LP
*
* 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
@ -16,6 +16,7 @@ package monasca.api.resource;
import monasca.api.domain.model.alarm.Alarm;
import monasca.api.domain.model.common.Link;
import monasca.api.domain.model.common.Paged;
import monasca.common.model.alarm.AlarmState;
import static org.testng.Assert.assertEquals;
import static org.mockito.Mockito.mock;
@ -24,51 +25,112 @@ import static org.mockito.Mockito.when;
import org.joda.time.DateTime;
import org.testng.annotations.Test;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
@Test
public class LinksTest {
public void shouldPrefixForHttps() {
private static final String ALARM_DEF_ID = "af72b3d8-51f3-4eee-8086-535b5e7a9dc8";
public void shouldPrefixForHttps() throws UnsupportedEncodingException, URISyntaxException {
Links.accessedViaHttps = true;
assertEquals(Links.prefixForHttps("http://abc123blah/blah/blah"),
"https://abc123blah/blah/blah");
assertEquals(Links.prefixForHttps("https://abc123blah/blah/blah"),
"https://abc123blah/blah/blah");
checkSelfNextLinks(true);
// Negative
Links.accessedViaHttps = false;
assertEquals(Links.prefixForHttps("http://abc123blah/blah/blah"), "http://abc123blah/blah/blah");
assertEquals(Links.prefixForHttps("https://abc123blah/blah/blah"),
"https://abc123blah/blah/blah");
checkSelfNextLinks(false);
}
private void checkSelfNextLinks(final boolean returnHttps) throws URISyntaxException,
UnsupportedEncodingException {
final String base = "http://TheVip:8070/v2.0/alarms";
final String limitParam = "limit=1";
final String url = base + "?" + limitParam;
final UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(new URI(url));
when(uriInfo.getAbsolutePath()).thenReturn(new URI(base));
final Map<String, String> params = new HashMap<>();
params.put("limit", "1");
@SuppressWarnings("unchecked")
final MultivaluedMap<String, String> mockParams = mock(MultivaluedMap.class);
when(uriInfo.getQueryParameters()).thenReturn(mockParams);
when(mockParams.keySet()).thenReturn(params.keySet());
when(mockParams.get("limit")).thenReturn(Arrays.asList("1"));
// Since limit is 1, need to give two elements so code knows to add next link
final List<Alarm> elements = new ArrayList<>();
elements.add(createAlarm());
elements.add(createAlarm());
final int limit = 1;
final Paged expected = new Paged();
final List<Link> links = new ArrayList<>();
String expectedSelf = url;
String expectedNext = base + "?offset=" + elements.get(0).getId() + "&" + limitParam;
if (returnHttps) {
expectedSelf = expectedSelf.replace("http", "https");
expectedNext = expectedNext.replace("http", "https");
}
links.add(new Link("self", expectedSelf));
links.add(new Link("next", expectedNext));
expected.links = links;
final ArrayList<Alarm> expectedElements = new ArrayList<Alarm>();
// Since limit is one, only the first element is returned
expectedElements.add(elements.get(0));
expected.elements = expectedElements;
final Paged actual = Links.paginate(limit, elements, uriInfo);
assertEquals(actual, expected);
}
private Alarm createAlarm() {
final String alarmId = UUID.randomUUID().toString();
final Alarm alarm = new Alarm(alarmId, ALARM_DEF_ID, "Test", "LOW", null, AlarmState.OK,
"OPEN", null,
DateTime.parse("2015-03-14T09:26:53"),
DateTime.parse("2015-03-14T09:26:53"),
DateTime.parse("2015-03-14T09:26:53"));
return alarm;
}
public void shouldHydrate() throws URISyntaxException {
checkHydrate("http://localhost:8080/");
checkHydrate("https://localhost/");
checkHydrate("https://localhost//");
checkHydrate(""); // Should not happen but handle relative pathss
checkHydrate(""); // Should not happen but handle relative paths
}
private void checkHydrate(final String base) throws URISyntaxException {
final UriInfo uriInfo = mock(UriInfo.class);
final String alarmId = "b6a454b7-b557-426a-af51-657d0a7384d6";
final URI uri = new URI(base);
when(uriInfo.getBaseUri()).thenReturn(uri);
final String alarmDefinitionId = "af72b3d8-51f3-4eee-8086-535b5e7a9dc8";
final Alarm alarm = new Alarm(alarmId, alarmDefinitionId, "Test", "LOW", null, AlarmState.OK,
"OPEN", null,
DateTime.parse("2015-03-14T09:26:53"),
DateTime.parse("2015-03-14T09:26:53"),
DateTime.parse("2015-03-14T09:26:53"));
final Alarm alarm = createAlarm();
alarm.setId("42");
Links.hydrate(alarm.getAlarmDefinition(), uriInfo, AlarmDefinitionResource.ALARM_DEFINITIONS_PATH);
assertEquals(alarm.getAlarmDefinition().getLinks().size(), 1);
assertEquals(alarm.getAlarmDefinition().getLinks().get(0), new Link("self", base
// Have to cut the first / off of AlarmDefinitionResource.ALARM_DEFINITIONS_PATH
+ AlarmDefinitionResource.ALARM_DEFINITIONS_PATH.substring(1) + "/"
+ alarmDefinitionId));
+ ALARM_DEF_ID));
}
}